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(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 455 | bi = bio_alloc_mddev(GFP_NOIO, 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; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 610 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 611 | mddev->resync_min = 0; |
| 612 | mddev->resync_max = MaxSector; |
| 613 | mddev->level = LEVEL_NONE; |
| 614 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 615 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 616 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 617 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 619 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 621 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 622 | unit &= ~((1<<MdpMinorShift)-1); |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | retry: |
| 625 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 627 | if (unit) { |
| 628 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 629 | if (mddev->unit == unit) { |
| 630 | mddev_get(mddev); |
| 631 | spin_unlock(&all_mddevs_lock); |
| 632 | kfree(new); |
| 633 | return mddev; |
| 634 | } |
| 635 | |
| 636 | if (new) { |
| 637 | list_add(&new->all_mddevs, &all_mddevs); |
| 638 | spin_unlock(&all_mddevs_lock); |
| 639 | new->hold_active = UNTIL_IOCTL; |
| 640 | return new; |
| 641 | } |
| 642 | } else if (new) { |
| 643 | /* find an unused unit number */ |
| 644 | static int next_minor = 512; |
| 645 | int start = next_minor; |
| 646 | int is_free = 0; |
| 647 | int dev = 0; |
| 648 | while (!is_free) { |
| 649 | dev = MKDEV(MD_MAJOR, next_minor); |
| 650 | next_minor++; |
| 651 | if (next_minor > MINORMASK) |
| 652 | next_minor = 0; |
| 653 | if (next_minor == start) { |
| 654 | /* Oh dear, all in use. */ |
| 655 | spin_unlock(&all_mddevs_lock); |
| 656 | kfree(new); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | is_free = 1; |
| 661 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 662 | if (mddev->unit == dev) { |
| 663 | is_free = 0; |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | new->unit = dev; |
| 668 | new->md_minor = MINOR(dev); |
| 669 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | list_add(&new->all_mddevs, &all_mddevs); |
| 671 | spin_unlock(&all_mddevs_lock); |
| 672 | return new; |
| 673 | } |
| 674 | spin_unlock(&all_mddevs_lock); |
| 675 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 676 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if (!new) |
| 678 | return NULL; |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | new->unit = unit; |
| 681 | if (MAJOR(unit) == MD_MAJOR) |
| 682 | new->md_minor = MINOR(unit); |
| 683 | else |
| 684 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 685 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 686 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | goto retry; |
| 689 | } |
| 690 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 691 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 693 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 696 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 697 | { |
| 698 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 699 | } |
| 700 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 701 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 703 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 706 | static struct attribute_group md_redundancy_group; |
| 707 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 708 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 710 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 711 | /* These cannot be removed under reconfig_mutex as |
| 712 | * an access to the files will try to take reconfig_mutex |
| 713 | * while holding the file unremovable, which leads to |
| 714 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 715 | * So hold set sysfs_active while the remove in happeing, |
| 716 | * and anything else which might set ->to_remove or my |
| 717 | * otherwise change the sysfs namespace will fail with |
| 718 | * -EBUSY if sysfs_active is still set. |
| 719 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 720 | * test it under the same mutex to ensure its correct value |
| 721 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 722 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 723 | struct attribute_group *to_remove = mddev->to_remove; |
| 724 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 725 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 726 | mutex_unlock(&mddev->reconfig_mutex); |
| 727 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 728 | if (mddev->kobj.sd) { |
| 729 | if (to_remove != &md_redundancy_group) |
| 730 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 731 | if (mddev->pers == NULL || |
| 732 | mddev->pers->sync_request == NULL) { |
| 733 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 734 | if (mddev->sysfs_action) |
| 735 | sysfs_put(mddev->sysfs_action); |
| 736 | mddev->sysfs_action = NULL; |
| 737 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 738 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 739 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 740 | } else |
| 741 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 743 | /* As we've dropped the mutex we need a spinlock to |
| 744 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 745 | */ |
| 746 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 747 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 748 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 751 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 753 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 755 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (rdev->desc_nr == nr) |
| 757 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return NULL; |
| 760 | } |
| 761 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 762 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 764 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 766 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | if (rdev->bdev->bd_dev == dev) |
| 768 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 773 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 774 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 775 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 776 | list_for_each_entry(pers, &pers_list, list) { |
| 777 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 778 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 779 | if (strcmp(pers->name, clevel)==0) |
| 780 | return pers; |
| 781 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 782 | return NULL; |
| 783 | } |
| 784 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 785 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 786 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 788 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 789 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 792 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
| 794 | if (rdev->sb_page) |
| 795 | MD_BUG(); |
| 796 | |
| 797 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 798 | if (!rdev->sb_page) { |
| 799 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 800 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame^] | 806 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
| 808 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 809 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | rdev->sb_loaded = 0; |
| 811 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 812 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 813 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 815 | if (rdev->bb_page) { |
| 816 | put_page(rdev->bb_page); |
| 817 | rdev->bb_page = NULL; |
| 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame^] | 820 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 822 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 823 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 824 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 825 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 826 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 827 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 828 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 829 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 830 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 831 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 832 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 833 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 834 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 835 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 836 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 837 | } |
| 838 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 839 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 840 | sector_t sector, int size, struct page *page) |
| 841 | { |
| 842 | /* write first size bytes of page to sector of rdev |
| 843 | * Increment mddev->pending_writes before returning |
| 844 | * and decrement it on completion, waking up sb_wait |
| 845 | * if zero is reached. |
| 846 | * If an error occurred, call md_error |
| 847 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 848 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 849 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 850 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 851 | bio->bi_sector = sector; |
| 852 | bio_add_page(bio, page, size, 0); |
| 853 | bio->bi_private = rdev; |
| 854 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 855 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 856 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 857 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 858 | } |
| 859 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 860 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 861 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 862 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 863 | DEFINE_WAIT(wq); |
| 864 | for(;;) { |
| 865 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 866 | if (atomic_read(&mddev->pending_writes)==0) |
| 867 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 868 | schedule(); |
| 869 | } |
| 870 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 871 | } |
| 872 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 873 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 878 | 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] | 879 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 881 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | struct completion event; |
| 883 | int ret; |
| 884 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 885 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 887 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 888 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 889 | if (metadata_op) |
| 890 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 891 | else if (rdev->mddev->reshape_position != MaxSector && |
| 892 | (rdev->mddev->reshape_backwards == |
| 893 | (sector >= rdev->mddev->reshape_position))) |
| 894 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 895 | else |
| 896 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | bio_add_page(bio, page, size, 0); |
| 898 | init_completion(&event); |
| 899 | bio->bi_private = &event; |
| 900 | bio->bi_end_io = bi_complete; |
| 901 | submit_bio(rw, bio); |
| 902 | wait_for_completion(&event); |
| 903 | |
| 904 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 905 | bio_put(bio); |
| 906 | return ret; |
| 907 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 908 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 910 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | { |
| 912 | char b[BDEVNAME_SIZE]; |
| 913 | if (!rdev->sb_page) { |
| 914 | MD_BUG(); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | if (rdev->sb_loaded) |
| 918 | return 0; |
| 919 | |
| 920 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 921 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | goto fail; |
| 923 | rdev->sb_loaded = 1; |
| 924 | return 0; |
| 925 | |
| 926 | fail: |
| 927 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 928 | bdevname(rdev->bdev,b)); |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
| 932 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 933 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 934 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 935 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 936 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 937 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 941 | { |
| 942 | int ret; |
| 943 | mdp_super_t *tmp1, *tmp2; |
| 944 | |
| 945 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 946 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 947 | |
| 948 | if (!tmp1 || !tmp2) { |
| 949 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 950 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | goto abort; |
| 952 | } |
| 953 | |
| 954 | *tmp1 = *sb1; |
| 955 | *tmp2 = *sb2; |
| 956 | |
| 957 | /* |
| 958 | * nr_disks is not constant |
| 959 | */ |
| 960 | tmp1->nr_disks = 0; |
| 961 | tmp2->nr_disks = 0; |
| 962 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 963 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 965 | kfree(tmp1); |
| 966 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | return ret; |
| 968 | } |
| 969 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 970 | |
| 971 | static u32 md_csum_fold(u32 csum) |
| 972 | { |
| 973 | csum = (csum & 0xffff) + (csum >> 16); |
| 974 | return (csum & 0xffff) + (csum >> 16); |
| 975 | } |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 978 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 979 | u64 newcsum = 0; |
| 980 | u32 *sb32 = (u32*)sb; |
| 981 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | unsigned int disk_csum, csum; |
| 983 | |
| 984 | disk_csum = sb->sb_csum; |
| 985 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 986 | |
| 987 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 988 | newcsum += sb32[i]; |
| 989 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 990 | |
| 991 | |
| 992 | #ifdef CONFIG_ALPHA |
| 993 | /* This used to use csum_partial, which was wrong for several |
| 994 | * reasons including that different results are returned on |
| 995 | * different architectures. It isn't critical that we get exactly |
| 996 | * the same return value as before (we always csum_fold before |
| 997 | * testing, and that removes any differences). However as we |
| 998 | * know that csum_partial always returned a 16bit value on |
| 999 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1000 | */ |
| 1001 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1002 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1004 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return csum; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /* |
| 1010 | * Handle superblock details. |
| 1011 | * We want to be able to handle multiple superblock formats |
| 1012 | * so we have a common interface to them all, and an array of |
| 1013 | * different handlers. |
| 1014 | * We rely on user-space to write the initial superblock, and support |
| 1015 | * reading and updating of superblocks. |
| 1016 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1017 | * 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] | 1018 | * loads and validates a superblock on dev. |
| 1019 | * if refdev != NULL, compare superblocks on both devices |
| 1020 | * Return: |
| 1021 | * 0 - dev has a superblock that is compatible with refdev |
| 1022 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1023 | * so dev should be used as the refdev in future |
| 1024 | * -EINVAL superblock incompatible or invalid |
| 1025 | * -othererror e.g. -EIO |
| 1026 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1027 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * Verify that dev is acceptable into mddev. |
| 1029 | * The first time, mddev->raid_disks will be 0, and data from |
| 1030 | * dev should be merged in. Subsequent calls check that dev |
| 1031 | * is new enough. Return 0 or -EINVAL |
| 1032 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1033 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | * Update the superblock for rdev with data in mddev |
| 1035 | * This does not write to disc. |
| 1036 | * |
| 1037 | */ |
| 1038 | |
| 1039 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1040 | char *name; |
| 1041 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1042 | int (*load_super)(struct md_rdev *rdev, |
| 1043 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1044 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1045 | int (*validate_super)(struct mddev *mddev, |
| 1046 | struct md_rdev *rdev); |
| 1047 | void (*sync_super)(struct mddev *mddev, |
| 1048 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1049 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1050 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1051 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1052 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | }; |
| 1054 | |
| 1055 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1056 | * Check that the given mddev has no bitmap. |
| 1057 | * |
| 1058 | * This function is called from the run method of all personalities that do not |
| 1059 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1060 | * has a bitmap. Otherwise, it returns 0. |
| 1061 | * |
| 1062 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1063 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1064 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1065 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1066 | return 0; |
| 1067 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1068 | mdname(mddev), mddev->pers->name); |
| 1069 | return 1; |
| 1070 | } |
| 1071 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1072 | |
| 1073 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | * load_super for 0.90.0 |
| 1075 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1076 | 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] | 1077 | { |
| 1078 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1079 | mdp_super_t *sb; |
| 1080 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
| 1082 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1083 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | * it's at the end of the disk. |
| 1085 | * |
| 1086 | * It also happens to be a multiple of 4Kb. |
| 1087 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1088 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1090 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (ret) return ret; |
| 1092 | |
| 1093 | ret = -EINVAL; |
| 1094 | |
| 1095 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1096 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
| 1098 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1099 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1100 | b); |
| 1101 | goto abort; |
| 1102 | } |
| 1103 | |
| 1104 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1105 | sb->minor_version < 90 || |
| 1106 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1108 | sb->major_version, sb->minor_version, |
| 1109 | b); |
| 1110 | goto abort; |
| 1111 | } |
| 1112 | |
| 1113 | if (sb->raid_disks <= 0) |
| 1114 | goto abort; |
| 1115 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1116 | 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] | 1117 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1118 | b); |
| 1119 | goto abort; |
| 1120 | } |
| 1121 | |
| 1122 | rdev->preferred_minor = sb->md_minor; |
| 1123 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1124 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1125 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1126 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (sb->level == LEVEL_MULTIPATH) |
| 1129 | rdev->desc_nr = -1; |
| 1130 | else |
| 1131 | rdev->desc_nr = sb->this_disk.number; |
| 1132 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1133 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1135 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1137 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | if (!uuid_equal(refsb, sb)) { |
| 1139 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1140 | b, bdevname(refdev->bdev,b2)); |
| 1141 | goto abort; |
| 1142 | } |
| 1143 | if (!sb_equal(refsb, sb)) { |
| 1144 | printk(KERN_WARNING "md: %s has same UUID" |
| 1145 | " but different superblock to %s\n", |
| 1146 | b, bdevname(refdev->bdev, b2)); |
| 1147 | goto abort; |
| 1148 | } |
| 1149 | ev1 = md_event(sb); |
| 1150 | ev2 = md_event(refsb); |
| 1151 | if (ev1 > ev2) |
| 1152 | ret = 1; |
| 1153 | else |
| 1154 | ret = 0; |
| 1155 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1156 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1157 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1158 | if (rdev->sectors >= (2ULL << 32)) |
| 1159 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1161 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1162 | /* "this cannot possibly happen" ... */ |
| 1163 | ret = -EINVAL; |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | abort: |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * validate_super for 0.90.0 |
| 1171 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1172 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | { |
| 1174 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1175 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1176 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1178 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1179 | clear_bit(Faulty, &rdev->flags); |
| 1180 | clear_bit(In_sync, &rdev->flags); |
| 1181 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (mddev->raid_disks == 0) { |
| 1184 | mddev->major_version = 0; |
| 1185 | mddev->minor_version = sb->minor_version; |
| 1186 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1187 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1188 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | mddev->ctime = sb->ctime; |
| 1190 | mddev->utime = sb->utime; |
| 1191 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1192 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | mddev->layout = sb->layout; |
| 1194 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1195 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1196 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1197 | mddev->bitmap_info.offset = 0; |
| 1198 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1199 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1201 | if (mddev->minor_version >= 91) { |
| 1202 | mddev->reshape_position = sb->reshape_position; |
| 1203 | mddev->delta_disks = sb->delta_disks; |
| 1204 | mddev->new_level = sb->new_level; |
| 1205 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1206 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1207 | if (mddev->delta_disks < 0) |
| 1208 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1209 | } else { |
| 1210 | mddev->reshape_position = MaxSector; |
| 1211 | mddev->delta_disks = 0; |
| 1212 | mddev->new_level = mddev->level; |
| 1213 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1214 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1218 | mddev->recovery_cp = MaxSector; |
| 1219 | else { |
| 1220 | if (sb->events_hi == sb->cp_events_hi && |
| 1221 | sb->events_lo == sb->cp_events_lo) { |
| 1222 | mddev->recovery_cp = sb->recovery_cp; |
| 1223 | } else |
| 1224 | mddev->recovery_cp = 0; |
| 1225 | } |
| 1226 | |
| 1227 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1228 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1229 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1230 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1231 | |
| 1232 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1233 | |
| 1234 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1235 | mddev->bitmap_info.file == NULL) |
| 1236 | mddev->bitmap_info.offset = |
| 1237 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1238 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1239 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1240 | /* Insist on good event counter while assembling, except |
| 1241 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1243 | if (sb->disks[rdev->desc_nr].state & ( |
| 1244 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1245 | if (ev1 < mddev->events) |
| 1246 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1247 | } else if (mddev->bitmap) { |
| 1248 | /* if adding to array with a bitmap, then we can accept an |
| 1249 | * older device ... but not too old. |
| 1250 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1251 | if (ev1 < mddev->bitmap->events_cleared) |
| 1252 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1253 | } else { |
| 1254 | if (ev1 < mddev->events) |
| 1255 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1256 | return 0; |
| 1257 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | desc = sb->disks + rdev->desc_nr; |
| 1261 | |
| 1262 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1263 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1264 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1265 | desc->raid_disk < mddev->raid_disks */) { |
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 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1268 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1269 | /* active but not in sync implies recovery up to |
| 1270 | * reshape position. We don't know exactly where |
| 1271 | * that is, so set to zero for now */ |
| 1272 | if (mddev->minor_version >= 91) { |
| 1273 | rdev->recovery_offset = 0; |
| 1274 | rdev->raid_disk = desc->raid_disk; |
| 1275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1277 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1278 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1279 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1280 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | /* |
| 1285 | * sync_super for 0.90.0 |
| 1286 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1287 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | { |
| 1289 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1290 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* make rdev->sb match mddev data.. |
| 1295 | * |
| 1296 | * 1/ zero out disks |
| 1297 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1298 | * 3/ any empty disks < next_spare become removed |
| 1299 | * |
| 1300 | * disks[0] gets initialised to REMOVED because |
| 1301 | * we cannot be sure from other fields if it has |
| 1302 | * been initialised or not. |
| 1303 | */ |
| 1304 | int i; |
| 1305 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1306 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1307 | rdev->sb_size = MD_SB_BYTES; |
| 1308 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1309 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
| 1311 | memset(sb, 0, sizeof(*sb)); |
| 1312 | |
| 1313 | sb->md_magic = MD_SB_MAGIC; |
| 1314 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | sb->patch_version = mddev->patch_version; |
| 1316 | sb->gvalid_words = 0; /* ignored */ |
| 1317 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1318 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1319 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1320 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1321 | |
| 1322 | sb->ctime = mddev->ctime; |
| 1323 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1324 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | sb->raid_disks = mddev->raid_disks; |
| 1326 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1327 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | sb->utime = mddev->utime; |
| 1329 | sb->state = 0; |
| 1330 | sb->events_hi = (mddev->events>>32); |
| 1331 | sb->events_lo = (u32)mddev->events; |
| 1332 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1333 | if (mddev->reshape_position == MaxSector) |
| 1334 | sb->minor_version = 90; |
| 1335 | else { |
| 1336 | sb->minor_version = 91; |
| 1337 | sb->reshape_position = mddev->reshape_position; |
| 1338 | sb->new_level = mddev->new_level; |
| 1339 | sb->delta_disks = mddev->delta_disks; |
| 1340 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1341 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1342 | } |
| 1343 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | if (mddev->in_sync) |
| 1345 | { |
| 1346 | sb->recovery_cp = mddev->recovery_cp; |
| 1347 | sb->cp_events_hi = (mddev->events>>32); |
| 1348 | sb->cp_events_lo = (u32)mddev->events; |
| 1349 | if (mddev->recovery_cp == MaxSector) |
| 1350 | sb->state = (1<< MD_SB_CLEAN); |
| 1351 | } else |
| 1352 | sb->recovery_cp = 0; |
| 1353 | |
| 1354 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1355 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1357 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1358 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1361 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1363 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1364 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1365 | |
| 1366 | if (rdev2->raid_disk >= 0 && |
| 1367 | sb->minor_version >= 91) |
| 1368 | /* we have nowhere to store the recovery_offset, |
| 1369 | * but if it is not below the reshape_position, |
| 1370 | * we can piggy-back on that. |
| 1371 | */ |
| 1372 | is_active = 1; |
| 1373 | if (rdev2->raid_disk < 0 || |
| 1374 | test_bit(Faulty, &rdev2->flags)) |
| 1375 | is_active = 0; |
| 1376 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1377 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1379 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1380 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | d = &sb->disks[rdev2->desc_nr]; |
| 1382 | nr_disks++; |
| 1383 | d->number = rdev2->desc_nr; |
| 1384 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1385 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1386 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | d->raid_disk = rdev2->raid_disk; |
| 1388 | else |
| 1389 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1390 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1392 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1394 | if (test_bit(In_sync, &rdev2->flags)) |
| 1395 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | active++; |
| 1397 | working++; |
| 1398 | } else { |
| 1399 | d->state = 0; |
| 1400 | spare++; |
| 1401 | working++; |
| 1402 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1403 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1404 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1407 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1408 | mdp_disk_t *d = &sb->disks[i]; |
| 1409 | if (d->state == 0 && d->number == 0) { |
| 1410 | d->number = i; |
| 1411 | d->raid_disk = i; |
| 1412 | d->state = (1<<MD_DISK_REMOVED); |
| 1413 | d->state |= (1<<MD_DISK_FAULTY); |
| 1414 | failed++; |
| 1415 | } |
| 1416 | } |
| 1417 | sb->nr_disks = nr_disks; |
| 1418 | sb->active_disks = active; |
| 1419 | sb->working_disks = working; |
| 1420 | sb->failed_disks = failed; |
| 1421 | sb->spare_disks = spare; |
| 1422 | |
| 1423 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1424 | sb->sb_csum = calc_sb_csum(sb); |
| 1425 | } |
| 1426 | |
| 1427 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1428 | * rdev_size_change for 0.90.0 |
| 1429 | */ |
| 1430 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1431 | 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] | 1432 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1433 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1434 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1435 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1436 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1437 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1438 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1439 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1440 | /* Limit to 4TB as metadata cannot record more than that. |
| 1441 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1442 | */ |
| 1443 | if (num_sectors >= (2ULL << 32)) |
| 1444 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1446 | rdev->sb_page); |
| 1447 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1448 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1449 | } |
| 1450 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1451 | static int |
| 1452 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1453 | { |
| 1454 | /* non-zero offset changes not possible with v0.90 */ |
| 1455 | return new_offset == 0; |
| 1456 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1457 | |
| 1458 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | * version 1 superblock |
| 1460 | */ |
| 1461 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1462 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1464 | __le32 disk_csum; |
| 1465 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | unsigned long long newcsum; |
| 1467 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1468 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | int i; |
| 1470 | |
| 1471 | disk_csum = sb->sb_csum; |
| 1472 | sb->sb_csum = 0; |
| 1473 | newcsum = 0; |
| 1474 | for (i=0; size>=4; size -= 4 ) |
| 1475 | newcsum += le32_to_cpu(*isuper++); |
| 1476 | |
| 1477 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1478 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| 1480 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1481 | sb->sb_csum = disk_csum; |
| 1482 | return cpu_to_le32(csum); |
| 1483 | } |
| 1484 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1485 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1486 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1487 | 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] | 1488 | { |
| 1489 | struct mdp_superblock_1 *sb; |
| 1490 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1491 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1492 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1494 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
| 1496 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1497 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | * It is always aligned to a 4K boundary and |
| 1499 | * depeding on minor_version, it can be: |
| 1500 | * 0: At least 8K, but less than 12K, from end of device |
| 1501 | * 1: At start of device |
| 1502 | * 2: 4K from start of device. |
| 1503 | */ |
| 1504 | switch(minor_version) { |
| 1505 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1506 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1507 | sb_start -= 8*2; |
| 1508 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | break; |
| 1510 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1511 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | break; |
| 1513 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1514 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | break; |
| 1516 | default: |
| 1517 | return -EINVAL; |
| 1518 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1519 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1521 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1522 | * and it is safe to read 4k, so we do that |
| 1523 | */ |
| 1524 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | if (ret) return ret; |
| 1526 | |
| 1527 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1528 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
| 1530 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1531 | sb->major_version != cpu_to_le32(1) || |
| 1532 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1533 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1534 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | return -EINVAL; |
| 1536 | |
| 1537 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1538 | printk("md: invalid superblock checksum on %s\n", |
| 1539 | bdevname(rdev->bdev,b)); |
| 1540 | return -EINVAL; |
| 1541 | } |
| 1542 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1543 | printk("md: data_size too small on %s\n", |
| 1544 | bdevname(rdev->bdev,b)); |
| 1545 | return -EINVAL; |
| 1546 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1547 | if (sb->pad0 || |
| 1548 | sb->pad3[0] || |
| 1549 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1550 | /* Some padding is non-zero, might be a new feature */ |
| 1551 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | rdev->preferred_minor = 0xffff; |
| 1554 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1555 | rdev->new_data_offset = rdev->data_offset; |
| 1556 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1557 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1558 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1559 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1561 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1562 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1563 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1564 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1565 | |
| 1566 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1567 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1568 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1569 | if (minor_version |
| 1570 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1571 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1572 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1573 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1574 | rdev->desc_nr = -1; |
| 1575 | else |
| 1576 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1577 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1578 | if (!rdev->bb_page) { |
| 1579 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1580 | if (!rdev->bb_page) |
| 1581 | return -ENOMEM; |
| 1582 | } |
| 1583 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1584 | rdev->badblocks.count == 0) { |
| 1585 | /* need to load the bad block list. |
| 1586 | * Currently we limit it to one page. |
| 1587 | */ |
| 1588 | s32 offset; |
| 1589 | sector_t bb_sector; |
| 1590 | u64 *bbp; |
| 1591 | int i; |
| 1592 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1593 | if (sectors > (PAGE_SIZE / 512)) |
| 1594 | return -EINVAL; |
| 1595 | offset = le32_to_cpu(sb->bblog_offset); |
| 1596 | if (offset == 0) |
| 1597 | return -EINVAL; |
| 1598 | bb_sector = (long long)offset; |
| 1599 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1600 | rdev->bb_page, READ, true)) |
| 1601 | return -EIO; |
| 1602 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1603 | rdev->badblocks.shift = sb->bblog_shift; |
| 1604 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1605 | u64 bb = le64_to_cpu(*bbp); |
| 1606 | int count = bb & (0x3ff); |
| 1607 | u64 sector = bb >> 10; |
| 1608 | sector <<= sb->bblog_shift; |
| 1609 | count <<= sb->bblog_shift; |
| 1610 | if (bb + 1 == 0) |
| 1611 | break; |
| 1612 | if (md_set_badblocks(&rdev->badblocks, |
| 1613 | sector, count, 1) == 0) |
| 1614 | return -EINVAL; |
| 1615 | } |
| 1616 | } else if (sb->bblog_offset == 0) |
| 1617 | rdev->badblocks.shift = -1; |
| 1618 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1619 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1620 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1621 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1623 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
| 1625 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1626 | sb->level != refsb->level || |
| 1627 | sb->layout != refsb->layout || |
| 1628 | sb->chunksize != refsb->chunksize) { |
| 1629 | printk(KERN_WARNING "md: %s has strangely different" |
| 1630 | " superblock to %s\n", |
| 1631 | bdevname(rdev->bdev,b), |
| 1632 | bdevname(refdev->bdev,b2)); |
| 1633 | return -EINVAL; |
| 1634 | } |
| 1635 | ev1 = le64_to_cpu(sb->events); |
| 1636 | ev2 = le64_to_cpu(refsb->events); |
| 1637 | |
| 1638 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1639 | ret = 1; |
| 1640 | else |
| 1641 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1643 | if (minor_version) { |
| 1644 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1645 | sectors -= rdev->data_offset; |
| 1646 | } else |
| 1647 | sectors = rdev->sb_start; |
| 1648 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1650 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1651 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1654 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1656 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1657 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1659 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1660 | clear_bit(Faulty, &rdev->flags); |
| 1661 | clear_bit(In_sync, &rdev->flags); |
| 1662 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | if (mddev->raid_disks == 0) { |
| 1665 | mddev->major_version = 1; |
| 1666 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1667 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1668 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1670 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1671 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1672 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | mddev->layout = le32_to_cpu(sb->layout); |
| 1674 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1675 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1676 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1677 | mddev->bitmap_info.offset = 0; |
| 1678 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1679 | mddev->reshape_backwards = 0; |
| 1680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1682 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1683 | |
| 1684 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1685 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1686 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1687 | mddev->bitmap_info.file == NULL ) |
| 1688 | mddev->bitmap_info.offset = |
| 1689 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1690 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1691 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1692 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1693 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1694 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1695 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1696 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1697 | if (mddev->delta_disks < 0 || |
| 1698 | (mddev->delta_disks == 0 && |
| 1699 | (le32_to_cpu(sb->feature_map) |
| 1700 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1701 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1702 | } else { |
| 1703 | mddev->reshape_position = MaxSector; |
| 1704 | mddev->delta_disks = 0; |
| 1705 | mddev->new_level = mddev->level; |
| 1706 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1707 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1708 | } |
| 1709 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1710 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1711 | /* Insist of good event counter while assembling, except for |
| 1712 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1714 | if (rdev->desc_nr >= 0 && |
| 1715 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1716 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1717 | if (ev1 < mddev->events) |
| 1718 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1719 | } else if (mddev->bitmap) { |
| 1720 | /* If adding to array with a bitmap, then we can accept an |
| 1721 | * older device, but not too old. |
| 1722 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1723 | if (ev1 < mddev->bitmap->events_cleared) |
| 1724 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1725 | } else { |
| 1726 | if (ev1 < mddev->events) |
| 1727 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1728 | return 0; |
| 1729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1731 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1732 | if (rdev->desc_nr < 0 || |
| 1733 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1734 | role = 0xffff; |
| 1735 | rdev->desc_nr = -1; |
| 1736 | } else |
| 1737 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | switch(role) { |
| 1739 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | break; |
| 1741 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1742 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | break; |
| 1744 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1745 | if ((le32_to_cpu(sb->feature_map) & |
| 1746 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1747 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1748 | else |
| 1749 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | rdev->raid_disk = role; |
| 1751 | break; |
| 1752 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1753 | if (sb->devflags & WriteMostly1) |
| 1754 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1755 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1756 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1757 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1758 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1763 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | { |
| 1765 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1766 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | int max_dev, i; |
| 1768 | /* make rdev->sb match mddev and rdev data. */ |
| 1769 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1770 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | sb->feature_map = 0; |
| 1773 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1774 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1776 | |
| 1777 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1778 | sb->events = cpu_to_le64(mddev->events); |
| 1779 | if (mddev->in_sync) |
| 1780 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1781 | else |
| 1782 | sb->resync_offset = cpu_to_le64(0); |
| 1783 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1784 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1785 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1786 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1787 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1788 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1789 | sb->level = cpu_to_le32(mddev->level); |
| 1790 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1791 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1792 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1793 | sb->devflags |= WriteMostly1; |
| 1794 | else |
| 1795 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1796 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1797 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1798 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1799 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1800 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1801 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1802 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1803 | |
| 1804 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1805 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1806 | sb->feature_map |= |
| 1807 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1808 | sb->recovery_offset = |
| 1809 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1810 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1811 | if (test_bit(Replacement, &rdev->flags)) |
| 1812 | sb->feature_map |= |
| 1813 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1814 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1815 | if (mddev->reshape_position != MaxSector) { |
| 1816 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1817 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1818 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1819 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1820 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1821 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1822 | if (mddev->delta_disks == 0 && |
| 1823 | mddev->reshape_backwards) |
| 1824 | sb->feature_map |
| 1825 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1826 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1827 | sb->feature_map |
| 1828 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1829 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1830 | - rdev->data_offset)); |
| 1831 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1832 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1833 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1834 | if (rdev->badblocks.count == 0) |
| 1835 | /* Nothing to do for bad blocks*/ ; |
| 1836 | else if (sb->bblog_offset == 0) |
| 1837 | /* Cannot record bad blocks on this device */ |
| 1838 | md_error(mddev, rdev); |
| 1839 | else { |
| 1840 | struct badblocks *bb = &rdev->badblocks; |
| 1841 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1842 | u64 *p = bb->page; |
| 1843 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1844 | if (bb->changed) { |
| 1845 | unsigned seq; |
| 1846 | |
| 1847 | retry: |
| 1848 | seq = read_seqbegin(&bb->lock); |
| 1849 | |
| 1850 | memset(bbp, 0xff, PAGE_SIZE); |
| 1851 | |
| 1852 | for (i = 0 ; i < bb->count ; i++) { |
| 1853 | u64 internal_bb = *p++; |
| 1854 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1855 | | BB_LEN(internal_bb)); |
| 1856 | *bbp++ = cpu_to_le64(store_bb); |
| 1857 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1858 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1859 | if (read_seqretry(&bb->lock, seq)) |
| 1860 | goto retry; |
| 1861 | |
| 1862 | bb->sector = (rdev->sb_start + |
| 1863 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1864 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1869 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | if (rdev2->desc_nr+1 > max_dev) |
| 1871 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1872 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1873 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1874 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1875 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1876 | rdev->sb_size = max_dev * 2 + 256; |
| 1877 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1878 | if (rdev->sb_size & bmask) |
| 1879 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1880 | } else |
| 1881 | max_dev = le32_to_cpu(sb->max_dev); |
| 1882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | for (i=0; i<max_dev;i++) |
| 1884 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1885 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1886 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1888 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1890 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1892 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1893 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | else |
| 1895 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1896 | } |
| 1897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1899 | } |
| 1900 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1901 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1902 | 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] | 1903 | { |
| 1904 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1905 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1906 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1907 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1908 | if (rdev->data_offset != rdev->new_data_offset) |
| 1909 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1910 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1911 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1912 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1913 | max_sectors -= rdev->data_offset; |
| 1914 | if (!num_sectors || num_sectors > max_sectors) |
| 1915 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1916 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1917 | /* minor version 0 with bitmap we can't move */ |
| 1918 | return 0; |
| 1919 | } else { |
| 1920 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1921 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1922 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1923 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1924 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1925 | if (!num_sectors || num_sectors > max_sectors) |
| 1926 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1927 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1928 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1929 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1930 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1931 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1932 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1933 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1934 | rdev->sb_page); |
| 1935 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1936 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1937 | |
| 1938 | } |
| 1939 | |
| 1940 | static int |
| 1941 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1942 | unsigned long long new_offset) |
| 1943 | { |
| 1944 | /* All necessary checks on new >= old have been done */ |
| 1945 | struct bitmap *bitmap; |
| 1946 | if (new_offset >= rdev->data_offset) |
| 1947 | return 1; |
| 1948 | |
| 1949 | /* with 1.0 metadata, there is no metadata to tread on |
| 1950 | * so we can always move back */ |
| 1951 | if (rdev->mddev->minor_version == 0) |
| 1952 | return 1; |
| 1953 | |
| 1954 | /* otherwise we must be sure not to step on |
| 1955 | * any metadata, so stay: |
| 1956 | * 36K beyond start of superblock |
| 1957 | * beyond end of badblocks |
| 1958 | * beyond write-intent bitmap |
| 1959 | */ |
| 1960 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1961 | return 0; |
| 1962 | bitmap = rdev->mddev->bitmap; |
| 1963 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1964 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
| 1965 | bitmap->file_pages * (PAGE_SIZE>>9) > new_offset) |
| 1966 | return 0; |
| 1967 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1968 | return 0; |
| 1969 | |
| 1970 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1973 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | [0] = { |
| 1975 | .name = "0.90.0", |
| 1976 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1977 | .load_super = super_90_load, |
| 1978 | .validate_super = super_90_validate, |
| 1979 | .sync_super = super_90_sync, |
| 1980 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1981 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | }, |
| 1983 | [1] = { |
| 1984 | .name = "md-1", |
| 1985 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1986 | .load_super = super_1_load, |
| 1987 | .validate_super = super_1_validate, |
| 1988 | .sync_super = super_1_sync, |
| 1989 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1990 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | }, |
| 1992 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1994 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1995 | { |
| 1996 | if (mddev->sync_super) { |
| 1997 | mddev->sync_super(mddev, rdev); |
| 1998 | return; |
| 1999 | } |
| 2000 | |
| 2001 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2002 | |
| 2003 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2004 | } |
| 2005 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2006 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2008 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2010 | rcu_read_lock(); |
| 2011 | rdev_for_each_rcu(rdev, mddev1) |
| 2012 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2013 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2014 | rdev2->bdev->bd_contains) { |
| 2015 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2016 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2017 | } |
| 2018 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | return 0; |
| 2020 | } |
| 2021 | |
| 2022 | static LIST_HEAD(pending_raid_disks); |
| 2023 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2024 | /* |
| 2025 | * Try to register data integrity profile for an mddev |
| 2026 | * |
| 2027 | * This is called when an array is started and after a disk has been kicked |
| 2028 | * from the array. It only succeeds if all working and active component devices |
| 2029 | * are integrity capable with matching profiles. |
| 2030 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2031 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2032 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2033 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2034 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2035 | if (list_empty(&mddev->disks)) |
| 2036 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2037 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2038 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2039 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2040 | /* skip spares and non-functional disks */ |
| 2041 | if (test_bit(Faulty, &rdev->flags)) |
| 2042 | continue; |
| 2043 | if (rdev->raid_disk < 0) |
| 2044 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2045 | if (!reference) { |
| 2046 | /* Use the first rdev as the reference */ |
| 2047 | reference = rdev; |
| 2048 | continue; |
| 2049 | } |
| 2050 | /* does this rdev's profile match the reference profile? */ |
| 2051 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2052 | rdev->bdev->bd_disk) < 0) |
| 2053 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2054 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2055 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2056 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2057 | /* |
| 2058 | * All component devices are integrity capable and have matching |
| 2059 | * profiles, register the common profile for the md device. |
| 2060 | */ |
| 2061 | if (blk_integrity_register(mddev->gendisk, |
| 2062 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2063 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2064 | mdname(mddev)); |
| 2065 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2066 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2067 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2068 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2069 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2070 | mdname(mddev)); |
| 2071 | return -EINVAL; |
| 2072 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2073 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2074 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2075 | EXPORT_SYMBOL(md_integrity_register); |
| 2076 | |
| 2077 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2078 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2079 | { |
| 2080 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2081 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 2082 | |
| 2083 | if (!bi_mddev) /* nothing to do */ |
| 2084 | return; |
| 2085 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2086 | return; |
| 2087 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2088 | rdev->bdev->bd_disk) >= 0) |
| 2089 | return; |
| 2090 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2091 | blk_integrity_unregister(mddev->gendisk); |
| 2092 | } |
| 2093 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2094 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2095 | 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] | 2096 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2097 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2098 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2099 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2100 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
| 2102 | if (rdev->mddev) { |
| 2103 | MD_BUG(); |
| 2104 | return -EINVAL; |
| 2105 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2106 | |
| 2107 | /* prevent duplicates */ |
| 2108 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2109 | return -EEXIST; |
| 2110 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2111 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2112 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2113 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2114 | if (mddev->pers) { |
| 2115 | /* Cannot change size, so fail |
| 2116 | * If mddev->level <= 0, then we don't care |
| 2117 | * about aligning sizes (e.g. linear) |
| 2118 | */ |
| 2119 | if (mddev->level > 0) |
| 2120 | return -ENOSPC; |
| 2121 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2122 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | /* Verify rdev->desc_nr is unique. |
| 2126 | * If it is -1, assign a free number, else |
| 2127 | * check number is not in use |
| 2128 | */ |
| 2129 | if (rdev->desc_nr < 0) { |
| 2130 | int choice = 0; |
| 2131 | if (mddev->pers) choice = mddev->raid_disks; |
| 2132 | while (find_rdev_nr(mddev, choice)) |
| 2133 | choice++; |
| 2134 | rdev->desc_nr = choice; |
| 2135 | } else { |
| 2136 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2137 | return -EBUSY; |
| 2138 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2139 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2140 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2141 | mdname(mddev), mddev->max_disks); |
| 2142 | return -EBUSY; |
| 2143 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2144 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2145 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2146 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2149 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2150 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2151 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2152 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2153 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2154 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2155 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2156 | /* failure here is OK */; |
| 2157 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2158 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2159 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2160 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2161 | |
| 2162 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2163 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2166 | |
| 2167 | fail: |
| 2168 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2169 | b, mdname(mddev)); |
| 2170 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2173 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2174 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2175 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2176 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2177 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2180 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | { |
| 2182 | char b[BDEVNAME_SIZE]; |
| 2183 | if (!rdev->mddev) { |
| 2184 | MD_BUG(); |
| 2185 | return; |
| 2186 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2187 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2188 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2190 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2191 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2192 | sysfs_put(rdev->sysfs_state); |
| 2193 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2194 | kfree(rdev->badblocks.page); |
| 2195 | rdev->badblocks.count = 0; |
| 2196 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2197 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2198 | * writing to 'remove' to "dev/state". We also need |
| 2199 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2200 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2201 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2202 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2203 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2204 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * prevent the device from being mounted, repartitioned or |
| 2209 | * otherwise reused by a RAID array (or any other kernel |
| 2210 | * subsystem), by bd_claiming the device. |
| 2211 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2212 | 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] | 2213 | { |
| 2214 | int err = 0; |
| 2215 | struct block_device *bdev; |
| 2216 | char b[BDEVNAME_SIZE]; |
| 2217 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2218 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2219 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | if (IS_ERR(bdev)) { |
| 2221 | printk(KERN_ERR "md: could not open %s.\n", |
| 2222 | __bdevname(dev, b)); |
| 2223 | return PTR_ERR(bdev); |
| 2224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | rdev->bdev = bdev; |
| 2226 | return err; |
| 2227 | } |
| 2228 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2229 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | { |
| 2231 | struct block_device *bdev = rdev->bdev; |
| 2232 | rdev->bdev = NULL; |
| 2233 | if (!bdev) |
| 2234 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2235 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | void md_autodetect_dev(dev_t dev); |
| 2239 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2240 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | { |
| 2242 | char b[BDEVNAME_SIZE]; |
| 2243 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2244 | bdevname(rdev->bdev,b)); |
| 2245 | if (rdev->mddev) |
| 2246 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame^] | 2247 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2249 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2250 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | #endif |
| 2252 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2253 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2256 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | { |
| 2258 | unbind_rdev_from_array(rdev); |
| 2259 | export_rdev(rdev); |
| 2260 | } |
| 2261 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2262 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2264 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2266 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | if (!rdev->mddev) { |
| 2268 | MD_BUG(); |
| 2269 | continue; |
| 2270 | } |
| 2271 | kick_rdev_from_array(rdev); |
| 2272 | } |
| 2273 | if (!list_empty(&mddev->disks)) |
| 2274 | MD_BUG(); |
| 2275 | mddev->raid_disks = 0; |
| 2276 | mddev->major_version = 0; |
| 2277 | } |
| 2278 | |
| 2279 | static void print_desc(mdp_disk_t *desc) |
| 2280 | { |
| 2281 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2282 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2283 | } |
| 2284 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2285 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | { |
| 2287 | int i; |
| 2288 | |
| 2289 | printk(KERN_INFO |
| 2290 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2291 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2292 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2293 | sb->ctime); |
| 2294 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2295 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2296 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2297 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2298 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2299 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2300 | sb->failed_disks, sb->spare_disks, |
| 2301 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2302 | |
| 2303 | printk(KERN_INFO); |
| 2304 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2305 | mdp_disk_t *desc; |
| 2306 | |
| 2307 | desc = sb->disks + i; |
| 2308 | if (desc->number || desc->major || desc->minor || |
| 2309 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2310 | printk(" D %2d: ", i); |
| 2311 | print_desc(desc); |
| 2312 | } |
| 2313 | } |
| 2314 | printk(KERN_INFO "md: THIS: "); |
| 2315 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2318 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2319 | { |
| 2320 | __u8 *uuid; |
| 2321 | |
| 2322 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2323 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2324 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2325 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2326 | le32_to_cpu(sb->major_version), |
| 2327 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2328 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2329 | sb->set_name, |
| 2330 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2331 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2332 | |
| 2333 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2334 | printk(KERN_INFO |
| 2335 | "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] | 2336 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2337 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2338 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2339 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2340 | le32_to_cpu(sb->level), |
| 2341 | (unsigned long long)le64_to_cpu(sb->size), |
| 2342 | le32_to_cpu(sb->raid_disks), |
| 2343 | le32_to_cpu(sb->layout), |
| 2344 | le32_to_cpu(sb->chunksize), |
| 2345 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2346 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2347 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2348 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2349 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2350 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2351 | sb->devflags, |
| 2352 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2353 | (unsigned long long)le64_to_cpu(sb->events), |
| 2354 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2355 | le32_to_cpu(sb->sb_csum), |
| 2356 | le32_to_cpu(sb->max_dev) |
| 2357 | ); |
| 2358 | } |
| 2359 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2360 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | { |
| 2362 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2363 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2364 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2365 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2366 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2368 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2369 | switch (major_version) { |
| 2370 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2371 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2372 | break; |
| 2373 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2374 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2375 | break; |
| 2376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | } else |
| 2378 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2379 | } |
| 2380 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2381 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2383 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2384 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2385 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | char b[BDEVNAME_SIZE]; |
| 2387 | |
| 2388 | printk("\n"); |
| 2389 | printk("md: **********************************\n"); |
| 2390 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2391 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2392 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2394 | if (mddev->bitmap) |
| 2395 | bitmap_print_sb(mddev->bitmap); |
| 2396 | else |
| 2397 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2398 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2400 | printk("\n"); |
| 2401 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2402 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2403 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } |
| 2405 | printk("md: **********************************\n"); |
| 2406 | printk("\n"); |
| 2407 | } |
| 2408 | |
| 2409 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2410 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2412 | /* Update each superblock (in-memory image), but |
| 2413 | * if we are allowed to, skip spares which already |
| 2414 | * have the right event counter, or have one earlier |
| 2415 | * (which would mean they aren't being marked as dirty |
| 2416 | * with the rest of the array) |
| 2417 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2418 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2419 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2420 | if (rdev->sb_events == mddev->events || |
| 2421 | (nospares && |
| 2422 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2423 | rdev->sb_events+1 == mddev->events)) { |
| 2424 | /* Don't update this superblock */ |
| 2425 | rdev->sb_loaded = 2; |
| 2426 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2427 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2428 | rdev->sb_loaded = 1; |
| 2429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | } |
| 2431 | } |
| 2432 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2433 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2435 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2436 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2437 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2438 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2441 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2442 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2443 | if (rdev->raid_disk >= 0 && |
| 2444 | mddev->delta_disks >= 0 && |
| 2445 | !test_bit(In_sync, &rdev->flags) && |
| 2446 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2447 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2448 | |
| 2449 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2450 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2451 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2452 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2453 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2454 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2455 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2456 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2457 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2458 | md_ack_all_badblocks(&rdev->badblocks); |
| 2459 | md_error(mddev, rdev); |
| 2460 | } |
| 2461 | clear_bit(Blocked, &rdev->flags); |
| 2462 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2463 | wake_up(&rdev->blocked_wait); |
| 2464 | } |
| 2465 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2466 | wake_up(&mddev->sb_wait); |
| 2467 | return; |
| 2468 | } |
| 2469 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2470 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2471 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2472 | mddev->utime = get_seconds(); |
| 2473 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2474 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2475 | force_change = 1; |
| 2476 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2477 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2478 | * though if events isn't the right even/odd, we will have to do |
| 2479 | * spares after all |
| 2480 | */ |
| 2481 | nospares = 1; |
| 2482 | if (force_change) |
| 2483 | nospares = 0; |
| 2484 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2485 | /* If the array is degraded, then skipping spares is both |
| 2486 | * dangerous and fairly pointless. |
| 2487 | * Dangerous because a device that was removed from the array |
| 2488 | * might have a event_count that still looks up-to-date, |
| 2489 | * so it can be re-added without a resync. |
| 2490 | * Pointless because if there are any spares to skip, |
| 2491 | * then a recovery will happen and soon that array won't |
| 2492 | * be degraded any more and the spare can go back to sleep then. |
| 2493 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2494 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2495 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2496 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2497 | |
| 2498 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2499 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2500 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2501 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2502 | && mddev->can_decrease_events |
| 2503 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2504 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2505 | mddev->can_decrease_events = 0; |
| 2506 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2507 | /* otherwise we have to go forward and ... */ |
| 2508 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2509 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
| 2512 | if (!mddev->events) { |
| 2513 | /* |
| 2514 | * oops, this 64-bit counter should never wrap. |
| 2515 | * Either we are in around ~1 trillion A.C., assuming |
| 2516 | * 1 reboot per second, or we have a bug: |
| 2517 | */ |
| 2518 | MD_BUG(); |
| 2519 | mddev->events --; |
| 2520 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2521 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2522 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2523 | if (rdev->badblocks.changed) |
| 2524 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2525 | if (test_bit(Faulty, &rdev->flags)) |
| 2526 | set_bit(FaultRecorded, &rdev->flags); |
| 2527 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2528 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2529 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2530 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2532 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2533 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2535 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2536 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2538 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2539 | if (rdev->sb_loaded != 1) |
| 2540 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2542 | if (!test_bit(Faulty, &rdev->flags) && |
| 2543 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2544 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2545 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2546 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2547 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2548 | bdevname(rdev->bdev, b), |
| 2549 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2550 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2551 | if (rdev->badblocks.size) { |
| 2552 | md_super_write(mddev, rdev, |
| 2553 | rdev->badblocks.sector, |
| 2554 | rdev->badblocks.size << 9, |
| 2555 | rdev->bb_page); |
| 2556 | rdev->badblocks.size = 0; |
| 2557 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2558 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2559 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2560 | pr_debug("md: %s (skipping faulty)\n", |
| 2561 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2562 | else |
| 2563 | pr_debug("(skipping incremental s/r "); |
| 2564 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2565 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | /* only need to write one superblock... */ |
| 2567 | break; |
| 2568 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2569 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2570 | /* 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] | 2571 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2572 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2573 | if (mddev->in_sync != sync_req || |
| 2574 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2575 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2576 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2577 | goto repeat; |
| 2578 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2579 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2580 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2581 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2582 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2583 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2584 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2585 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2586 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2587 | clear_bit(Blocked, &rdev->flags); |
| 2588 | |
| 2589 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2590 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2591 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2592 | wake_up(&rdev->blocked_wait); |
| 2593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2596 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2597 | * We want to accept with case. For this we use cmd_match. |
| 2598 | */ |
| 2599 | static int cmd_match(const char *cmd, const char *str) |
| 2600 | { |
| 2601 | /* See if cmd, written into a sysfs file, matches |
| 2602 | * str. They must either be the same, or cmd can |
| 2603 | * have a trailing newline |
| 2604 | */ |
| 2605 | while (*cmd && *str && *cmd == *str) { |
| 2606 | cmd++; |
| 2607 | str++; |
| 2608 | } |
| 2609 | if (*cmd == '\n') |
| 2610 | cmd++; |
| 2611 | if (*str || *cmd) |
| 2612 | return 0; |
| 2613 | return 1; |
| 2614 | } |
| 2615 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2616 | struct rdev_sysfs_entry { |
| 2617 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2618 | ssize_t (*show)(struct md_rdev *, char *); |
| 2619 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2620 | }; |
| 2621 | |
| 2622 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2623 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2624 | { |
| 2625 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2626 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2627 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2628 | if (test_bit(Faulty, &rdev->flags) || |
| 2629 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2630 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2631 | sep = ","; |
| 2632 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2633 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2634 | len += sprintf(page+len, "%sin_sync",sep); |
| 2635 | sep = ","; |
| 2636 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2637 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2638 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2639 | sep = ","; |
| 2640 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2641 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2642 | (rdev->badblocks.unacked_exist |
| 2643 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2644 | len += sprintf(page+len, "%sblocked", sep); |
| 2645 | sep = ","; |
| 2646 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2647 | if (!test_bit(Faulty, &rdev->flags) && |
| 2648 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2649 | len += sprintf(page+len, "%sspare", sep); |
| 2650 | sep = ","; |
| 2651 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2652 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2653 | len += sprintf(page+len, "%swrite_error", sep); |
| 2654 | sep = ","; |
| 2655 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2656 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2657 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2658 | sep = ","; |
| 2659 | } |
| 2660 | if (test_bit(Replacement, &rdev->flags)) { |
| 2661 | len += sprintf(page+len, "%sreplacement", sep); |
| 2662 | sep = ","; |
| 2663 | } |
| 2664 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2665 | return len+sprintf(page+len, "\n"); |
| 2666 | } |
| 2667 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2668 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2669 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2670 | { |
| 2671 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2672 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2673 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2674 | * writemostly - sets write_mostly |
| 2675 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2676 | * blocked - sets the Blocked flags |
| 2677 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2678 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2679 | * write_error - sets WriteErrorSeen |
| 2680 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2681 | */ |
| 2682 | int err = -EINVAL; |
| 2683 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2684 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2685 | if (test_bit(Faulty, &rdev->flags)) |
| 2686 | err = 0; |
| 2687 | else |
| 2688 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2689 | } else if (cmd_match(buf, "remove")) { |
| 2690 | if (rdev->raid_disk >= 0) |
| 2691 | err = -EBUSY; |
| 2692 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2693 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2694 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2695 | if (mddev->pers) |
| 2696 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2697 | md_new_event(mddev); |
| 2698 | err = 0; |
| 2699 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2700 | } else if (cmd_match(buf, "writemostly")) { |
| 2701 | set_bit(WriteMostly, &rdev->flags); |
| 2702 | err = 0; |
| 2703 | } else if (cmd_match(buf, "-writemostly")) { |
| 2704 | clear_bit(WriteMostly, &rdev->flags); |
| 2705 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2706 | } else if (cmd_match(buf, "blocked")) { |
| 2707 | set_bit(Blocked, &rdev->flags); |
| 2708 | err = 0; |
| 2709 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2710 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2711 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2712 | /* metadata handler doesn't understand badblocks, |
| 2713 | * so we need to fail the device |
| 2714 | */ |
| 2715 | md_error(rdev->mddev, rdev); |
| 2716 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2717 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2718 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2719 | wake_up(&rdev->blocked_wait); |
| 2720 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2721 | md_wakeup_thread(rdev->mddev->thread); |
| 2722 | |
| 2723 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2724 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2725 | set_bit(In_sync, &rdev->flags); |
| 2726 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2727 | } else if (cmd_match(buf, "write_error")) { |
| 2728 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2729 | err = 0; |
| 2730 | } else if (cmd_match(buf, "-write_error")) { |
| 2731 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2732 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2733 | } else if (cmd_match(buf, "want_replacement")) { |
| 2734 | /* Any non-spare device that is not a replacement can |
| 2735 | * become want_replacement at any time, but we then need to |
| 2736 | * check if recovery is needed. |
| 2737 | */ |
| 2738 | if (rdev->raid_disk >= 0 && |
| 2739 | !test_bit(Replacement, &rdev->flags)) |
| 2740 | set_bit(WantReplacement, &rdev->flags); |
| 2741 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2742 | md_wakeup_thread(rdev->mddev->thread); |
| 2743 | err = 0; |
| 2744 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2745 | /* Clearing 'want_replacement' is always allowed. |
| 2746 | * Once replacements starts it is too late though. |
| 2747 | */ |
| 2748 | err = 0; |
| 2749 | clear_bit(WantReplacement, &rdev->flags); |
| 2750 | } else if (cmd_match(buf, "replacement")) { |
| 2751 | /* Can only set a device as a replacement when array has not |
| 2752 | * yet been started. Once running, replacement is automatic |
| 2753 | * from spares, or by assigning 'slot'. |
| 2754 | */ |
| 2755 | if (rdev->mddev->pers) |
| 2756 | err = -EBUSY; |
| 2757 | else { |
| 2758 | set_bit(Replacement, &rdev->flags); |
| 2759 | err = 0; |
| 2760 | } |
| 2761 | } else if (cmd_match(buf, "-replacement")) { |
| 2762 | /* Similarly, can only clear Replacement before start */ |
| 2763 | if (rdev->mddev->pers) |
| 2764 | err = -EBUSY; |
| 2765 | else { |
| 2766 | clear_bit(Replacement, &rdev->flags); |
| 2767 | err = 0; |
| 2768 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2769 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2770 | if (!err) |
| 2771 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2772 | return err ? err : len; |
| 2773 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2774 | static struct rdev_sysfs_entry rdev_state = |
| 2775 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2776 | |
| 2777 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2778 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2779 | { |
| 2780 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2781 | } |
| 2782 | |
| 2783 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2784 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2785 | { |
| 2786 | char *e; |
| 2787 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2788 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2789 | atomic_set(&rdev->corrected_errors, n); |
| 2790 | return len; |
| 2791 | } |
| 2792 | return -EINVAL; |
| 2793 | } |
| 2794 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2795 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2796 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2797 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2798 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2799 | { |
| 2800 | if (rdev->raid_disk < 0) |
| 2801 | return sprintf(page, "none\n"); |
| 2802 | else |
| 2803 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2804 | } |
| 2805 | |
| 2806 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2807 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2808 | { |
| 2809 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2810 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2811 | int slot = simple_strtoul(buf, &e, 10); |
| 2812 | if (strncmp(buf, "none", 4)==0) |
| 2813 | slot = -1; |
| 2814 | else if (e==buf || (*e && *e!= '\n')) |
| 2815 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2816 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2817 | /* Setting 'slot' on an active array requires also |
| 2818 | * updating the 'rd%d' link, and communicating |
| 2819 | * with the personality with ->hot_*_disk. |
| 2820 | * For now we only support removing |
| 2821 | * failed/spare devices. This normally happens automatically, |
| 2822 | * but not when the metadata is externally managed. |
| 2823 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2824 | if (rdev->raid_disk == -1) |
| 2825 | return -EEXIST; |
| 2826 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2827 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2828 | return -EINVAL; |
| 2829 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2830 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2831 | if (err) |
| 2832 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2833 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2834 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2835 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2836 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2837 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2838 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2839 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2840 | */ |
| 2841 | |
| 2842 | if (rdev->raid_disk != -1) |
| 2843 | return -EBUSY; |
| 2844 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2845 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2846 | return -EBUSY; |
| 2847 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2848 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2849 | return -EINVAL; |
| 2850 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2851 | if (slot >= rdev->mddev->raid_disks && |
| 2852 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2853 | return -ENOSPC; |
| 2854 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2855 | rdev->raid_disk = slot; |
| 2856 | if (test_bit(In_sync, &rdev->flags)) |
| 2857 | rdev->saved_raid_disk = slot; |
| 2858 | else |
| 2859 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2860 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2861 | err = rdev->mddev->pers-> |
| 2862 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2863 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2864 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2865 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2866 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2867 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2868 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2869 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2870 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2871 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2872 | if (slot >= rdev->mddev->raid_disks && |
| 2873 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2874 | return -ENOSPC; |
| 2875 | rdev->raid_disk = slot; |
| 2876 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2877 | clear_bit(Faulty, &rdev->flags); |
| 2878 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2879 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2880 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2881 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2882 | return len; |
| 2883 | } |
| 2884 | |
| 2885 | |
| 2886 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2887 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2888 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2889 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2890 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2891 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2892 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2896 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2897 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2898 | unsigned long long offset; |
| 2899 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2900 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2901 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2902 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2903 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2904 | /* Must set offset before size, so overlap checks |
| 2905 | * can be sane */ |
| 2906 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2907 | rdev->data_offset = offset; |
| 2908 | return len; |
| 2909 | } |
| 2910 | |
| 2911 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2912 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2913 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2914 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2915 | { |
| 2916 | return sprintf(page, "%llu\n", |
| 2917 | (unsigned long long)rdev->new_data_offset); |
| 2918 | } |
| 2919 | |
| 2920 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2921 | const char *buf, size_t len) |
| 2922 | { |
| 2923 | unsigned long long new_offset; |
| 2924 | struct mddev *mddev = rdev->mddev; |
| 2925 | |
| 2926 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2927 | return -EINVAL; |
| 2928 | |
| 2929 | if (mddev->sync_thread) |
| 2930 | return -EBUSY; |
| 2931 | if (new_offset == rdev->data_offset) |
| 2932 | /* reset is always permitted */ |
| 2933 | ; |
| 2934 | else if (new_offset > rdev->data_offset) { |
| 2935 | /* must not push array size beyond rdev_sectors */ |
| 2936 | if (new_offset - rdev->data_offset |
| 2937 | + mddev->dev_sectors > rdev->sectors) |
| 2938 | return -E2BIG; |
| 2939 | } |
| 2940 | /* Metadata worries about other space details. */ |
| 2941 | |
| 2942 | /* decreasing the offset is inconsistent with a backwards |
| 2943 | * reshape. |
| 2944 | */ |
| 2945 | if (new_offset < rdev->data_offset && |
| 2946 | mddev->reshape_backwards) |
| 2947 | return -EINVAL; |
| 2948 | /* Increasing offset is inconsistent with forwards |
| 2949 | * reshape. reshape_direction should be set to |
| 2950 | * 'backwards' first. |
| 2951 | */ |
| 2952 | if (new_offset > rdev->data_offset && |
| 2953 | !mddev->reshape_backwards) |
| 2954 | return -EINVAL; |
| 2955 | |
| 2956 | if (mddev->pers && mddev->persistent && |
| 2957 | !super_types[mddev->major_version] |
| 2958 | .allow_new_offset(rdev, new_offset)) |
| 2959 | return -E2BIG; |
| 2960 | rdev->new_data_offset = new_offset; |
| 2961 | if (new_offset > rdev->data_offset) |
| 2962 | mddev->reshape_backwards = 1; |
| 2963 | else if (new_offset < rdev->data_offset) |
| 2964 | mddev->reshape_backwards = 0; |
| 2965 | |
| 2966 | return len; |
| 2967 | } |
| 2968 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2969 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2970 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2971 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2972 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2973 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2974 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2975 | } |
| 2976 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2977 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2978 | { |
| 2979 | /* check if two start/length pairs overlap */ |
| 2980 | if (s1+l1 <= s2) |
| 2981 | return 0; |
| 2982 | if (s2+l2 <= s1) |
| 2983 | return 0; |
| 2984 | return 1; |
| 2985 | } |
| 2986 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2987 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2988 | { |
| 2989 | unsigned long long blocks; |
| 2990 | sector_t new; |
| 2991 | |
| 2992 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2993 | return -EINVAL; |
| 2994 | |
| 2995 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2996 | return -EINVAL; /* sector conversion overflow */ |
| 2997 | |
| 2998 | new = blocks * 2; |
| 2999 | if (new != blocks * 2) |
| 3000 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3001 | |
| 3002 | *sectors = new; |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3006 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3007 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3008 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3009 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3010 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3011 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3012 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3013 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3014 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3015 | if (rdev->data_offset != rdev->new_data_offset) |
| 3016 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3017 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3018 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3019 | sectors = super_types[my_mddev->major_version]. |
| 3020 | rdev_size_change(rdev, sectors); |
| 3021 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3022 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3023 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3024 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3025 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3026 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3027 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3028 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3029 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3030 | rdev->sectors = sectors; |
| 3031 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3032 | /* need to check that all other rdevs with the same ->bdev |
| 3033 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3034 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3035 | * we have to change it back, we will have the lock again. |
| 3036 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3037 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3038 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3039 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3040 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3041 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3042 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3043 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3044 | |
| 3045 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3046 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3047 | if (rdev->bdev == rdev2->bdev && |
| 3048 | rdev != rdev2 && |
| 3049 | overlaps(rdev->data_offset, rdev->sectors, |
| 3050 | rdev2->data_offset, |
| 3051 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3052 | overlap = 1; |
| 3053 | break; |
| 3054 | } |
| 3055 | mddev_unlock(mddev); |
| 3056 | if (overlap) { |
| 3057 | mddev_put(mddev); |
| 3058 | break; |
| 3059 | } |
| 3060 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3061 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3062 | if (overlap) { |
| 3063 | /* Someone else could have slipped in a size |
| 3064 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3065 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3066 | * safe, and trust userspace not to race with |
| 3067 | * itself |
| 3068 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3069 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3070 | return -EBUSY; |
| 3071 | } |
| 3072 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3073 | return len; |
| 3074 | } |
| 3075 | |
| 3076 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3077 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3078 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3079 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3080 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3081 | { |
| 3082 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3083 | |
| 3084 | if (test_bit(In_sync, &rdev->flags) || |
| 3085 | recovery_start == MaxSector) |
| 3086 | return sprintf(page, "none\n"); |
| 3087 | |
| 3088 | return sprintf(page, "%llu\n", recovery_start); |
| 3089 | } |
| 3090 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3091 | 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] | 3092 | { |
| 3093 | unsigned long long recovery_start; |
| 3094 | |
| 3095 | if (cmd_match(buf, "none")) |
| 3096 | recovery_start = MaxSector; |
| 3097 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3098 | return -EINVAL; |
| 3099 | |
| 3100 | if (rdev->mddev->pers && |
| 3101 | rdev->raid_disk >= 0) |
| 3102 | return -EBUSY; |
| 3103 | |
| 3104 | rdev->recovery_offset = recovery_start; |
| 3105 | if (recovery_start == MaxSector) |
| 3106 | set_bit(In_sync, &rdev->flags); |
| 3107 | else |
| 3108 | clear_bit(In_sync, &rdev->flags); |
| 3109 | return len; |
| 3110 | } |
| 3111 | |
| 3112 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3113 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3114 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3115 | |
| 3116 | static ssize_t |
| 3117 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3118 | static ssize_t |
| 3119 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3120 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3121 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3122 | { |
| 3123 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3124 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3125 | 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] | 3126 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3127 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3128 | /* Maybe that ack was all we needed */ |
| 3129 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3130 | wake_up(&rdev->blocked_wait); |
| 3131 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3132 | } |
| 3133 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3134 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3135 | |
| 3136 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3137 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3138 | { |
| 3139 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3140 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3141 | 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] | 3142 | { |
| 3143 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3144 | } |
| 3145 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3146 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3147 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3148 | static struct attribute *rdev_default_attrs[] = { |
| 3149 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3150 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3151 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3152 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3153 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3154 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3155 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3156 | &rdev_bad_blocks.attr, |
| 3157 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3158 | NULL, |
| 3159 | }; |
| 3160 | static ssize_t |
| 3161 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3162 | { |
| 3163 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3164 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3165 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3166 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3167 | |
| 3168 | if (!entry->show) |
| 3169 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3170 | |
| 3171 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3172 | if (!rv) { |
| 3173 | if (rdev->mddev == NULL) |
| 3174 | rv = -EBUSY; |
| 3175 | else |
| 3176 | rv = entry->show(rdev, page); |
| 3177 | mddev_unlock(mddev); |
| 3178 | } |
| 3179 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | static ssize_t |
| 3183 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3184 | const char *page, size_t length) |
| 3185 | { |
| 3186 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3187 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3188 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3189 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3190 | |
| 3191 | if (!entry->store) |
| 3192 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3193 | if (!capable(CAP_SYS_ADMIN)) |
| 3194 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3195 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3196 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3197 | if (rdev->mddev == NULL) |
| 3198 | rv = -EBUSY; |
| 3199 | else |
| 3200 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3201 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3202 | } |
| 3203 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | static void rdev_free(struct kobject *ko) |
| 3207 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3208 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3209 | kfree(rdev); |
| 3210 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3211 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3212 | .show = rdev_attr_show, |
| 3213 | .store = rdev_attr_store, |
| 3214 | }; |
| 3215 | static struct kobj_type rdev_ktype = { |
| 3216 | .release = rdev_free, |
| 3217 | .sysfs_ops = &rdev_sysfs_ops, |
| 3218 | .default_attrs = rdev_default_attrs, |
| 3219 | }; |
| 3220 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3221 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3222 | { |
| 3223 | rdev->desc_nr = -1; |
| 3224 | rdev->saved_raid_disk = -1; |
| 3225 | rdev->raid_disk = -1; |
| 3226 | rdev->flags = 0; |
| 3227 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3228 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3229 | rdev->sb_events = 0; |
| 3230 | rdev->last_read_error.tv_sec = 0; |
| 3231 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3232 | rdev->sb_loaded = 0; |
| 3233 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3234 | atomic_set(&rdev->nr_pending, 0); |
| 3235 | atomic_set(&rdev->read_errors, 0); |
| 3236 | atomic_set(&rdev->corrected_errors, 0); |
| 3237 | |
| 3238 | INIT_LIST_HEAD(&rdev->same_set); |
| 3239 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3240 | |
| 3241 | /* Add space to store bad block list. |
| 3242 | * This reserves the space even on arrays where it cannot |
| 3243 | * be used - I wonder if that matters |
| 3244 | */ |
| 3245 | rdev->badblocks.count = 0; |
| 3246 | rdev->badblocks.shift = 0; |
| 3247 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3248 | seqlock_init(&rdev->badblocks.lock); |
| 3249 | if (rdev->badblocks.page == NULL) |
| 3250 | return -ENOMEM; |
| 3251 | |
| 3252 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3253 | } |
| 3254 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | /* |
| 3256 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3257 | * |
| 3258 | * mark the device faulty if: |
| 3259 | * |
| 3260 | * - the device is nonexistent (zero size) |
| 3261 | * - the device has no valid superblock |
| 3262 | * |
| 3263 | * a faulty rdev _never_ has rdev->sb set. |
| 3264 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3265 | 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] | 3266 | { |
| 3267 | char b[BDEVNAME_SIZE]; |
| 3268 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3269 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | sector_t size; |
| 3271 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3272 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | if (!rdev) { |
| 3274 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3275 | return ERR_PTR(-ENOMEM); |
| 3276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3278 | err = md_rdev_init(rdev); |
| 3279 | if (err) |
| 3280 | goto abort_free; |
| 3281 | err = alloc_disk_sb(rdev); |
| 3282 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | goto abort_free; |
| 3284 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3285 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | if (err) |
| 3287 | goto abort_free; |
| 3288 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3289 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3290 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3291 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | if (!size) { |
| 3293 | printk(KERN_WARNING |
| 3294 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3295 | bdevname(rdev->bdev,b)); |
| 3296 | err = -EINVAL; |
| 3297 | goto abort_free; |
| 3298 | } |
| 3299 | |
| 3300 | if (super_format >= 0) { |
| 3301 | err = super_types[super_format]. |
| 3302 | load_super(rdev, NULL, super_minor); |
| 3303 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3304 | printk(KERN_WARNING |
| 3305 | "md: %s does not have a valid v%d.%d " |
| 3306 | "superblock, not importing!\n", |
| 3307 | bdevname(rdev->bdev,b), |
| 3308 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | goto abort_free; |
| 3310 | } |
| 3311 | if (err < 0) { |
| 3312 | printk(KERN_WARNING |
| 3313 | "md: could not read %s's sb, not importing!\n", |
| 3314 | bdevname(rdev->bdev,b)); |
| 3315 | goto abort_free; |
| 3316 | } |
| 3317 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3318 | if (super_format == -1) |
| 3319 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3320 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | return rdev; |
| 3323 | |
| 3324 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3325 | if (rdev->bdev) |
| 3326 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame^] | 3327 | md_rdev_clear(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3328 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | kfree(rdev); |
| 3330 | return ERR_PTR(err); |
| 3331 | } |
| 3332 | |
| 3333 | /* |
| 3334 | * Check a full RAID array for plausibility |
| 3335 | */ |
| 3336 | |
| 3337 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3338 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | { |
| 3340 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3341 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | char b[BDEVNAME_SIZE]; |
| 3343 | |
| 3344 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3345 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | switch (super_types[mddev->major_version]. |
| 3347 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3348 | case 1: |
| 3349 | freshest = rdev; |
| 3350 | break; |
| 3351 | case 0: |
| 3352 | break; |
| 3353 | default: |
| 3354 | printk( KERN_ERR \ |
| 3355 | "md: fatal superblock inconsistency in %s" |
| 3356 | " -- removing from array\n", |
| 3357 | bdevname(rdev->bdev,b)); |
| 3358 | kick_rdev_from_array(rdev); |
| 3359 | } |
| 3360 | |
| 3361 | |
| 3362 | super_types[mddev->major_version]. |
| 3363 | validate_super(mddev, freshest); |
| 3364 | |
| 3365 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3366 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3367 | if (mddev->max_disks && |
| 3368 | (rdev->desc_nr >= mddev->max_disks || |
| 3369 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3370 | printk(KERN_WARNING |
| 3371 | "md: %s: %s: only %d devices permitted\n", |
| 3372 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3373 | mddev->max_disks); |
| 3374 | kick_rdev_from_array(rdev); |
| 3375 | continue; |
| 3376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | if (rdev != freshest) |
| 3378 | if (super_types[mddev->major_version]. |
| 3379 | validate_super(mddev, rdev)) { |
| 3380 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3381 | " from array!\n", |
| 3382 | bdevname(rdev->bdev,b)); |
| 3383 | kick_rdev_from_array(rdev); |
| 3384 | continue; |
| 3385 | } |
| 3386 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3387 | rdev->desc_nr = i++; |
| 3388 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3389 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3390 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3391 | rdev->raid_disk = -1; |
| 3392 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | } |
| 3394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3397 | /* Read a fixed-point number. |
| 3398 | * Numbers in sysfs attributes should be in "standard" units where |
| 3399 | * possible, so time should be in seconds. |
| 3400 | * However we internally use a a much smaller unit such as |
| 3401 | * milliseconds or jiffies. |
| 3402 | * This function takes a decimal number with a possible fractional |
| 3403 | * component, and produces an integer which is the result of |
| 3404 | * multiplying that number by 10^'scale'. |
| 3405 | * all without any floating-point arithmetic. |
| 3406 | */ |
| 3407 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3408 | { |
| 3409 | unsigned long result = 0; |
| 3410 | long decimals = -1; |
| 3411 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3412 | if (*cp == '.') |
| 3413 | decimals = 0; |
| 3414 | else if (decimals < scale) { |
| 3415 | unsigned int value; |
| 3416 | value = *cp - '0'; |
| 3417 | result = result * 10 + value; |
| 3418 | if (decimals >= 0) |
| 3419 | decimals++; |
| 3420 | } |
| 3421 | cp++; |
| 3422 | } |
| 3423 | if (*cp == '\n') |
| 3424 | cp++; |
| 3425 | if (*cp) |
| 3426 | return -EINVAL; |
| 3427 | if (decimals < 0) |
| 3428 | decimals = 0; |
| 3429 | while (decimals < scale) { |
| 3430 | result *= 10; |
| 3431 | decimals ++; |
| 3432 | } |
| 3433 | *res = result; |
| 3434 | return 0; |
| 3435 | } |
| 3436 | |
| 3437 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3438 | static void md_safemode_timeout(unsigned long data); |
| 3439 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3440 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3441 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3442 | { |
| 3443 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3444 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3445 | } |
| 3446 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3447 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3448 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3449 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3450 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3451 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3452 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3453 | if (msec == 0) |
| 3454 | mddev->safemode_delay = 0; |
| 3455 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3456 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3457 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3458 | if (mddev->safemode_delay == 0) |
| 3459 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3460 | if (mddev->safemode_delay < old_delay) |
| 3461 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3462 | } |
| 3463 | return len; |
| 3464 | } |
| 3465 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3466 | __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] | 3467 | |
| 3468 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3469 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3470 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3471 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3472 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3473 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3474 | else if (mddev->clevel[0]) |
| 3475 | return sprintf(page, "%s\n", mddev->clevel); |
| 3476 | else if (mddev->level != LEVEL_NONE) |
| 3477 | return sprintf(page, "%d\n", mddev->level); |
| 3478 | else |
| 3479 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3480 | } |
| 3481 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3482 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3483 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3484 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3485 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3486 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3487 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3488 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3489 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3490 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3491 | |
| 3492 | if (mddev->pers == NULL) { |
| 3493 | if (len == 0) |
| 3494 | return 0; |
| 3495 | if (len >= sizeof(mddev->clevel)) |
| 3496 | return -ENOSPC; |
| 3497 | strncpy(mddev->clevel, buf, len); |
| 3498 | if (mddev->clevel[len-1] == '\n') |
| 3499 | len--; |
| 3500 | mddev->clevel[len] = 0; |
| 3501 | mddev->level = LEVEL_NONE; |
| 3502 | return rv; |
| 3503 | } |
| 3504 | |
| 3505 | /* request to change the personality. Need to ensure: |
| 3506 | * - array is not engaged in resync/recovery/reshape |
| 3507 | * - old personality can be suspended |
| 3508 | * - new personality will access other array. |
| 3509 | */ |
| 3510 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3511 | if (mddev->sync_thread || |
| 3512 | mddev->reshape_position != MaxSector || |
| 3513 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3514 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3515 | |
| 3516 | if (!mddev->pers->quiesce) { |
| 3517 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3518 | mdname(mddev), mddev->pers->name); |
| 3519 | return -EINVAL; |
| 3520 | } |
| 3521 | |
| 3522 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3523 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3524 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3525 | strncpy(clevel, buf, len); |
| 3526 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3527 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3528 | clevel[len] = 0; |
| 3529 | if (strict_strtol(clevel, 10, &level)) |
| 3530 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3531 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3532 | if (request_module("md-%s", clevel) != 0) |
| 3533 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3534 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3535 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3536 | if (!pers || !try_module_get(pers->owner)) { |
| 3537 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3538 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3539 | return -EINVAL; |
| 3540 | } |
| 3541 | spin_unlock(&pers_lock); |
| 3542 | |
| 3543 | if (pers == mddev->pers) { |
| 3544 | /* Nothing to do! */ |
| 3545 | module_put(pers->owner); |
| 3546 | return rv; |
| 3547 | } |
| 3548 | if (!pers->takeover) { |
| 3549 | module_put(pers->owner); |
| 3550 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3551 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3552 | return -EINVAL; |
| 3553 | } |
| 3554 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3555 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3556 | rdev->new_raid_disk = rdev->raid_disk; |
| 3557 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3558 | /* ->takeover must set new_* and/or delta_disks |
| 3559 | * if it succeeds, and may set them when it fails. |
| 3560 | */ |
| 3561 | priv = pers->takeover(mddev); |
| 3562 | if (IS_ERR(priv)) { |
| 3563 | mddev->new_level = mddev->level; |
| 3564 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3565 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3566 | mddev->raid_disks -= mddev->delta_disks; |
| 3567 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3568 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3569 | module_put(pers->owner); |
| 3570 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3571 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3572 | return PTR_ERR(priv); |
| 3573 | } |
| 3574 | |
| 3575 | /* Looks like we have a winner */ |
| 3576 | mddev_suspend(mddev); |
| 3577 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3578 | |
| 3579 | if (mddev->pers->sync_request == NULL && |
| 3580 | pers->sync_request != NULL) { |
| 3581 | /* need to add the md_redundancy_group */ |
| 3582 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3583 | printk(KERN_WARNING |
| 3584 | "md: cannot register extra attributes for %s\n", |
| 3585 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3586 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3587 | } |
| 3588 | if (mddev->pers->sync_request != NULL && |
| 3589 | pers->sync_request == NULL) { |
| 3590 | /* need to remove the md_redundancy_group */ |
| 3591 | if (mddev->to_remove == NULL) |
| 3592 | mddev->to_remove = &md_redundancy_group; |
| 3593 | } |
| 3594 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3595 | if (mddev->pers->sync_request == NULL && |
| 3596 | mddev->external) { |
| 3597 | /* We are converting from a no-redundancy array |
| 3598 | * to a redundancy array and metadata is managed |
| 3599 | * externally so we need to be sure that writes |
| 3600 | * won't block due to a need to transition |
| 3601 | * clean->dirty |
| 3602 | * until external management is started. |
| 3603 | */ |
| 3604 | mddev->in_sync = 0; |
| 3605 | mddev->safemode_delay = 0; |
| 3606 | mddev->safemode = 0; |
| 3607 | } |
| 3608 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3609 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3610 | if (rdev->raid_disk < 0) |
| 3611 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3612 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3613 | rdev->new_raid_disk = -1; |
| 3614 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3615 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3616 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3617 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3618 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3619 | if (rdev->raid_disk < 0) |
| 3620 | continue; |
| 3621 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3622 | continue; |
| 3623 | rdev->raid_disk = rdev->new_raid_disk; |
| 3624 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3625 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3626 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3627 | if (sysfs_link_rdev(mddev, rdev)) |
| 3628 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3629 | " for %s after level change\n", |
| 3630 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3631 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3635 | mddev->pers = pers; |
| 3636 | mddev->private = priv; |
| 3637 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3638 | mddev->level = mddev->new_level; |
| 3639 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3640 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3641 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3642 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3643 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3644 | if (mddev->pers->sync_request == NULL) { |
| 3645 | /* this is now an array without redundancy, so |
| 3646 | * it must always be in_sync |
| 3647 | */ |
| 3648 | mddev->in_sync = 1; |
| 3649 | del_timer_sync(&mddev->safemode_timer); |
| 3650 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3651 | pers->run(mddev); |
| 3652 | mddev_resume(mddev); |
| 3653 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3654 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3655 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3656 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3657 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3658 | return rv; |
| 3659 | } |
| 3660 | |
| 3661 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3662 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3663 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3664 | |
| 3665 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3666 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3667 | { |
| 3668 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3669 | if (mddev->reshape_position != MaxSector && |
| 3670 | mddev->layout != mddev->new_layout) |
| 3671 | return sprintf(page, "%d (%d)\n", |
| 3672 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3673 | return sprintf(page, "%d\n", mddev->layout); |
| 3674 | } |
| 3675 | |
| 3676 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3677 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3678 | { |
| 3679 | char *e; |
| 3680 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3681 | |
| 3682 | if (!*buf || (*e && *e != '\n')) |
| 3683 | return -EINVAL; |
| 3684 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3685 | if (mddev->pers) { |
| 3686 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3687 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3688 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3689 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3690 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3691 | if (err) { |
| 3692 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3693 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3694 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3695 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3696 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3697 | if (mddev->reshape_position == MaxSector) |
| 3698 | mddev->layout = n; |
| 3699 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3700 | return len; |
| 3701 | } |
| 3702 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3703 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3704 | |
| 3705 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3706 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3707 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3708 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3709 | if (mddev->raid_disks == 0) |
| 3710 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3711 | if (mddev->reshape_position != MaxSector && |
| 3712 | mddev->delta_disks != 0) |
| 3713 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3714 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3715 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3716 | } |
| 3717 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3718 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3719 | |
| 3720 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3721 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3722 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3723 | char *e; |
| 3724 | int rv = 0; |
| 3725 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3726 | |
| 3727 | if (!*buf || (*e && *e != '\n')) |
| 3728 | return -EINVAL; |
| 3729 | |
| 3730 | if (mddev->pers) |
| 3731 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3732 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3733 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3734 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3735 | |
| 3736 | rdev_for_each(rdev, mddev) { |
| 3737 | if (olddisks < n && |
| 3738 | rdev->data_offset < rdev->new_data_offset) |
| 3739 | return -EINVAL; |
| 3740 | if (olddisks > n && |
| 3741 | rdev->data_offset > rdev->new_data_offset) |
| 3742 | return -EINVAL; |
| 3743 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3744 | mddev->delta_disks = n - olddisks; |
| 3745 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3746 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3747 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3748 | mddev->raid_disks = n; |
| 3749 | return rv ? rv : len; |
| 3750 | } |
| 3751 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3752 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3753 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3754 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3755 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3756 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3757 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3758 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3759 | return sprintf(page, "%d (%d)\n", |
| 3760 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3761 | mddev->chunk_sectors << 9); |
| 3762 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3766 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3768 | char *e; |
| 3769 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3770 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3771 | if (!*buf || (*e && *e != '\n')) |
| 3772 | return -EINVAL; |
| 3773 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3774 | if (mddev->pers) { |
| 3775 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3776 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3777 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3778 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3779 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3780 | if (err) { |
| 3781 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3782 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3783 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3784 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3785 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3786 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3787 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3788 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3789 | return len; |
| 3790 | } |
| 3791 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3792 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3793 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3794 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3795 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3796 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3797 | if (mddev->recovery_cp == MaxSector) |
| 3798 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3799 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3800 | } |
| 3801 | |
| 3802 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3803 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3804 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3805 | char *e; |
| 3806 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3807 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3808 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3809 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3810 | if (cmd_match(buf, "none")) |
| 3811 | n = MaxSector; |
| 3812 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3813 | return -EINVAL; |
| 3814 | |
| 3815 | mddev->recovery_cp = n; |
| 3816 | return len; |
| 3817 | } |
| 3818 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3819 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3820 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3821 | /* |
| 3822 | * The array state can be: |
| 3823 | * |
| 3824 | * clear |
| 3825 | * No devices, no size, no level |
| 3826 | * Equivalent to STOP_ARRAY ioctl |
| 3827 | * inactive |
| 3828 | * May have some settings, but array is not active |
| 3829 | * all IO results in error |
| 3830 | * When written, doesn't tear down array, but just stops it |
| 3831 | * suspended (not supported yet) |
| 3832 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3833 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3834 | * readonly |
| 3835 | * no resync can happen. no superblocks get written. |
| 3836 | * write requests fail |
| 3837 | * read-auto |
| 3838 | * like readonly, but behaves like 'clean' on a write request. |
| 3839 | * |
| 3840 | * clean - no pending writes, but otherwise active. |
| 3841 | * When written to inactive array, starts without resync |
| 3842 | * If a write request arrives then |
| 3843 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3844 | * if not known, block and switch to write-pending |
| 3845 | * If written to an active array that has pending writes, then fails. |
| 3846 | * active |
| 3847 | * fully active: IO and resync can be happening. |
| 3848 | * When written to inactive array, starts with resync |
| 3849 | * |
| 3850 | * write-pending |
| 3851 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3852 | * |
| 3853 | * active-idle |
| 3854 | * like active, but no writes have been seen for a while (100msec). |
| 3855 | * |
| 3856 | */ |
| 3857 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3858 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3859 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3860 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3861 | "write-pending", "active-idle", NULL }; |
| 3862 | |
| 3863 | static int match_word(const char *word, char **list) |
| 3864 | { |
| 3865 | int n; |
| 3866 | for (n=0; list[n]; n++) |
| 3867 | if (cmd_match(word, list[n])) |
| 3868 | break; |
| 3869 | return n; |
| 3870 | } |
| 3871 | |
| 3872 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3873 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3874 | { |
| 3875 | enum array_state st = inactive; |
| 3876 | |
| 3877 | if (mddev->pers) |
| 3878 | switch(mddev->ro) { |
| 3879 | case 1: |
| 3880 | st = readonly; |
| 3881 | break; |
| 3882 | case 2: |
| 3883 | st = read_auto; |
| 3884 | break; |
| 3885 | case 0: |
| 3886 | if (mddev->in_sync) |
| 3887 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3888 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3889 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3890 | else if (mddev->safemode) |
| 3891 | st = active_idle; |
| 3892 | else |
| 3893 | st = active; |
| 3894 | } |
| 3895 | else { |
| 3896 | if (list_empty(&mddev->disks) && |
| 3897 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3898 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3899 | st = clear; |
| 3900 | else |
| 3901 | st = inactive; |
| 3902 | } |
| 3903 | return sprintf(page, "%s\n", array_states[st]); |
| 3904 | } |
| 3905 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3906 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3907 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3908 | static int do_md_run(struct mddev * mddev); |
| 3909 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3910 | |
| 3911 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3912 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3913 | { |
| 3914 | int err = -EINVAL; |
| 3915 | enum array_state st = match_word(buf, array_states); |
| 3916 | switch(st) { |
| 3917 | case bad_word: |
| 3918 | break; |
| 3919 | case clear: |
| 3920 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3921 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3922 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3923 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3924 | break; |
| 3925 | case inactive: |
| 3926 | /* stopping an active array */ |
| 3927 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3928 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3929 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3930 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3931 | } else |
| 3932 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3933 | break; |
| 3934 | case suspended: |
| 3935 | break; /* not supported yet */ |
| 3936 | case readonly: |
| 3937 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3938 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3939 | else { |
| 3940 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3941 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3942 | err = do_md_run(mddev); |
| 3943 | } |
| 3944 | break; |
| 3945 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3946 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3947 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3948 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3949 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3950 | err = restart_array(mddev); |
| 3951 | if (err == 0) { |
| 3952 | mddev->ro = 2; |
| 3953 | set_disk_ro(mddev->gendisk, 0); |
| 3954 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3955 | } else { |
| 3956 | mddev->ro = 2; |
| 3957 | err = do_md_run(mddev); |
| 3958 | } |
| 3959 | break; |
| 3960 | case clean: |
| 3961 | if (mddev->pers) { |
| 3962 | restart_array(mddev); |
| 3963 | spin_lock_irq(&mddev->write_lock); |
| 3964 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3965 | if (mddev->in_sync == 0) { |
| 3966 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3967 | if (mddev->safemode == 1) |
| 3968 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3969 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3970 | } |
| 3971 | err = 0; |
| 3972 | } else |
| 3973 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3974 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3975 | } else |
| 3976 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | break; |
| 3978 | case active: |
| 3979 | if (mddev->pers) { |
| 3980 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3981 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3982 | wake_up(&mddev->sb_wait); |
| 3983 | err = 0; |
| 3984 | } else { |
| 3985 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3986 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3987 | err = do_md_run(mddev); |
| 3988 | } |
| 3989 | break; |
| 3990 | case write_pending: |
| 3991 | case active_idle: |
| 3992 | /* these cannot be set */ |
| 3993 | break; |
| 3994 | } |
| 3995 | if (err) |
| 3996 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3997 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3998 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3999 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4000 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4001 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4002 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4003 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4004 | static struct md_sysfs_entry md_array_state = |
| 4005 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4006 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4007 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4008 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4009 | return sprintf(page, "%d\n", |
| 4010 | atomic_read(&mddev->max_corr_read_errors)); |
| 4011 | } |
| 4012 | |
| 4013 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4014 | 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] | 4015 | { |
| 4016 | char *e; |
| 4017 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 4018 | |
| 4019 | if (*buf && (*e == 0 || *e == '\n')) { |
| 4020 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4021 | return len; |
| 4022 | } |
| 4023 | return -EINVAL; |
| 4024 | } |
| 4025 | |
| 4026 | static struct md_sysfs_entry max_corr_read_errors = |
| 4027 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4028 | max_corrected_read_errors_store); |
| 4029 | |
| 4030 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4031 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4032 | { |
| 4033 | return -EINVAL; |
| 4034 | } |
| 4035 | |
| 4036 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4037 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4038 | { |
| 4039 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4040 | /* The new device is added to the array. |
| 4041 | * If the array has a persistent superblock, we read the |
| 4042 | * superblock to initialise info and check validity. |
| 4043 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4044 | * which mainly checks size. |
| 4045 | */ |
| 4046 | char *e; |
| 4047 | int major = simple_strtoul(buf, &e, 10); |
| 4048 | int minor; |
| 4049 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4050 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4051 | int err; |
| 4052 | |
| 4053 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4054 | return -EINVAL; |
| 4055 | minor = simple_strtoul(e+1, &e, 10); |
| 4056 | if (*e && *e != '\n') |
| 4057 | return -EINVAL; |
| 4058 | dev = MKDEV(major, minor); |
| 4059 | if (major != MAJOR(dev) || |
| 4060 | minor != MINOR(dev)) |
| 4061 | return -EOVERFLOW; |
| 4062 | |
| 4063 | |
| 4064 | if (mddev->persistent) { |
| 4065 | rdev = md_import_device(dev, mddev->major_version, |
| 4066 | mddev->minor_version); |
| 4067 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4068 | struct md_rdev *rdev0 |
| 4069 | = list_entry(mddev->disks.next, |
| 4070 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4071 | err = super_types[mddev->major_version] |
| 4072 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4073 | if (err < 0) |
| 4074 | goto out; |
| 4075 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4076 | } else if (mddev->external) |
| 4077 | rdev = md_import_device(dev, -2, -1); |
| 4078 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4079 | rdev = md_import_device(dev, -1, -1); |
| 4080 | |
| 4081 | if (IS_ERR(rdev)) |
| 4082 | return PTR_ERR(rdev); |
| 4083 | err = bind_rdev_to_array(rdev, mddev); |
| 4084 | out: |
| 4085 | if (err) |
| 4086 | export_rdev(rdev); |
| 4087 | return err ? err : len; |
| 4088 | } |
| 4089 | |
| 4090 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4091 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4092 | |
| 4093 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4094 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4095 | { |
| 4096 | char *end; |
| 4097 | unsigned long chunk, end_chunk; |
| 4098 | |
| 4099 | if (!mddev->bitmap) |
| 4100 | goto out; |
| 4101 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4102 | while (*buf) { |
| 4103 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4104 | if (buf == end) break; |
| 4105 | if (*end == '-') { /* range */ |
| 4106 | buf = end + 1; |
| 4107 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4108 | if (buf == end) break; |
| 4109 | } |
| 4110 | if (*end && !isspace(*end)) break; |
| 4111 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4112 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4113 | } |
| 4114 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4115 | out: |
| 4116 | return len; |
| 4117 | } |
| 4118 | |
| 4119 | static struct md_sysfs_entry md_bitmap = |
| 4120 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4121 | |
| 4122 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4123 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4124 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4125 | return sprintf(page, "%llu\n", |
| 4126 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4129 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4130 | |
| 4131 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4132 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4133 | { |
| 4134 | /* If array is inactive, we can reduce the component size, but |
| 4135 | * not increase it (except from 0). |
| 4136 | * If array is active, we can try an on-line resize |
| 4137 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4138 | sector_t sectors; |
| 4139 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4140 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4141 | if (err < 0) |
| 4142 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4143 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4144 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4145 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4146 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4147 | if (mddev->dev_sectors == 0 || |
| 4148 | mddev->dev_sectors > sectors) |
| 4149 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4150 | else |
| 4151 | err = -ENOSPC; |
| 4152 | } |
| 4153 | return err ? err : len; |
| 4154 | } |
| 4155 | |
| 4156 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4157 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4158 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4159 | |
| 4160 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4161 | * This is one of |
| 4162 | * 'none' for arrays with no metadata (good luck...) |
| 4163 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4164 | * or N.M for internally known formats |
| 4165 | */ |
| 4166 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4167 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4168 | { |
| 4169 | if (mddev->persistent) |
| 4170 | return sprintf(page, "%d.%d\n", |
| 4171 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4172 | else if (mddev->external) |
| 4173 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4174 | else |
| 4175 | return sprintf(page, "none\n"); |
| 4176 | } |
| 4177 | |
| 4178 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4179 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4180 | { |
| 4181 | int major, minor; |
| 4182 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4183 | /* Changing the details of 'external' metadata is |
| 4184 | * always permitted. Otherwise there must be |
| 4185 | * no devices attached to the array. |
| 4186 | */ |
| 4187 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4188 | ; |
| 4189 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4190 | return -EBUSY; |
| 4191 | |
| 4192 | if (cmd_match(buf, "none")) { |
| 4193 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4194 | mddev->external = 0; |
| 4195 | mddev->major_version = 0; |
| 4196 | mddev->minor_version = 90; |
| 4197 | return len; |
| 4198 | } |
| 4199 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4200 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4201 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4202 | namelen = sizeof(mddev->metadata_type)-1; |
| 4203 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4204 | mddev->metadata_type[namelen] = 0; |
| 4205 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4206 | mddev->metadata_type[--namelen] = 0; |
| 4207 | mddev->persistent = 0; |
| 4208 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4209 | mddev->major_version = 0; |
| 4210 | mddev->minor_version = 90; |
| 4211 | return len; |
| 4212 | } |
| 4213 | major = simple_strtoul(buf, &e, 10); |
| 4214 | if (e==buf || *e != '.') |
| 4215 | return -EINVAL; |
| 4216 | buf = e+1; |
| 4217 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4218 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4219 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4220 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4221 | return -ENOENT; |
| 4222 | mddev->major_version = major; |
| 4223 | mddev->minor_version = minor; |
| 4224 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4225 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4226 | return len; |
| 4227 | } |
| 4228 | |
| 4229 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4230 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4231 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4232 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4233 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4234 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4235 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4236 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4237 | type = "frozen"; |
| 4238 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4239 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4240 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4241 | type = "reshape"; |
| 4242 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4243 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4244 | type = "resync"; |
| 4245 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4246 | type = "check"; |
| 4247 | else |
| 4248 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4249 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4250 | type = "recover"; |
| 4251 | } |
| 4252 | return sprintf(page, "%s\n", type); |
| 4253 | } |
| 4254 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4255 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4256 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4257 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4258 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4259 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4260 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4261 | return -EINVAL; |
| 4262 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4263 | if (cmd_match(page, "frozen")) |
| 4264 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4265 | else |
| 4266 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4267 | |
| 4268 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4269 | if (mddev->sync_thread) { |
| 4270 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4271 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4272 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4273 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4274 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4275 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4276 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4277 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4278 | else if (cmd_match(page, "recover")) { |
| 4279 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4280 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4281 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4282 | int err; |
| 4283 | if (mddev->pers->start_reshape == NULL) |
| 4284 | return -EINVAL; |
| 4285 | err = mddev->pers->start_reshape(mddev); |
| 4286 | if (err) |
| 4287 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4288 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4289 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4290 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4291 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4292 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4293 | return -EINVAL; |
| 4294 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4295 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4296 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4297 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4298 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4299 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4300 | return len; |
| 4301 | } |
| 4302 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4303 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4304 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4305 | { |
| 4306 | return sprintf(page, "%llu\n", |
| 4307 | (unsigned long long) mddev->resync_mismatches); |
| 4308 | } |
| 4309 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4310 | static struct md_sysfs_entry md_scan_mode = |
| 4311 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4312 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4313 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4314 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4315 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4316 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4317 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4318 | { |
| 4319 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4320 | mddev->sync_speed_min ? "local": "system"); |
| 4321 | } |
| 4322 | |
| 4323 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4324 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4325 | { |
| 4326 | int min; |
| 4327 | char *e; |
| 4328 | if (strncmp(buf, "system", 6)==0) { |
| 4329 | mddev->sync_speed_min = 0; |
| 4330 | return len; |
| 4331 | } |
| 4332 | min = simple_strtoul(buf, &e, 10); |
| 4333 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4334 | return -EINVAL; |
| 4335 | mddev->sync_speed_min = min; |
| 4336 | return len; |
| 4337 | } |
| 4338 | |
| 4339 | static struct md_sysfs_entry md_sync_min = |
| 4340 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4341 | |
| 4342 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4343 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4344 | { |
| 4345 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4346 | mddev->sync_speed_max ? "local": "system"); |
| 4347 | } |
| 4348 | |
| 4349 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4350 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4351 | { |
| 4352 | int max; |
| 4353 | char *e; |
| 4354 | if (strncmp(buf, "system", 6)==0) { |
| 4355 | mddev->sync_speed_max = 0; |
| 4356 | return len; |
| 4357 | } |
| 4358 | max = simple_strtoul(buf, &e, 10); |
| 4359 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4360 | return -EINVAL; |
| 4361 | mddev->sync_speed_max = max; |
| 4362 | return len; |
| 4363 | } |
| 4364 | |
| 4365 | static struct md_sysfs_entry md_sync_max = |
| 4366 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4367 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4368 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4369 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4370 | { |
| 4371 | return sprintf(page, "%d\n", mddev->degraded); |
| 4372 | } |
| 4373 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4374 | |
| 4375 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4376 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4377 | { |
| 4378 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4379 | } |
| 4380 | |
| 4381 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4382 | 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] | 4383 | { |
| 4384 | long n; |
| 4385 | |
| 4386 | if (strict_strtol(buf, 10, &n)) |
| 4387 | return -EINVAL; |
| 4388 | |
| 4389 | if (n != 0 && n != 1) |
| 4390 | return -EINVAL; |
| 4391 | |
| 4392 | mddev->parallel_resync = n; |
| 4393 | |
| 4394 | if (mddev->sync_thread) |
| 4395 | wake_up(&resync_wait); |
| 4396 | |
| 4397 | return len; |
| 4398 | } |
| 4399 | |
| 4400 | /* force parallel resync, even with shared block devices */ |
| 4401 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4402 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4403 | sync_force_parallel_show, sync_force_parallel_store); |
| 4404 | |
| 4405 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4406 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4407 | { |
| 4408 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4409 | if (mddev->curr_resync == 0) |
| 4410 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4411 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4412 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4413 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4414 | db = resync - mddev->resync_mark_cnt; |
| 4415 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4416 | } |
| 4417 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4418 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4419 | |
| 4420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4421 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4422 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4423 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4424 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4425 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4426 | return sprintf(page, "none\n"); |
| 4427 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4428 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4429 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4430 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4431 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4432 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4433 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4434 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4435 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4436 | } |
| 4437 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4438 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4439 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4440 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4441 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4442 | { |
| 4443 | return sprintf(page, "%llu\n", |
| 4444 | (unsigned long long)mddev->resync_min); |
| 4445 | } |
| 4446 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4447 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4448 | { |
| 4449 | unsigned long long min; |
| 4450 | if (strict_strtoull(buf, 10, &min)) |
| 4451 | return -EINVAL; |
| 4452 | if (min > mddev->resync_max) |
| 4453 | return -EINVAL; |
| 4454 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4455 | return -EBUSY; |
| 4456 | |
| 4457 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4458 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4459 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4460 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4461 | return -EINVAL; |
| 4462 | } |
| 4463 | mddev->resync_min = min; |
| 4464 | |
| 4465 | return len; |
| 4466 | } |
| 4467 | |
| 4468 | static struct md_sysfs_entry md_min_sync = |
| 4469 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4470 | |
| 4471 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4472 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4473 | { |
| 4474 | if (mddev->resync_max == MaxSector) |
| 4475 | return sprintf(page, "max\n"); |
| 4476 | else |
| 4477 | return sprintf(page, "%llu\n", |
| 4478 | (unsigned long long)mddev->resync_max); |
| 4479 | } |
| 4480 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4481 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4482 | { |
| 4483 | if (strncmp(buf, "max", 3) == 0) |
| 4484 | mddev->resync_max = MaxSector; |
| 4485 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4486 | unsigned long long max; |
| 4487 | if (strict_strtoull(buf, 10, &max)) |
| 4488 | return -EINVAL; |
| 4489 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4490 | return -EINVAL; |
| 4491 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4492 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4493 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4494 | return -EBUSY; |
| 4495 | |
| 4496 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4497 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4498 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4499 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4500 | return -EINVAL; |
| 4501 | } |
| 4502 | mddev->resync_max = max; |
| 4503 | } |
| 4504 | wake_up(&mddev->recovery_wait); |
| 4505 | return len; |
| 4506 | } |
| 4507 | |
| 4508 | static struct md_sysfs_entry md_max_sync = |
| 4509 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4510 | |
| 4511 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4512 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4513 | { |
| 4514 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4515 | } |
| 4516 | |
| 4517 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4518 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4519 | { |
| 4520 | char *e; |
| 4521 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4522 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4523 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4524 | if (mddev->pers == NULL || |
| 4525 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4526 | return -EINVAL; |
| 4527 | if (buf == e || (*e && *e != '\n')) |
| 4528 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4529 | |
| 4530 | mddev->suspend_lo = new; |
| 4531 | if (new >= old) |
| 4532 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4533 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4534 | else { |
| 4535 | /* Expanding suspended region - need to wait */ |
| 4536 | mddev->pers->quiesce(mddev, 1); |
| 4537 | mddev->pers->quiesce(mddev, 0); |
| 4538 | } |
| 4539 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4540 | } |
| 4541 | static struct md_sysfs_entry md_suspend_lo = |
| 4542 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4543 | |
| 4544 | |
| 4545 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4546 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4547 | { |
| 4548 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4549 | } |
| 4550 | |
| 4551 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4552 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4553 | { |
| 4554 | char *e; |
| 4555 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4556 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4557 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4558 | if (mddev->pers == NULL || |
| 4559 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4560 | return -EINVAL; |
| 4561 | if (buf == e || (*e && *e != '\n')) |
| 4562 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4563 | |
| 4564 | mddev->suspend_hi = new; |
| 4565 | if (new <= old) |
| 4566 | /* Shrinking suspended region */ |
| 4567 | mddev->pers->quiesce(mddev, 2); |
| 4568 | else { |
| 4569 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4570 | mddev->pers->quiesce(mddev, 1); |
| 4571 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4572 | } |
| 4573 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4574 | } |
| 4575 | static struct md_sysfs_entry md_suspend_hi = |
| 4576 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4577 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4578 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4579 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4580 | { |
| 4581 | if (mddev->reshape_position != MaxSector) |
| 4582 | return sprintf(page, "%llu\n", |
| 4583 | (unsigned long long)mddev->reshape_position); |
| 4584 | strcpy(page, "none\n"); |
| 4585 | return 5; |
| 4586 | } |
| 4587 | |
| 4588 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4589 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4590 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4591 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4592 | char *e; |
| 4593 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4594 | if (mddev->pers) |
| 4595 | return -EBUSY; |
| 4596 | if (buf == e || (*e && *e != '\n')) |
| 4597 | return -EINVAL; |
| 4598 | mddev->reshape_position = new; |
| 4599 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4600 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4601 | mddev->new_level = mddev->level; |
| 4602 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4603 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4604 | rdev_for_each(rdev, mddev) |
| 4605 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4606 | return len; |
| 4607 | } |
| 4608 | |
| 4609 | static struct md_sysfs_entry md_reshape_position = |
| 4610 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4611 | reshape_position_store); |
| 4612 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4613 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4614 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4615 | { |
| 4616 | return sprintf(page, "%s\n", |
| 4617 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4618 | } |
| 4619 | |
| 4620 | static ssize_t |
| 4621 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4622 | { |
| 4623 | int backwards = 0; |
| 4624 | if (cmd_match(buf, "forwards")) |
| 4625 | backwards = 0; |
| 4626 | else if (cmd_match(buf, "backwards")) |
| 4627 | backwards = 1; |
| 4628 | else |
| 4629 | return -EINVAL; |
| 4630 | if (mddev->reshape_backwards == backwards) |
| 4631 | return len; |
| 4632 | |
| 4633 | /* check if we are allowed to change */ |
| 4634 | if (mddev->delta_disks) |
| 4635 | return -EBUSY; |
| 4636 | |
| 4637 | if (mddev->persistent && |
| 4638 | mddev->major_version == 0) |
| 4639 | return -EINVAL; |
| 4640 | |
| 4641 | mddev->reshape_backwards = backwards; |
| 4642 | return len; |
| 4643 | } |
| 4644 | |
| 4645 | static struct md_sysfs_entry md_reshape_direction = |
| 4646 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4647 | reshape_direction_store); |
| 4648 | |
| 4649 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4650 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4651 | { |
| 4652 | if (mddev->external_size) |
| 4653 | return sprintf(page, "%llu\n", |
| 4654 | (unsigned long long)mddev->array_sectors/2); |
| 4655 | else |
| 4656 | return sprintf(page, "default\n"); |
| 4657 | } |
| 4658 | |
| 4659 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4660 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4661 | { |
| 4662 | sector_t sectors; |
| 4663 | |
| 4664 | if (strncmp(buf, "default", 7) == 0) { |
| 4665 | if (mddev->pers) |
| 4666 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4667 | else |
| 4668 | sectors = mddev->array_sectors; |
| 4669 | |
| 4670 | mddev->external_size = 0; |
| 4671 | } else { |
| 4672 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4673 | return -EINVAL; |
| 4674 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4675 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4676 | |
| 4677 | mddev->external_size = 1; |
| 4678 | } |
| 4679 | |
| 4680 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4681 | if (mddev->pers) { |
| 4682 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4683 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4684 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4685 | return len; |
| 4686 | } |
| 4687 | |
| 4688 | static struct md_sysfs_entry md_array_size = |
| 4689 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4690 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4691 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4692 | static struct attribute *md_default_attrs[] = { |
| 4693 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4694 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4695 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4696 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4697 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4698 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4699 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4700 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4701 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4702 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4703 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4704 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4705 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4706 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4707 | NULL, |
| 4708 | }; |
| 4709 | |
| 4710 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4711 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4712 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4713 | &md_sync_min.attr, |
| 4714 | &md_sync_max.attr, |
| 4715 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4716 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4717 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4718 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4719 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4720 | &md_suspend_lo.attr, |
| 4721 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4722 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4723 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4724 | NULL, |
| 4725 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4726 | static struct attribute_group md_redundancy_group = { |
| 4727 | .name = NULL, |
| 4728 | .attrs = md_redundancy_attrs, |
| 4729 | }; |
| 4730 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4731 | |
| 4732 | static ssize_t |
| 4733 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4734 | { |
| 4735 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4736 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4737 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4738 | |
| 4739 | if (!entry->show) |
| 4740 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4741 | spin_lock(&all_mddevs_lock); |
| 4742 | if (list_empty(&mddev->all_mddevs)) { |
| 4743 | spin_unlock(&all_mddevs_lock); |
| 4744 | return -EBUSY; |
| 4745 | } |
| 4746 | mddev_get(mddev); |
| 4747 | spin_unlock(&all_mddevs_lock); |
| 4748 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4749 | rv = mddev_lock(mddev); |
| 4750 | if (!rv) { |
| 4751 | rv = entry->show(mddev, page); |
| 4752 | mddev_unlock(mddev); |
| 4753 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4754 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4755 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | static ssize_t |
| 4759 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4760 | const char *page, size_t length) |
| 4761 | { |
| 4762 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4763 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4764 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4765 | |
| 4766 | if (!entry->store) |
| 4767 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4768 | if (!capable(CAP_SYS_ADMIN)) |
| 4769 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4770 | spin_lock(&all_mddevs_lock); |
| 4771 | if (list_empty(&mddev->all_mddevs)) { |
| 4772 | spin_unlock(&all_mddevs_lock); |
| 4773 | return -EBUSY; |
| 4774 | } |
| 4775 | mddev_get(mddev); |
| 4776 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4777 | rv = mddev_lock(mddev); |
| 4778 | if (!rv) { |
| 4779 | rv = entry->store(mddev, page, length); |
| 4780 | mddev_unlock(mddev); |
| 4781 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4782 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4783 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | static void md_free(struct kobject *ko) |
| 4787 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4788 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4789 | |
| 4790 | if (mddev->sysfs_state) |
| 4791 | sysfs_put(mddev->sysfs_state); |
| 4792 | |
| 4793 | if (mddev->gendisk) { |
| 4794 | del_gendisk(mddev->gendisk); |
| 4795 | put_disk(mddev->gendisk); |
| 4796 | } |
| 4797 | if (mddev->queue) |
| 4798 | blk_cleanup_queue(mddev->queue); |
| 4799 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4800 | kfree(mddev); |
| 4801 | } |
| 4802 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4803 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4804 | .show = md_attr_show, |
| 4805 | .store = md_attr_store, |
| 4806 | }; |
| 4807 | static struct kobj_type md_ktype = { |
| 4808 | .release = md_free, |
| 4809 | .sysfs_ops = &md_sysfs_ops, |
| 4810 | .default_attrs = md_default_attrs, |
| 4811 | }; |
| 4812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | int mdp_major = 0; |
| 4814 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4815 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4816 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4817 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4818 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4819 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4820 | kobject_del(&mddev->kobj); |
| 4821 | kobject_put(&mddev->kobj); |
| 4822 | } |
| 4823 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4824 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4826 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4827 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4829 | int partitioned; |
| 4830 | int shift; |
| 4831 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4832 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
| 4834 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4835 | return -ENODEV; |
| 4836 | |
| 4837 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4838 | shift = partitioned ? MdpMinorShift : 0; |
| 4839 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4841 | /* wait for any previous instance of this device to be |
| 4842 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4843 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4844 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4845 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4846 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4847 | error = -EEXIST; |
| 4848 | if (mddev->gendisk) |
| 4849 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4850 | |
| 4851 | if (name) { |
| 4852 | /* Need to ensure that 'name' is not a duplicate. |
| 4853 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4854 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4855 | spin_lock(&all_mddevs_lock); |
| 4856 | |
| 4857 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4858 | if (mddev2->gendisk && |
| 4859 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4860 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4861 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4862 | } |
| 4863 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4865 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4866 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4867 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4868 | if (!mddev->queue) |
| 4869 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4870 | mddev->queue->queuedata = mddev; |
| 4871 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4872 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4873 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | disk = alloc_disk(1 << shift); |
| 4876 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4877 | blk_cleanup_queue(mddev->queue); |
| 4878 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4879 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4881 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4883 | if (name) |
| 4884 | strcpy(disk->disk_name, name); |
| 4885 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4887 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | disk->fops = &md_fops; |
| 4890 | disk->private_data = mddev; |
| 4891 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4892 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4893 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4894 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4895 | * remove it now. |
| 4896 | */ |
| 4897 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4899 | /* As soon as we call add_disk(), another thread could get |
| 4900 | * through to md_open, so make sure it doesn't get too far |
| 4901 | */ |
| 4902 | mutex_lock(&mddev->open_mutex); |
| 4903 | add_disk(disk); |
| 4904 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4905 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4906 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4907 | if (error) { |
| 4908 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4909 | * __must_check, we must do something with the result |
| 4910 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4911 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4912 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4913 | error = 0; |
| 4914 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4915 | if (mddev->kobj.sd && |
| 4916 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4917 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4918 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4919 | abort: |
| 4920 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4921 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4922 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4923 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4924 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4925 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4926 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4930 | { |
| 4931 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | return NULL; |
| 4933 | } |
| 4934 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4935 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4936 | { |
| 4937 | /* val must be "md_*" where * is not all digits. |
| 4938 | * We allocate an array with a large free minor number, and |
| 4939 | * set the name to val. val must not already be an active name. |
| 4940 | */ |
| 4941 | int len = strlen(val); |
| 4942 | char buf[DISK_NAME_LEN]; |
| 4943 | |
| 4944 | while (len && val[len-1] == '\n') |
| 4945 | len--; |
| 4946 | if (len >= DISK_NAME_LEN) |
| 4947 | return -E2BIG; |
| 4948 | strlcpy(buf, val, len+1); |
| 4949 | if (strncmp(buf, "md_", 3) != 0) |
| 4950 | return -EINVAL; |
| 4951 | return md_alloc(0, buf); |
| 4952 | } |
| 4953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | static void md_safemode_timeout(unsigned long data) |
| 4955 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4956 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4958 | if (!atomic_read(&mddev->writes_pending)) { |
| 4959 | mddev->safemode = 1; |
| 4960 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4961 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | md_wakeup_thread(mddev->thread); |
| 4964 | } |
| 4965 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4966 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4968 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4970 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4971 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4972 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4974 | if (list_empty(&mddev->disks)) |
| 4975 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
| 4978 | if (mddev->pers) |
| 4979 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4980 | /* Cannot run until previous stop completes properly */ |
| 4981 | if (mddev->sysfs_active) |
| 4982 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | /* |
| 4985 | * Analyze all RAID superblock(s) |
| 4986 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4987 | if (!mddev->raid_disks) { |
| 4988 | if (!mddev->persistent) |
| 4989 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4990 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4993 | if (mddev->level != LEVEL_NONE) |
| 4994 | request_module("md-level-%d", mddev->level); |
| 4995 | else if (mddev->clevel[0]) |
| 4996 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | |
| 4998 | /* |
| 4999 | * Drop all container device buffers, from now on |
| 5000 | * the only valid external interface is through the md |
| 5001 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5003 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5004 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | continue; |
| 5006 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5007 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5008 | |
| 5009 | /* perform some consistency tests on the device. |
| 5010 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5011 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5012 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5013 | if (rdev->meta_bdev) { |
| 5014 | /* Nothing to check */; |
| 5015 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5016 | if (mddev->dev_sectors && |
| 5017 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5018 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5019 | printk("md: %s: data overlaps metadata\n", |
| 5020 | mdname(mddev)); |
| 5021 | return -EINVAL; |
| 5022 | } |
| 5023 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5024 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5025 | > rdev->data_offset) { |
| 5026 | printk("md: %s: metadata overlaps data\n", |
| 5027 | mdname(mddev)); |
| 5028 | return -EINVAL; |
| 5029 | } |
| 5030 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5031 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5034 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 5035 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5036 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5039 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5040 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5042 | if (mddev->level != LEVEL_NONE) |
| 5043 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5044 | mddev->level); |
| 5045 | else |
| 5046 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5047 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5048 | return -EINVAL; |
| 5049 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5050 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5052 | if (mddev->level != pers->level) { |
| 5053 | mddev->level = pers->level; |
| 5054 | mddev->new_level = pers->level; |
| 5055 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5056 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5058 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5059 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5060 | /* This personality cannot handle reshaping... */ |
| 5061 | mddev->pers = NULL; |
| 5062 | module_put(pers->owner); |
| 5063 | return -EINVAL; |
| 5064 | } |
| 5065 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5066 | if (pers->sync_request) { |
| 5067 | /* Warn if this is a potentially silly |
| 5068 | * configuration. |
| 5069 | */ |
| 5070 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5071 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5072 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5073 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5074 | rdev_for_each(rdev, mddev) |
| 5075 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5076 | if (rdev < rdev2 && |
| 5077 | rdev->bdev->bd_contains == |
| 5078 | rdev2->bdev->bd_contains) { |
| 5079 | printk(KERN_WARNING |
| 5080 | "%s: WARNING: %s appears to be" |
| 5081 | " on the same physical disk as" |
| 5082 | " %s.\n", |
| 5083 | mdname(mddev), |
| 5084 | bdevname(rdev->bdev,b), |
| 5085 | bdevname(rdev2->bdev,b2)); |
| 5086 | warned = 1; |
| 5087 | } |
| 5088 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5089 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5090 | if (warned) |
| 5091 | printk(KERN_WARNING |
| 5092 | "True protection against single-disk" |
| 5093 | " failure might be compromised.\n"); |
| 5094 | } |
| 5095 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5096 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5097 | /* may be over-ridden by personality */ |
| 5098 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5099 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5100 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5102 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5103 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5104 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5105 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5106 | if (err) |
| 5107 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5108 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5109 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5110 | " but 'external_size' not in effect?\n", __func__); |
| 5111 | printk(KERN_ERR |
| 5112 | "md: invalid array_size %llu > default size %llu\n", |
| 5113 | (unsigned long long)mddev->array_sectors / 2, |
| 5114 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5115 | err = -EINVAL; |
| 5116 | mddev->pers->stop(mddev); |
| 5117 | } |
| 5118 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5119 | err = bitmap_create(mddev); |
| 5120 | if (err) { |
| 5121 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5122 | mdname(mddev), err); |
| 5123 | mddev->pers->stop(mddev); |
| 5124 | } |
| 5125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | module_put(mddev->pers->owner); |
| 5128 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5129 | bitmap_destroy(mddev); |
| 5130 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5132 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5133 | if (mddev->kobj.sd && |
| 5134 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5135 | printk(KERN_WARNING |
| 5136 | "md: cannot register extra attributes for %s\n", |
| 5137 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5138 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5139 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5140 | mddev->ro = 0; |
| 5141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5143 | atomic_set(&mddev->max_corr_read_errors, |
| 5144 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | mddev->safemode = 0; |
| 5146 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5147 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5148 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5150 | smp_wmb(); |
| 5151 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5152 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5153 | if (rdev->raid_disk >= 0) |
| 5154 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5155 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | |
| 5157 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5158 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5159 | if (mddev->flags) |
| 5160 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5162 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5163 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5164 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5165 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | return 0; |
| 5167 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5168 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5170 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5171 | { |
| 5172 | int err; |
| 5173 | |
| 5174 | err = md_run(mddev); |
| 5175 | if (err) |
| 5176 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5177 | err = bitmap_load(mddev); |
| 5178 | if (err) { |
| 5179 | bitmap_destroy(mddev); |
| 5180 | goto out; |
| 5181 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5182 | |
| 5183 | md_wakeup_thread(mddev->thread); |
| 5184 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5185 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5186 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5187 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5188 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5189 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5190 | out: |
| 5191 | return err; |
| 5192 | } |
| 5193 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5194 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | { |
| 5196 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5198 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5200 | return -ENXIO; |
| 5201 | if (!mddev->pers) |
| 5202 | return -EINVAL; |
| 5203 | if (!mddev->ro) |
| 5204 | return -EBUSY; |
| 5205 | mddev->safemode = 0; |
| 5206 | mddev->ro = 0; |
| 5207 | set_disk_ro(disk, 0); |
| 5208 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5209 | mdname(mddev)); |
| 5210 | /* Kick recovery or resync if necessary */ |
| 5211 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5212 | md_wakeup_thread(mddev->thread); |
| 5213 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5214 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5215 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } |
| 5217 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5218 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5219 | * to the file ourselves */ |
| 5220 | static int deny_bitmap_write_access(struct file * file) |
| 5221 | { |
| 5222 | struct inode *inode = file->f_mapping->host; |
| 5223 | |
| 5224 | spin_lock(&inode->i_lock); |
| 5225 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5226 | spin_unlock(&inode->i_lock); |
| 5227 | return -ETXTBSY; |
| 5228 | } |
| 5229 | atomic_set(&inode->i_writecount, -1); |
| 5230 | spin_unlock(&inode->i_lock); |
| 5231 | |
| 5232 | return 0; |
| 5233 | } |
| 5234 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5235 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5236 | { |
| 5237 | struct inode *inode = file->f_mapping->host; |
| 5238 | |
| 5239 | spin_lock(&inode->i_lock); |
| 5240 | atomic_set(&inode->i_writecount, 1); |
| 5241 | spin_unlock(&inode->i_lock); |
| 5242 | } |
| 5243 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5244 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5245 | { |
| 5246 | mddev->array_sectors = 0; |
| 5247 | mddev->external_size = 0; |
| 5248 | mddev->dev_sectors = 0; |
| 5249 | mddev->raid_disks = 0; |
| 5250 | mddev->recovery_cp = 0; |
| 5251 | mddev->resync_min = 0; |
| 5252 | mddev->resync_max = MaxSector; |
| 5253 | mddev->reshape_position = MaxSector; |
| 5254 | mddev->external = 0; |
| 5255 | mddev->persistent = 0; |
| 5256 | mddev->level = LEVEL_NONE; |
| 5257 | mddev->clevel[0] = 0; |
| 5258 | mddev->flags = 0; |
| 5259 | mddev->ro = 0; |
| 5260 | mddev->metadata_type[0] = 0; |
| 5261 | mddev->chunk_sectors = 0; |
| 5262 | mddev->ctime = mddev->utime = 0; |
| 5263 | mddev->layout = 0; |
| 5264 | mddev->max_disks = 0; |
| 5265 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5266 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5267 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5268 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5269 | mddev->new_level = LEVEL_NONE; |
| 5270 | mddev->new_layout = 0; |
| 5271 | mddev->new_chunk_sectors = 0; |
| 5272 | mddev->curr_resync = 0; |
| 5273 | mddev->resync_mismatches = 0; |
| 5274 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5275 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5276 | mddev->recovery = 0; |
| 5277 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5278 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5279 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5280 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5281 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5282 | mddev->bitmap_info.offset = 0; |
| 5283 | mddev->bitmap_info.default_offset = 0; |
| 5284 | mddev->bitmap_info.chunksize = 0; |
| 5285 | mddev->bitmap_info.daemon_sleep = 0; |
| 5286 | mddev->bitmap_info.max_write_behind = 0; |
| 5287 | } |
| 5288 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5289 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5290 | { |
| 5291 | if (mddev->sync_thread) { |
| 5292 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5293 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5294 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | del_timer_sync(&mddev->safemode_timer); |
| 5298 | |
| 5299 | bitmap_flush(mddev); |
| 5300 | md_super_wait(mddev); |
| 5301 | |
| 5302 | if (!mddev->in_sync || mddev->flags) { |
| 5303 | /* mark array as shutdown cleanly */ |
| 5304 | mddev->in_sync = 1; |
| 5305 | md_update_sb(mddev, 1); |
| 5306 | } |
| 5307 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5308 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5309 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5310 | { |
| 5311 | mddev_lock(mddev); |
| 5312 | __md_stop_writes(mddev); |
| 5313 | mddev_unlock(mddev); |
| 5314 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5315 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5316 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5317 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5318 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5319 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5320 | mddev->pers->stop(mddev); |
| 5321 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5322 | mddev->to_remove = &md_redundancy_group; |
| 5323 | module_put(mddev->pers->owner); |
| 5324 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5325 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5326 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5327 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5328 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5329 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5330 | { |
| 5331 | int err = 0; |
| 5332 | mutex_lock(&mddev->open_mutex); |
| 5333 | if (atomic_read(&mddev->openers) > is_open) { |
| 5334 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5335 | err = -EBUSY; |
| 5336 | goto out; |
| 5337 | } |
| 5338 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5339 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5340 | |
| 5341 | err = -ENXIO; |
| 5342 | if (mddev->ro==1) |
| 5343 | goto out; |
| 5344 | mddev->ro = 1; |
| 5345 | set_disk_ro(mddev->gendisk, 1); |
| 5346 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5347 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5348 | err = 0; |
| 5349 | } |
| 5350 | out: |
| 5351 | mutex_unlock(&mddev->open_mutex); |
| 5352 | return err; |
| 5353 | } |
| 5354 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5355 | /* mode: |
| 5356 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5357 | * 2 - stop but do not disassemble array |
| 5358 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5359 | 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] | 5360 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5362 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5364 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5365 | if (atomic_read(&mddev->openers) > is_open || |
| 5366 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5367 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5368 | mutex_unlock(&mddev->open_mutex); |
| 5369 | return -EBUSY; |
| 5370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5372 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5373 | if (mddev->ro) |
| 5374 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5375 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5376 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5377 | md_stop(mddev); |
| 5378 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5379 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5380 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5381 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5382 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5383 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5384 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5385 | if (rdev->raid_disk >= 0) |
| 5386 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5387 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5388 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5389 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5390 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5391 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5392 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5393 | if (mddev->ro) |
| 5394 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5395 | } else |
| 5396 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | /* |
| 5398 | * Free resources if final stop |
| 5399 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5400 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5402 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5403 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5404 | if (mddev->bitmap_info.file) { |
| 5405 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5406 | fput(mddev->bitmap_info.file); |
| 5407 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5408 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5409 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | export_array(mddev); |
| 5412 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5413 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5414 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5415 | if (mddev->hold_active == UNTIL_STOP) |
| 5416 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5417 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5418 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5419 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5420 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5421 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | } |
| 5423 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5424 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5425 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5427 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | int err; |
| 5429 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5430 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | |
| 5433 | printk(KERN_INFO "md: running: "); |
| 5434 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5435 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | char b[BDEVNAME_SIZE]; |
| 5437 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5438 | } |
| 5439 | printk("\n"); |
| 5440 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5441 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | if (err) { |
| 5443 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5444 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | /* |
| 5449 | * lets try to run arrays based on all disks that have arrived |
| 5450 | * until now. (those are in pending_raid_disks) |
| 5451 | * |
| 5452 | * the method: pick the first pending disk, collect all disks with |
| 5453 | * the same UUID, remove all from the pending list and put them into |
| 5454 | * the 'same_array' list. Then order this list based on superblock |
| 5455 | * update time (freshest comes first), kick out 'old' disks and |
| 5456 | * compare superblocks. If everything's fine then run it. |
| 5457 | * |
| 5458 | * If "unit" is allocated, then bump its reference count |
| 5459 | */ |
| 5460 | static void autorun_devices(int part) |
| 5461 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5462 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5463 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | char b[BDEVNAME_SIZE]; |
| 5465 | |
| 5466 | printk(KERN_INFO "md: autorun ...\n"); |
| 5467 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5468 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5470 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5472 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
| 5474 | printk(KERN_INFO "md: considering %s ...\n", |
| 5475 | bdevname(rdev0->bdev,b)); |
| 5476 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5477 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5479 | printk(KERN_INFO "md: adding %s ...\n", |
| 5480 | bdevname(rdev->bdev,b)); |
| 5481 | list_move(&rdev->same_set, &candidates); |
| 5482 | } |
| 5483 | /* |
| 5484 | * now we have a set of devices, with all of them having |
| 5485 | * mostly sane superblocks. It's time to allocate the |
| 5486 | * mddev. |
| 5487 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5488 | if (part) { |
| 5489 | dev = MKDEV(mdp_major, |
| 5490 | rdev0->preferred_minor << MdpMinorShift); |
| 5491 | unit = MINOR(dev) >> MdpMinorShift; |
| 5492 | } else { |
| 5493 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5494 | unit = MINOR(dev); |
| 5495 | } |
| 5496 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5498 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5499 | break; |
| 5500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | |
| 5502 | md_probe(dev, NULL, NULL); |
| 5503 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5504 | if (!mddev || !mddev->gendisk) { |
| 5505 | if (mddev) |
| 5506 | mddev_put(mddev); |
| 5507 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | "md: cannot allocate memory for md drive.\n"); |
| 5509 | break; |
| 5510 | } |
| 5511 | if (mddev_lock(mddev)) |
| 5512 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5513 | mdname(mddev)); |
| 5514 | else if (mddev->raid_disks || mddev->major_version |
| 5515 | || !list_empty(&mddev->disks)) { |
| 5516 | printk(KERN_WARNING |
| 5517 | "md: %s already running, cannot run %s\n", |
| 5518 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5519 | mddev_unlock(mddev); |
| 5520 | } else { |
| 5521 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5522 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5523 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | list_del_init(&rdev->same_set); |
| 5525 | if (bind_rdev_to_array(rdev, mddev)) |
| 5526 | export_rdev(rdev); |
| 5527 | } |
| 5528 | autorun_array(mddev); |
| 5529 | mddev_unlock(mddev); |
| 5530 | } |
| 5531 | /* on success, candidates will be empty, on error |
| 5532 | * it won't... |
| 5533 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5534 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5535 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | mddev_put(mddev); |
| 5539 | } |
| 5540 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5541 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5542 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5544 | static int get_version(void __user * arg) |
| 5545 | { |
| 5546 | mdu_version_t ver; |
| 5547 | |
| 5548 | ver.major = MD_MAJOR_VERSION; |
| 5549 | ver.minor = MD_MINOR_VERSION; |
| 5550 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5551 | |
| 5552 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5553 | return -EFAULT; |
| 5554 | |
| 5555 | return 0; |
| 5556 | } |
| 5557 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5558 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | { |
| 5560 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5561 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5562 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5564 | nr=working=insync=failed=spare=0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5565 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5567 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | failed++; |
| 5569 | else { |
| 5570 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5571 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5572 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | else |
| 5574 | spare++; |
| 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | info.major_version = mddev->major_version; |
| 5579 | info.minor_version = mddev->minor_version; |
| 5580 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5581 | info.ctime = mddev->ctime; |
| 5582 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5583 | info.size = mddev->dev_sectors / 2; |
| 5584 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5585 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | info.nr_disks = nr; |
| 5587 | info.raid_disks = mddev->raid_disks; |
| 5588 | info.md_minor = mddev->md_minor; |
| 5589 | info.not_persistent= !mddev->persistent; |
| 5590 | |
| 5591 | info.utime = mddev->utime; |
| 5592 | info.state = 0; |
| 5593 | if (mddev->in_sync) |
| 5594 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5595 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5596 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5597 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | info.working_disks = working; |
| 5599 | info.failed_disks = failed; |
| 5600 | info.spare_disks = spare; |
| 5601 | |
| 5602 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5603 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | |
| 5605 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5606 | return -EFAULT; |
| 5607 | |
| 5608 | return 0; |
| 5609 | } |
| 5610 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5611 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5612 | { |
| 5613 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5614 | char *ptr, *buf = NULL; |
| 5615 | int err = -ENOMEM; |
| 5616 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5617 | if (md_allow_write(mddev)) |
| 5618 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5619 | else |
| 5620 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5621 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5622 | if (!file) |
| 5623 | goto out; |
| 5624 | |
| 5625 | /* bitmap disabled, zero the first byte and copy out */ |
| 5626 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5627 | file->pathname[0] = '\0'; |
| 5628 | goto copy_out; |
| 5629 | } |
| 5630 | |
| 5631 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5632 | if (!buf) |
| 5633 | goto out; |
| 5634 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5635 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5636 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5637 | goto out; |
| 5638 | |
| 5639 | strcpy(file->pathname, ptr); |
| 5640 | |
| 5641 | copy_out: |
| 5642 | err = 0; |
| 5643 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5644 | err = -EFAULT; |
| 5645 | out: |
| 5646 | kfree(buf); |
| 5647 | kfree(file); |
| 5648 | return err; |
| 5649 | } |
| 5650 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5651 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | { |
| 5653 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5654 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | |
| 5656 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5657 | return -EFAULT; |
| 5658 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5659 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | if (rdev) { |
| 5661 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5662 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5663 | info.raid_disk = rdev->raid_disk; |
| 5664 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5665 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5667 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5669 | info.state |= (1<<MD_DISK_SYNC); |
| 5670 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5671 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5672 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | } else { |
| 5674 | info.major = info.minor = 0; |
| 5675 | info.raid_disk = -1; |
| 5676 | info.state = (1<<MD_DISK_REMOVED); |
| 5677 | } |
| 5678 | |
| 5679 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5680 | return -EFAULT; |
| 5681 | |
| 5682 | return 0; |
| 5683 | } |
| 5684 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5685 | 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] | 5686 | { |
| 5687 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5688 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | dev_t dev = MKDEV(info->major,info->minor); |
| 5690 | |
| 5691 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5692 | return -EOVERFLOW; |
| 5693 | |
| 5694 | if (!mddev->raid_disks) { |
| 5695 | int err; |
| 5696 | /* expecting a device which has a superblock */ |
| 5697 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5698 | if (IS_ERR(rdev)) { |
| 5699 | printk(KERN_WARNING |
| 5700 | "md: md_import_device returned %ld\n", |
| 5701 | PTR_ERR(rdev)); |
| 5702 | return PTR_ERR(rdev); |
| 5703 | } |
| 5704 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5705 | struct md_rdev *rdev0 |
| 5706 | = list_entry(mddev->disks.next, |
| 5707 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5708 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5710 | if (err < 0) { |
| 5711 | printk(KERN_WARNING |
| 5712 | "md: %s has different UUID to %s\n", |
| 5713 | bdevname(rdev->bdev,b), |
| 5714 | bdevname(rdev0->bdev,b2)); |
| 5715 | export_rdev(rdev); |
| 5716 | return -EINVAL; |
| 5717 | } |
| 5718 | } |
| 5719 | err = bind_rdev_to_array(rdev, mddev); |
| 5720 | if (err) |
| 5721 | export_rdev(rdev); |
| 5722 | return err; |
| 5723 | } |
| 5724 | |
| 5725 | /* |
| 5726 | * add_new_disk can be used once the array is assembled |
| 5727 | * to add "hot spares". They must already have a superblock |
| 5728 | * written |
| 5729 | */ |
| 5730 | if (mddev->pers) { |
| 5731 | int err; |
| 5732 | if (!mddev->pers->hot_add_disk) { |
| 5733 | printk(KERN_WARNING |
| 5734 | "%s: personality does not support diskops!\n", |
| 5735 | mdname(mddev)); |
| 5736 | return -EINVAL; |
| 5737 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5738 | if (mddev->persistent) |
| 5739 | rdev = md_import_device(dev, mddev->major_version, |
| 5740 | mddev->minor_version); |
| 5741 | else |
| 5742 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | if (IS_ERR(rdev)) { |
| 5744 | printk(KERN_WARNING |
| 5745 | "md: md_import_device returned %ld\n", |
| 5746 | PTR_ERR(rdev)); |
| 5747 | return PTR_ERR(rdev); |
| 5748 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5749 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5750 | if (!mddev->persistent) { |
| 5751 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5752 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5753 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5754 | set_bit(In_sync, &rdev->flags); |
| 5755 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5756 | rdev->raid_disk = -1; |
| 5757 | } else |
| 5758 | super_types[mddev->major_version]. |
| 5759 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5760 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5761 | (!test_bit(In_sync, &rdev->flags) || |
| 5762 | rdev->raid_disk != info->raid_disk)) { |
| 5763 | /* This was a hot-add request, but events doesn't |
| 5764 | * match, so reject it. |
| 5765 | */ |
| 5766 | export_rdev(rdev); |
| 5767 | return -EINVAL; |
| 5768 | } |
| 5769 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5770 | if (test_bit(In_sync, &rdev->flags)) |
| 5771 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5772 | else |
| 5773 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5774 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5775 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5776 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5777 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5778 | else |
| 5779 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | rdev->raid_disk = -1; |
| 5782 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5783 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5784 | /* If there is hot_add_disk but no hot_remove_disk |
| 5785 | * then added disks for geometry changes, |
| 5786 | * and should be added immediately. |
| 5787 | */ |
| 5788 | super_types[mddev->major_version]. |
| 5789 | validate_super(mddev, rdev); |
| 5790 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5791 | if (err) |
| 5792 | unbind_rdev_from_array(rdev); |
| 5793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | if (err) |
| 5795 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5796 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5797 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5798 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5799 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5800 | if (mddev->degraded) |
| 5801 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5802 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5803 | if (!err) |
| 5804 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5805 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | return err; |
| 5807 | } |
| 5808 | |
| 5809 | /* otherwise, add_new_disk is only allowed |
| 5810 | * for major_version==0 superblocks |
| 5811 | */ |
| 5812 | if (mddev->major_version != 0) { |
| 5813 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5814 | mdname(mddev)); |
| 5815 | return -EINVAL; |
| 5816 | } |
| 5817 | |
| 5818 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5819 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5820 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | if (IS_ERR(rdev)) { |
| 5822 | printk(KERN_WARNING |
| 5823 | "md: error, md_import_device() returned %ld\n", |
| 5824 | PTR_ERR(rdev)); |
| 5825 | return PTR_ERR(rdev); |
| 5826 | } |
| 5827 | rdev->desc_nr = info->number; |
| 5828 | if (info->raid_disk < mddev->raid_disks) |
| 5829 | rdev->raid_disk = info->raid_disk; |
| 5830 | else |
| 5831 | rdev->raid_disk = -1; |
| 5832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5834 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5835 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5837 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5838 | set_bit(WriteMostly, &rdev->flags); |
| 5839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | if (!mddev->persistent) { |
| 5841 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5842 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5843 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5844 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5845 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5847 | err = bind_rdev_to_array(rdev, mddev); |
| 5848 | if (err) { |
| 5849 | export_rdev(rdev); |
| 5850 | return err; |
| 5851 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | } |
| 5853 | |
| 5854 | return 0; |
| 5855 | } |
| 5856 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5857 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | { |
| 5859 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5860 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | rdev = find_rdev(mddev, dev); |
| 5863 | if (!rdev) |
| 5864 | return -ENXIO; |
| 5865 | |
| 5866 | if (rdev->raid_disk >= 0) |
| 5867 | goto busy; |
| 5868 | |
| 5869 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5870 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5871 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | |
| 5873 | return 0; |
| 5874 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5875 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5877 | return -EBUSY; |
| 5878 | } |
| 5879 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5880 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | { |
| 5882 | char b[BDEVNAME_SIZE]; |
| 5883 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5884 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | |
| 5886 | if (!mddev->pers) |
| 5887 | return -ENODEV; |
| 5888 | |
| 5889 | if (mddev->major_version != 0) { |
| 5890 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5891 | " version-0 superblocks.\n", |
| 5892 | mdname(mddev)); |
| 5893 | return -EINVAL; |
| 5894 | } |
| 5895 | if (!mddev->pers->hot_add_disk) { |
| 5896 | printk(KERN_WARNING |
| 5897 | "%s: personality does not support diskops!\n", |
| 5898 | mdname(mddev)); |
| 5899 | return -EINVAL; |
| 5900 | } |
| 5901 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5902 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | if (IS_ERR(rdev)) { |
| 5904 | printk(KERN_WARNING |
| 5905 | "md: error, md_import_device() returned %ld\n", |
| 5906 | PTR_ERR(rdev)); |
| 5907 | return -EINVAL; |
| 5908 | } |
| 5909 | |
| 5910 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5911 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5913 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5915 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5917 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | printk(KERN_WARNING |
| 5919 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5920 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5921 | err = -EINVAL; |
| 5922 | goto abort_export; |
| 5923 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5924 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5926 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5927 | err = bind_rdev_to_array(rdev, mddev); |
| 5928 | if (err) |
| 5929 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
| 5931 | /* |
| 5932 | * The rest should better be atomic, we can have disk failures |
| 5933 | * noticed in interrupt contexts ... |
| 5934 | */ |
| 5935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | rdev->raid_disk = -1; |
| 5937 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5938 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | |
| 5940 | /* |
| 5941 | * Kick recovery, maybe this spare has to be added to the |
| 5942 | * array immediately. |
| 5943 | */ |
| 5944 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5945 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5946 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | return 0; |
| 5948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | abort_export: |
| 5950 | export_rdev(rdev); |
| 5951 | return err; |
| 5952 | } |
| 5953 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5954 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5955 | { |
| 5956 | int err; |
| 5957 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5958 | if (mddev->pers) { |
| 5959 | if (!mddev->pers->quiesce) |
| 5960 | return -EBUSY; |
| 5961 | if (mddev->recovery || mddev->sync_thread) |
| 5962 | return -EBUSY; |
| 5963 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5966 | |
| 5967 | if (fd >= 0) { |
| 5968 | if (mddev->bitmap) |
| 5969 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5970 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5971 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5972 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5973 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5974 | mdname(mddev)); |
| 5975 | return -EBADF; |
| 5976 | } |
| 5977 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5978 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5979 | if (err) { |
| 5980 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5981 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5982 | fput(mddev->bitmap_info.file); |
| 5983 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5984 | return err; |
| 5985 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5986 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5987 | } else if (mddev->bitmap == NULL) |
| 5988 | return -ENOENT; /* cannot remove what isn't there */ |
| 5989 | err = 0; |
| 5990 | if (mddev->pers) { |
| 5991 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5992 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5993 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5994 | if (!err) |
| 5995 | err = bitmap_load(mddev); |
| 5996 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5997 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5998 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5999 | fd = -1; /* make sure to put the file */ |
| 6000 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6001 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6002 | } |
| 6003 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6004 | if (mddev->bitmap_info.file) { |
| 6005 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 6006 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 6007 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6008 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6009 | } |
| 6010 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6011 | return err; |
| 6012 | } |
| 6013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | /* |
| 6015 | * set_array_info is used two different ways |
| 6016 | * The original usage is when creating a new array. |
| 6017 | * In this usage, raid_disks is > 0 and it together with |
| 6018 | * level, size, not_persistent,layout,chunksize determine the |
| 6019 | * shape of the array. |
| 6020 | * This will always create an array with a type-0.90.0 superblock. |
| 6021 | * The newer usage is when assembling an array. |
| 6022 | * In this case raid_disks will be 0, and the major_version field is |
| 6023 | * use to determine which style super-blocks are to be found on the devices. |
| 6024 | * The minor and patch _version numbers are also kept incase the |
| 6025 | * super_block handler wishes to interpret them. |
| 6026 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6027 | 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] | 6028 | { |
| 6029 | |
| 6030 | if (info->raid_disks == 0) { |
| 6031 | /* just setting version number for superblock loading */ |
| 6032 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6033 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | super_types[info->major_version].name == NULL) { |
| 6035 | /* maybe try to auto-load a module? */ |
| 6036 | printk(KERN_INFO |
| 6037 | "md: superblock version %d not known\n", |
| 6038 | info->major_version); |
| 6039 | return -EINVAL; |
| 6040 | } |
| 6041 | mddev->major_version = info->major_version; |
| 6042 | mddev->minor_version = info->minor_version; |
| 6043 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6044 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6045 | /* ensure mddev_put doesn't delete this now that there |
| 6046 | * is some minimal configuration. |
| 6047 | */ |
| 6048 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | return 0; |
| 6050 | } |
| 6051 | mddev->major_version = MD_MAJOR_VERSION; |
| 6052 | mddev->minor_version = MD_MINOR_VERSION; |
| 6053 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6054 | mddev->ctime = get_seconds(); |
| 6055 | |
| 6056 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6057 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6058 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | mddev->raid_disks = info->raid_disks; |
| 6060 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6061 | * openned |
| 6062 | */ |
| 6063 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6064 | mddev->recovery_cp = MaxSector; |
| 6065 | else |
| 6066 | mddev->recovery_cp = 0; |
| 6067 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6068 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | |
| 6070 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6071 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | |
| 6073 | mddev->max_disks = MD_SB_DISKS; |
| 6074 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6075 | if (mddev->persistent) |
| 6076 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6077 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6079 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 6080 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6081 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6082 | mddev->reshape_position = MaxSector; |
| 6083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | /* |
| 6085 | * Generate a 128 bit UUID |
| 6086 | */ |
| 6087 | get_random_bytes(mddev->uuid, 16); |
| 6088 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6089 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6090 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6091 | mddev->new_layout = mddev->layout; |
| 6092 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6093 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | return 0; |
| 6096 | } |
| 6097 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6098 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6099 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6100 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6101 | |
| 6102 | if (mddev->external_size) |
| 6103 | return; |
| 6104 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6105 | mddev->array_sectors = array_sectors; |
| 6106 | } |
| 6107 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6108 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6109 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6110 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6111 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6112 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6113 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6114 | |
| 6115 | if (mddev->pers->resize == NULL) |
| 6116 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6117 | /* The "num_sectors" is the number of sectors of each device that |
| 6118 | * is used. This can only make sense for arrays with redundancy. |
| 6119 | * linear and raid0 always use whatever space is available. We can only |
| 6120 | * consider changing this number if no resync or reconstruction is |
| 6121 | * 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] | 6122 | * 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] | 6123 | * of each device. If num_sectors is zero, we find the largest size |
| 6124 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6125 | */ |
| 6126 | if (mddev->sync_thread) |
| 6127 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6128 | if (mddev->bitmap) |
| 6129 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 6130 | * grow, and re-add. |
| 6131 | */ |
| 6132 | return -EBUSY; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6133 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6134 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6135 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6136 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6137 | num_sectors = avail; |
| 6138 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6139 | return -ENOSPC; |
| 6140 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6141 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6142 | if (!rv) |
| 6143 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6144 | return rv; |
| 6145 | } |
| 6146 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6147 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6148 | { |
| 6149 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6150 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6151 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6152 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6153 | return -EINVAL; |
| 6154 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6155 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6156 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6157 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6158 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6159 | |
| 6160 | rdev_for_each(rdev, mddev) { |
| 6161 | if (mddev->raid_disks < raid_disks && |
| 6162 | rdev->data_offset < rdev->new_data_offset) |
| 6163 | return -EINVAL; |
| 6164 | if (mddev->raid_disks > raid_disks && |
| 6165 | rdev->data_offset > rdev->new_data_offset) |
| 6166 | return -EINVAL; |
| 6167 | } |
| 6168 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6169 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6170 | if (mddev->delta_disks < 0) |
| 6171 | mddev->reshape_backwards = 1; |
| 6172 | else if (mddev->delta_disks > 0) |
| 6173 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6174 | |
| 6175 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6176 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6177 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6178 | mddev->reshape_backwards = 0; |
| 6179 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6180 | return rv; |
| 6181 | } |
| 6182 | |
| 6183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | /* |
| 6185 | * update_array_info is used to change the configuration of an |
| 6186 | * on-line array. |
| 6187 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6188 | * fields in the info are checked against the array. |
| 6189 | * Any differences that cannot be handled will cause an error. |
| 6190 | * Normally, only one change can be managed at a time. |
| 6191 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6192 | 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] | 6193 | { |
| 6194 | int rv = 0; |
| 6195 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6196 | int state = 0; |
| 6197 | |
| 6198 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6199 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6200 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6201 | |
| 6202 | if (mddev->major_version != info->major_version || |
| 6203 | mddev->minor_version != info->minor_version || |
| 6204 | /* mddev->patch_version != info->patch_version || */ |
| 6205 | mddev->ctime != info->ctime || |
| 6206 | mddev->level != info->level || |
| 6207 | /* mddev->layout != info->layout || */ |
| 6208 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6209 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6210 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6211 | ((state^info->state) & 0xfffffe00) |
| 6212 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | return -EINVAL; |
| 6214 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6215 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6216 | cnt++; |
| 6217 | if (mddev->raid_disks != info->raid_disks) |
| 6218 | cnt++; |
| 6219 | if (mddev->layout != info->layout) |
| 6220 | cnt++; |
| 6221 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6222 | cnt++; |
| 6223 | if (cnt == 0) |
| 6224 | return 0; |
| 6225 | if (cnt > 1) |
| 6226 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | |
| 6228 | if (mddev->layout != info->layout) { |
| 6229 | /* Change layout |
| 6230 | * we don't need to do anything at the md level, the |
| 6231 | * personality will take care of it all. |
| 6232 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6233 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6235 | else { |
| 6236 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6237 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6238 | if (rv) |
| 6239 | mddev->new_layout = mddev->layout; |
| 6240 | return rv; |
| 6241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6243 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6244 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6246 | if (mddev->raid_disks != info->raid_disks) |
| 6247 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6249 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6250 | if (mddev->pers->quiesce == NULL) |
| 6251 | return -EINVAL; |
| 6252 | if (mddev->recovery || mddev->sync_thread) |
| 6253 | return -EBUSY; |
| 6254 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6255 | /* add the bitmap */ |
| 6256 | if (mddev->bitmap) |
| 6257 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6258 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6259 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6260 | mddev->bitmap_info.offset = |
| 6261 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6262 | mddev->pers->quiesce(mddev, 1); |
| 6263 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6264 | if (!rv) |
| 6265 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6266 | if (rv) |
| 6267 | bitmap_destroy(mddev); |
| 6268 | mddev->pers->quiesce(mddev, 0); |
| 6269 | } else { |
| 6270 | /* remove the bitmap */ |
| 6271 | if (!mddev->bitmap) |
| 6272 | return -ENOENT; |
| 6273 | if (mddev->bitmap->file) |
| 6274 | return -EINVAL; |
| 6275 | mddev->pers->quiesce(mddev, 1); |
| 6276 | bitmap_destroy(mddev); |
| 6277 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6278 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6279 | } |
| 6280 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6281 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | return rv; |
| 6283 | } |
| 6284 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6285 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6287 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | |
| 6289 | if (mddev->pers == NULL) |
| 6290 | return -ENODEV; |
| 6291 | |
| 6292 | rdev = find_rdev(mddev, dev); |
| 6293 | if (!rdev) |
| 6294 | return -ENODEV; |
| 6295 | |
| 6296 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6297 | if (!test_bit(Faulty, &rdev->flags)) |
| 6298 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | return 0; |
| 6300 | } |
| 6301 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6302 | /* |
| 6303 | * We have a problem here : there is no easy way to give a CHS |
| 6304 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6305 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6306 | * dosfs just mad... ;-) |
| 6307 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6308 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6309 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6310 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6311 | |
| 6312 | geo->heads = 2; |
| 6313 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6314 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6315 | return 0; |
| 6316 | } |
| 6317 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6318 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6319 | unsigned int cmd, unsigned long arg) |
| 6320 | { |
| 6321 | int err = 0; |
| 6322 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6323 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6324 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6326 | switch (cmd) { |
| 6327 | case RAID_VERSION: |
| 6328 | case GET_ARRAY_INFO: |
| 6329 | case GET_DISK_INFO: |
| 6330 | break; |
| 6331 | default: |
| 6332 | if (!capable(CAP_SYS_ADMIN)) |
| 6333 | return -EACCES; |
| 6334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | |
| 6336 | /* |
| 6337 | * Commands dealing with the RAID driver but not any |
| 6338 | * particular array: |
| 6339 | */ |
| 6340 | switch (cmd) |
| 6341 | { |
| 6342 | case RAID_VERSION: |
| 6343 | err = get_version(argp); |
| 6344 | goto done; |
| 6345 | |
| 6346 | case PRINT_RAID_DEBUG: |
| 6347 | err = 0; |
| 6348 | md_print_devices(); |
| 6349 | goto done; |
| 6350 | |
| 6351 | #ifndef MODULE |
| 6352 | case RAID_AUTORUN: |
| 6353 | err = 0; |
| 6354 | autostart_arrays(arg); |
| 6355 | goto done; |
| 6356 | #endif |
| 6357 | default:; |
| 6358 | } |
| 6359 | |
| 6360 | /* |
| 6361 | * Commands creating/starting a new array: |
| 6362 | */ |
| 6363 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6364 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | |
| 6366 | if (!mddev) { |
| 6367 | BUG(); |
| 6368 | goto abort; |
| 6369 | } |
| 6370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | err = mddev_lock(mddev); |
| 6372 | if (err) { |
| 6373 | printk(KERN_INFO |
| 6374 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6375 | err, cmd); |
| 6376 | goto abort; |
| 6377 | } |
| 6378 | |
| 6379 | switch (cmd) |
| 6380 | { |
| 6381 | case SET_ARRAY_INFO: |
| 6382 | { |
| 6383 | mdu_array_info_t info; |
| 6384 | if (!arg) |
| 6385 | memset(&info, 0, sizeof(info)); |
| 6386 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6387 | err = -EFAULT; |
| 6388 | goto abort_unlock; |
| 6389 | } |
| 6390 | if (mddev->pers) { |
| 6391 | err = update_array_info(mddev, &info); |
| 6392 | if (err) { |
| 6393 | printk(KERN_WARNING "md: couldn't update" |
| 6394 | " array info. %d\n", err); |
| 6395 | goto abort_unlock; |
| 6396 | } |
| 6397 | goto done_unlock; |
| 6398 | } |
| 6399 | if (!list_empty(&mddev->disks)) { |
| 6400 | printk(KERN_WARNING |
| 6401 | "md: array %s already has disks!\n", |
| 6402 | mdname(mddev)); |
| 6403 | err = -EBUSY; |
| 6404 | goto abort_unlock; |
| 6405 | } |
| 6406 | if (mddev->raid_disks) { |
| 6407 | printk(KERN_WARNING |
| 6408 | "md: array %s already initialised!\n", |
| 6409 | mdname(mddev)); |
| 6410 | err = -EBUSY; |
| 6411 | goto abort_unlock; |
| 6412 | } |
| 6413 | err = set_array_info(mddev, &info); |
| 6414 | if (err) { |
| 6415 | printk(KERN_WARNING "md: couldn't set" |
| 6416 | " array info. %d\n", err); |
| 6417 | goto abort_unlock; |
| 6418 | } |
| 6419 | } |
| 6420 | goto done_unlock; |
| 6421 | |
| 6422 | default:; |
| 6423 | } |
| 6424 | |
| 6425 | /* |
| 6426 | * Commands querying/configuring an existing array: |
| 6427 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6428 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6429 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6430 | if ((!mddev->raid_disks && !mddev->external) |
| 6431 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6432 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6433 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | err = -ENODEV; |
| 6435 | goto abort_unlock; |
| 6436 | } |
| 6437 | |
| 6438 | /* |
| 6439 | * Commands even a read-only array can execute: |
| 6440 | */ |
| 6441 | switch (cmd) |
| 6442 | { |
| 6443 | case GET_ARRAY_INFO: |
| 6444 | err = get_array_info(mddev, argp); |
| 6445 | goto done_unlock; |
| 6446 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6447 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6448 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6449 | goto done_unlock; |
| 6450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | case GET_DISK_INFO: |
| 6452 | err = get_disk_info(mddev, argp); |
| 6453 | goto done_unlock; |
| 6454 | |
| 6455 | case RESTART_ARRAY_RW: |
| 6456 | err = restart_array(mddev); |
| 6457 | goto done_unlock; |
| 6458 | |
| 6459 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6460 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | goto done_unlock; |
| 6462 | |
| 6463 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6464 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | goto done_unlock; |
| 6466 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6467 | case BLKROSET: |
| 6468 | if (get_user(ro, (int __user *)(arg))) { |
| 6469 | err = -EFAULT; |
| 6470 | goto done_unlock; |
| 6471 | } |
| 6472 | err = -EINVAL; |
| 6473 | |
| 6474 | /* if the bdev is going readonly the value of mddev->ro |
| 6475 | * does not matter, no writes are coming |
| 6476 | */ |
| 6477 | if (ro) |
| 6478 | goto done_unlock; |
| 6479 | |
| 6480 | /* are we are already prepared for writes? */ |
| 6481 | if (mddev->ro != 1) |
| 6482 | goto done_unlock; |
| 6483 | |
| 6484 | /* transitioning to readauto need only happen for |
| 6485 | * arrays that call md_write_start |
| 6486 | */ |
| 6487 | if (mddev->pers) { |
| 6488 | err = restart_array(mddev); |
| 6489 | if (err == 0) { |
| 6490 | mddev->ro = 2; |
| 6491 | set_disk_ro(mddev->gendisk, 0); |
| 6492 | } |
| 6493 | } |
| 6494 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | /* |
| 6498 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6499 | * superblock, so we do not allow them on read-only arrays. |
| 6500 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6501 | * here and hit the 'default' below, so only disallow |
| 6502 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6504 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6505 | if (mddev->ro == 2) { |
| 6506 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6507 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6508 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6509 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6510 | } else { |
| 6511 | err = -EROFS; |
| 6512 | goto abort_unlock; |
| 6513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | } |
| 6515 | |
| 6516 | switch (cmd) |
| 6517 | { |
| 6518 | case ADD_NEW_DISK: |
| 6519 | { |
| 6520 | mdu_disk_info_t info; |
| 6521 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6522 | err = -EFAULT; |
| 6523 | else |
| 6524 | err = add_new_disk(mddev, &info); |
| 6525 | goto done_unlock; |
| 6526 | } |
| 6527 | |
| 6528 | case HOT_REMOVE_DISK: |
| 6529 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6530 | goto done_unlock; |
| 6531 | |
| 6532 | case HOT_ADD_DISK: |
| 6533 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6534 | goto done_unlock; |
| 6535 | |
| 6536 | case SET_DISK_FAULTY: |
| 6537 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6538 | goto done_unlock; |
| 6539 | |
| 6540 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6541 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | goto done_unlock; |
| 6543 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6544 | case SET_BITMAP_FILE: |
| 6545 | err = set_bitmap_file(mddev, (int)arg); |
| 6546 | goto done_unlock; |
| 6547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | err = -EINVAL; |
| 6550 | goto abort_unlock; |
| 6551 | } |
| 6552 | |
| 6553 | done_unlock: |
| 6554 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6555 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6556 | err != -EINVAL) |
| 6557 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | mddev_unlock(mddev); |
| 6559 | |
| 6560 | return err; |
| 6561 | done: |
| 6562 | if (err) |
| 6563 | MD_BUG(); |
| 6564 | abort: |
| 6565 | return err; |
| 6566 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6567 | #ifdef CONFIG_COMPAT |
| 6568 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6569 | unsigned int cmd, unsigned long arg) |
| 6570 | { |
| 6571 | switch (cmd) { |
| 6572 | case HOT_REMOVE_DISK: |
| 6573 | case HOT_ADD_DISK: |
| 6574 | case SET_DISK_FAULTY: |
| 6575 | case SET_BITMAP_FILE: |
| 6576 | /* These take in integer arg, do not convert */ |
| 6577 | break; |
| 6578 | default: |
| 6579 | arg = (unsigned long)compat_ptr(arg); |
| 6580 | break; |
| 6581 | } |
| 6582 | |
| 6583 | return md_ioctl(bdev, mode, cmd, arg); |
| 6584 | } |
| 6585 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6587 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | { |
| 6589 | /* |
| 6590 | * Succeed if we can lock the mddev, which confirms that |
| 6591 | * it isn't being stopped right now. |
| 6592 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6593 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6594 | int err; |
| 6595 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6596 | if (mddev->gendisk != bdev->bd_disk) { |
| 6597 | /* we are racing with mddev_put which is discarding this |
| 6598 | * bd_disk. |
| 6599 | */ |
| 6600 | mddev_put(mddev); |
| 6601 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6602 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6603 | /* Then retry the open from the top */ |
| 6604 | return -ERESTARTSYS; |
| 6605 | } |
| 6606 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6607 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6608 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | goto out; |
| 6610 | |
| 6611 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6612 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6613 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6615 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | out: |
| 6617 | return err; |
| 6618 | } |
| 6619 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6620 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6621 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6622 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6623 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6624 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6625 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | mddev_put(mddev); |
| 6627 | |
| 6628 | return 0; |
| 6629 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6630 | |
| 6631 | static int md_media_changed(struct gendisk *disk) |
| 6632 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6633 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6634 | |
| 6635 | return mddev->changed; |
| 6636 | } |
| 6637 | |
| 6638 | static int md_revalidate(struct gendisk *disk) |
| 6639 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6640 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6641 | |
| 6642 | mddev->changed = 0; |
| 6643 | return 0; |
| 6644 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6645 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | { |
| 6647 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6648 | .open = md_open, |
| 6649 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6650 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6651 | #ifdef CONFIG_COMPAT |
| 6652 | .compat_ioctl = md_compat_ioctl, |
| 6653 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6654 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6655 | .media_changed = md_media_changed, |
| 6656 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | }; |
| 6658 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6659 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6661 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | /* |
| 6664 | * md_thread is a 'system-thread', it's priority should be very |
| 6665 | * high. We avoid resource deadlocks individually in each |
| 6666 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6667 | * the very same RT priority as kswapd, thus we will never get |
| 6668 | * into a priority inversion deadlock. |
| 6669 | * |
| 6670 | * we definitely have to have equal or higher priority than |
| 6671 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6672 | * many dirty RAID5 blocks. |
| 6673 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6675 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6676 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6678 | /* We need to wait INTERRUPTIBLE so that |
| 6679 | * we don't add to the load-average. |
| 6680 | * That means we need to be sure no signals are |
| 6681 | * pending |
| 6682 | */ |
| 6683 | if (signal_pending(current)) |
| 6684 | flush_signals(current); |
| 6685 | |
| 6686 | wait_event_interruptible_timeout |
| 6687 | (thread->wqueue, |
| 6688 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6689 | || kthread_should_stop(), |
| 6690 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6692 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6693 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6694 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | return 0; |
| 6698 | } |
| 6699 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6700 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | { |
| 6702 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6703 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6705 | wake_up(&thread->wqueue); |
| 6706 | } |
| 6707 | } |
| 6708 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6709 | 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] | 6710 | const char *name) |
| 6711 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6712 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6714 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | if (!thread) |
| 6716 | return NULL; |
| 6717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | init_waitqueue_head(&thread->wqueue); |
| 6719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | thread->run = run; |
| 6721 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6722 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6723 | thread->tsk = kthread_run(md_thread, thread, |
| 6724 | "%s_%s", |
| 6725 | mdname(thread->mddev), |
| 6726 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6727 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | kfree(thread); |
| 6729 | return NULL; |
| 6730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | return thread; |
| 6732 | } |
| 6733 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6734 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6736 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6737 | if (!thread) |
| 6738 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6739 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6740 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6741 | * non-existent thread |
| 6742 | */ |
| 6743 | spin_lock(&pers_lock); |
| 6744 | *threadp = NULL; |
| 6745 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6746 | |
| 6747 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | kfree(thread); |
| 6749 | } |
| 6750 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6751 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | { |
| 6753 | if (!mddev) { |
| 6754 | MD_BUG(); |
| 6755 | return; |
| 6756 | } |
| 6757 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6758 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6759 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6760 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6761 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | return; |
| 6763 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6764 | if (mddev->degraded) |
| 6765 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6766 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6768 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6769 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6770 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6771 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6772 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | /* seq_file implementation /proc/mdstat */ |
| 6776 | |
| 6777 | static void status_unused(struct seq_file *seq) |
| 6778 | { |
| 6779 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6780 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | |
| 6782 | seq_printf(seq, "unused devices: "); |
| 6783 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6784 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | char b[BDEVNAME_SIZE]; |
| 6786 | i++; |
| 6787 | seq_printf(seq, "%s ", |
| 6788 | bdevname(rdev->bdev,b)); |
| 6789 | } |
| 6790 | if (!i) |
| 6791 | seq_printf(seq, "<none>"); |
| 6792 | |
| 6793 | seq_printf(seq, "\n"); |
| 6794 | } |
| 6795 | |
| 6796 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6797 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6799 | sector_t max_sectors, resync, res; |
| 6800 | unsigned long dt, db; |
| 6801 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6802 | int scale; |
| 6803 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6805 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6807 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6808 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6809 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6811 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | |
| 6813 | /* |
| 6814 | * Should not happen. |
| 6815 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6816 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | MD_BUG(); |
| 6818 | return; |
| 6819 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6820 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6821 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6822 | * u32, as those are the requirements for sector_div. |
| 6823 | * Thus 'scale' must be at least 10 |
| 6824 | */ |
| 6825 | scale = 10; |
| 6826 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6827 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6828 | scale++; |
| 6829 | } |
| 6830 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6831 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6832 | |
| 6833 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6835 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | seq_printf(seq, "["); |
| 6837 | for (i = 0; i < x; i++) |
| 6838 | seq_printf(seq, "="); |
| 6839 | seq_printf(seq, ">"); |
| 6840 | for (i = 0; i < y; i++) |
| 6841 | seq_printf(seq, "."); |
| 6842 | seq_printf(seq, "] "); |
| 6843 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6844 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6845 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6846 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6847 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6848 | "check" : |
| 6849 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6850 | "resync" : "recovery"))), |
| 6851 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6852 | (unsigned long long) resync/2, |
| 6853 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | |
| 6855 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | * dt: time from mark until now |
| 6857 | * db: blocks written from mark until now |
| 6858 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6859 | * |
| 6860 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6861 | * So we divide before multiply in case it is 32bit and close |
| 6862 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6863 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6864 | * near the end of resync when the number of remaining sectors |
| 6865 | * is close to 'db'. |
| 6866 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6867 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | */ |
| 6869 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6870 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6871 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6872 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6874 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6875 | sector_div(rt, db/32+1); |
| 6876 | rt *= dt; |
| 6877 | rt >>= 5; |
| 6878 | |
| 6879 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6880 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6882 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6886 | { |
| 6887 | struct list_head *tmp; |
| 6888 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6889 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | |
| 6891 | if (l >= 0x10000) |
| 6892 | return NULL; |
| 6893 | if (!l--) |
| 6894 | /* header */ |
| 6895 | return (void*)1; |
| 6896 | |
| 6897 | spin_lock(&all_mddevs_lock); |
| 6898 | list_for_each(tmp,&all_mddevs) |
| 6899 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6900 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | mddev_get(mddev); |
| 6902 | spin_unlock(&all_mddevs_lock); |
| 6903 | return mddev; |
| 6904 | } |
| 6905 | spin_unlock(&all_mddevs_lock); |
| 6906 | if (!l--) |
| 6907 | return (void*)2;/* tail */ |
| 6908 | return NULL; |
| 6909 | } |
| 6910 | |
| 6911 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6912 | { |
| 6913 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6914 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | |
| 6916 | ++*pos; |
| 6917 | if (v == (void*)2) |
| 6918 | return NULL; |
| 6919 | |
| 6920 | spin_lock(&all_mddevs_lock); |
| 6921 | if (v == (void*)1) |
| 6922 | tmp = all_mddevs.next; |
| 6923 | else |
| 6924 | tmp = mddev->all_mddevs.next; |
| 6925 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6926 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | else { |
| 6928 | next_mddev = (void*)2; |
| 6929 | *pos = 0x10000; |
| 6930 | } |
| 6931 | spin_unlock(&all_mddevs_lock); |
| 6932 | |
| 6933 | if (v != (void*)1) |
| 6934 | mddev_put(mddev); |
| 6935 | return next_mddev; |
| 6936 | |
| 6937 | } |
| 6938 | |
| 6939 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6940 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6941 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | |
| 6943 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6944 | mddev_put(mddev); |
| 6945 | } |
| 6946 | |
| 6947 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6948 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6949 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6950 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6951 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | |
| 6953 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6954 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | seq_printf(seq, "Personalities : "); |
| 6956 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6957 | list_for_each_entry(pers, &pers_list, list) |
| 6958 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | |
| 6960 | spin_unlock(&pers_lock); |
| 6961 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6962 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | return 0; |
| 6964 | } |
| 6965 | if (v == (void*)2) { |
| 6966 | status_unused(seq); |
| 6967 | return 0; |
| 6968 | } |
| 6969 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6970 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6971 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6974 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6975 | mddev->pers ? "" : "in"); |
| 6976 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6977 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6979 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6980 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | seq_printf(seq, " %s", mddev->pers->name); |
| 6982 | } |
| 6983 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6984 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6985 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | char b[BDEVNAME_SIZE]; |
| 6987 | seq_printf(seq, " %s[%d]", |
| 6988 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6989 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6990 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6991 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | seq_printf(seq, "(F)"); |
| 6993 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6994 | } |
| 6995 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6996 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6997 | if (test_bit(Replacement, &rdev->flags)) |
| 6998 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6999 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | if (!list_empty(&mddev->disks)) { |
| 7003 | if (mddev->pers) |
| 7004 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7005 | (unsigned long long) |
| 7006 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | else |
| 7008 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7009 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7011 | if (mddev->persistent) { |
| 7012 | if (mddev->major_version != 0 || |
| 7013 | mddev->minor_version != 90) { |
| 7014 | seq_printf(seq," super %d.%d", |
| 7015 | mddev->major_version, |
| 7016 | mddev->minor_version); |
| 7017 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7018 | } else if (mddev->external) |
| 7019 | seq_printf(seq, " super external:%s", |
| 7020 | mddev->metadata_type); |
| 7021 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7022 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | |
| 7024 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7025 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7027 | if (mddev->pers->sync_request) { |
| 7028 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7029 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7030 | seq_printf(seq, "\n "); |
| 7031 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 7032 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7033 | else if (mddev->recovery_cp < MaxSector) |
| 7034 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7035 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7036 | } else |
| 7037 | seq_printf(seq, "\n "); |
| 7038 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7039 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | |
| 7041 | seq_printf(seq, "\n"); |
| 7042 | } |
| 7043 | mddev_unlock(mddev); |
| 7044 | |
| 7045 | return 0; |
| 7046 | } |
| 7047 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7048 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | .start = md_seq_start, |
| 7050 | .next = md_seq_next, |
| 7051 | .stop = md_seq_stop, |
| 7052 | .show = md_seq_show, |
| 7053 | }; |
| 7054 | |
| 7055 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7056 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7057 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | int error; |
| 7059 | |
| 7060 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7061 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7062 | return error; |
| 7063 | |
| 7064 | seq = file->private_data; |
| 7065 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | return error; |
| 7067 | } |
| 7068 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7069 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7070 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7071 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7072 | int mask; |
| 7073 | |
| 7074 | poll_wait(filp, &md_event_waiters, wait); |
| 7075 | |
| 7076 | /* always allow read */ |
| 7077 | mask = POLLIN | POLLRDNORM; |
| 7078 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7079 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7080 | mask |= POLLERR | POLLPRI; |
| 7081 | return mask; |
| 7082 | } |
| 7083 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7084 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7085 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7086 | .open = md_seq_open, |
| 7087 | .read = seq_read, |
| 7088 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7089 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7090 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | }; |
| 7092 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7093 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7094 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7096 | list_add_tail(&p->list, &pers_list); |
| 7097 | 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] | 7098 | spin_unlock(&pers_lock); |
| 7099 | return 0; |
| 7100 | } |
| 7101 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7102 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7104 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7105 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7106 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | spin_unlock(&pers_lock); |
| 7108 | return 0; |
| 7109 | } |
| 7110 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7111 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7113 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7115 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | |
| 7117 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7118 | rcu_read_lock(); |
| 7119 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7121 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7122 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7123 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7124 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7125 | * as sync_io is counted when a request starts, and |
| 7126 | * disk_stats is counted when it completes. |
| 7127 | * So resync activity will cause curr_events to be smaller than |
| 7128 | * when there was no such activity. |
| 7129 | * non-sync IO will cause disk_stat to increase without |
| 7130 | * increasing sync_io so curr_events will (eventually) |
| 7131 | * be larger than it was before. Once it becomes |
| 7132 | * substantially larger, the test below will cause |
| 7133 | * the array to appear non-idle, and resync will slow |
| 7134 | * down. |
| 7135 | * If there is a lot of outstanding resync activity when |
| 7136 | * we set last_event to curr_events, then all that activity |
| 7137 | * completing might cause the array to appear non-idle |
| 7138 | * and resync will be slowed down even though there might |
| 7139 | * not have been non-resync activity. This will only |
| 7140 | * happen once though. 'last_events' will soon reflect |
| 7141 | * the state where there is little or no outstanding |
| 7142 | * resync requests, and further resync activity will |
| 7143 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7144 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7146 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | rdev->last_events = curr_events; |
| 7148 | idle = 0; |
| 7149 | } |
| 7150 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7151 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | return idle; |
| 7153 | } |
| 7154 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7155 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | { |
| 7157 | /* another "blocks" (512byte) blocks have been synced */ |
| 7158 | atomic_sub(blocks, &mddev->recovery_active); |
| 7159 | wake_up(&mddev->recovery_wait); |
| 7160 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7161 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | md_wakeup_thread(mddev->thread); |
| 7163 | // stop recovery, signal do_sync .... |
| 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7168 | /* md_write_start(mddev, bi) |
| 7169 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7170 | * in superblock) before writing, schedule a superblock update |
| 7171 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7172 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7173 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7175 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7176 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7177 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7178 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7179 | BUG_ON(mddev->ro == 1); |
| 7180 | if (mddev->ro == 2) { |
| 7181 | /* need to switch to read/write */ |
| 7182 | mddev->ro = 0; |
| 7183 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7184 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7185 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7186 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7187 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7188 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7189 | if (mddev->safemode == 1) |
| 7190 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7191 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7192 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7193 | if (mddev->in_sync) { |
| 7194 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7195 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7196 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7197 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7198 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7199 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7200 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7201 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7202 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7203 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7204 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7205 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | } |
| 7207 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7208 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | { |
| 7210 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7211 | if (mddev->safemode == 2) |
| 7212 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7213 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7215 | } |
| 7216 | } |
| 7217 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7218 | /* md_allow_write(mddev) |
| 7219 | * Calling this ensures that the array is marked 'active' so that writes |
| 7220 | * may proceed without blocking. It is important to call this before |
| 7221 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7222 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7223 | * |
| 7224 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7225 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7226 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7227 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7228 | { |
| 7229 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7230 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7231 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7232 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7233 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7234 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7235 | |
| 7236 | spin_lock_irq(&mddev->write_lock); |
| 7237 | if (mddev->in_sync) { |
| 7238 | mddev->in_sync = 0; |
| 7239 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7240 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7241 | if (mddev->safemode_delay && |
| 7242 | mddev->safemode == 0) |
| 7243 | mddev->safemode = 1; |
| 7244 | spin_unlock_irq(&mddev->write_lock); |
| 7245 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7246 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7247 | } else |
| 7248 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7249 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7250 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7251 | return -EAGAIN; |
| 7252 | else |
| 7253 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7254 | } |
| 7255 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7257 | #define SYNC_MARKS 10 |
| 7258 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7259 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7261 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | unsigned int currspeed = 0, |
| 7263 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7264 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | unsigned long mark[SYNC_MARKS]; |
| 7266 | sector_t mark_cnt[SYNC_MARKS]; |
| 7267 | int last_mark,m; |
| 7268 | struct list_head *tmp; |
| 7269 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7270 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7271 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7272 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | |
| 7274 | /* just incase thread restarts... */ |
| 7275 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7276 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7277 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7278 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7280 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7281 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7282 | desc = "data-check"; |
| 7283 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7284 | desc = "requested-resync"; |
| 7285 | else |
| 7286 | desc = "resync"; |
| 7287 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7288 | desc = "reshape"; |
| 7289 | else |
| 7290 | desc = "recovery"; |
| 7291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | /* we overload curr_resync somewhat here. |
| 7293 | * 0 == not engaged in resync at all |
| 7294 | * 2 == checking that there is no conflict with another sync |
| 7295 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7296 | * commense |
| 7297 | * other == active in resync - this many blocks |
| 7298 | * |
| 7299 | * Before starting a resync we must have set curr_resync to |
| 7300 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7301 | * less than ours. When we find one that is the same or higher |
| 7302 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7303 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7304 | * This will mean we have to start checking from the beginning again. |
| 7305 | * |
| 7306 | */ |
| 7307 | |
| 7308 | do { |
| 7309 | mddev->curr_resync = 2; |
| 7310 | |
| 7311 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7312 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7313 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7314 | |
| 7315 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7317 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | if (mddev2 == mddev) |
| 7319 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7320 | if (!mddev->parallel_resync |
| 7321 | && mddev2->curr_resync |
| 7322 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | DEFINE_WAIT(wq); |
| 7324 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7325 | /* arbitrarily yield */ |
| 7326 | mddev->curr_resync = 1; |
| 7327 | wake_up(&resync_wait); |
| 7328 | } |
| 7329 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7330 | /* no need to wait here, we can wait the next |
| 7331 | * time 'round when curr_resync == 2 |
| 7332 | */ |
| 7333 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7334 | /* We need to wait 'interruptible' so as not to |
| 7335 | * contribute to the load average, and not to |
| 7336 | * be caught by 'softlockup' |
| 7337 | */ |
| 7338 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7339 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7340 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7341 | printk(KERN_INFO "md: delaying %s of %s" |
| 7342 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7344 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7346 | if (signal_pending(current)) |
| 7347 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | schedule(); |
| 7349 | finish_wait(&resync_wait, &wq); |
| 7350 | goto try_again; |
| 7351 | } |
| 7352 | finish_wait(&resync_wait, &wq); |
| 7353 | } |
| 7354 | } |
| 7355 | } while (mddev->curr_resync < 2); |
| 7356 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7357 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7358 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7360 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | */ |
| 7362 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7363 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7364 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7365 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7366 | j = mddev->resync_min; |
| 7367 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7368 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7369 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7370 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7371 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7372 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7373 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7374 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7375 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7376 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7377 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7378 | if (rdev->raid_disk >= 0 && |
| 7379 | !test_bit(Faulty, &rdev->flags) && |
| 7380 | !test_bit(In_sync, &rdev->flags) && |
| 7381 | rdev->recovery_offset < j) |
| 7382 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7383 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7386 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7387 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7388 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7389 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7390 | "(but not more than %d KB/sec) for %s.\n", |
| 7391 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7393 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7394 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7395 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7397 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7398 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | } |
| 7400 | last_mark = 0; |
| 7401 | mddev->resync_mark = mark[last_mark]; |
| 7402 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7403 | |
| 7404 | /* |
| 7405 | * Tune reconstruction: |
| 7406 | */ |
| 7407 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7408 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7409 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | |
| 7411 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | last_check = 0; |
| 7413 | |
| 7414 | if (j>2) { |
| 7415 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7416 | "md: resuming %s of %s from checkpoint.\n", |
| 7417 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | mddev->curr_resync = j; |
| 7419 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7420 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | |
| 7422 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7423 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7425 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7426 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7427 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7428 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7429 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7430 | > (max_sectors >> 4)) || |
| 7431 | (j - mddev->curr_resync_completed)*2 |
| 7432 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7433 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7434 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7435 | wait_event(mddev->recovery_wait, |
| 7436 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7437 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7438 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7439 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7440 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7441 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7442 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7443 | /* As this condition is controlled by user-space, |
| 7444 | * we can block indefinitely, so use '_interruptible' |
| 7445 | * to avoid triggering warnings. |
| 7446 | */ |
| 7447 | flush_signals(current); /* just in case */ |
| 7448 | wait_event_interruptible(mddev->recovery_wait, |
| 7449 | mddev->resync_max > j |
| 7450 | || kthread_should_stop()); |
| 7451 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7452 | |
| 7453 | if (kthread_should_stop()) |
| 7454 | goto interrupted; |
| 7455 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7456 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7457 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7458 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7459 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7460 | goto out; |
| 7461 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7462 | |
| 7463 | if (!skipped) { /* actual IO requested */ |
| 7464 | io_sectors += sectors; |
| 7465 | atomic_add(sectors, &mddev->recovery_active); |
| 7466 | } |
| 7467 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7468 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7469 | break; |
| 7470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | j += sectors; |
| 7472 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7473 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7474 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7475 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7476 | * visible in /proc/mdstat |
| 7477 | */ |
| 7478 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7479 | |
| 7480 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7481 | continue; |
| 7482 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7483 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | repeat: |
| 7485 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7486 | /* step marks */ |
| 7487 | int next = (last_mark+1) % SYNC_MARKS; |
| 7488 | |
| 7489 | mddev->resync_mark = mark[next]; |
| 7490 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7491 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7492 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7493 | last_mark = next; |
| 7494 | } |
| 7495 | |
| 7496 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7497 | if (kthread_should_stop()) |
| 7498 | goto interrupted; |
| 7499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7500 | |
| 7501 | /* |
| 7502 | * this loop exits only if either when we are slower than |
| 7503 | * the 'hard' speed limit, or the system was IO-idle for |
| 7504 | * a jiffy. |
| 7505 | * the system might be non-idle CPU-wise, but we only care |
| 7506 | * about not overloading the IO subsystem. (things like an |
| 7507 | * e2fsck being done on the RAID array should execute fast) |
| 7508 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | cond_resched(); |
| 7510 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7511 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7512 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7514 | if (currspeed > speed_min(mddev)) { |
| 7515 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7516 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7517 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | goto repeat; |
| 7519 | } |
| 7520 | } |
| 7521 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7522 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | /* |
| 7524 | * this also signals 'finished resyncing' to md_stop |
| 7525 | */ |
| 7526 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7527 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7528 | |
| 7529 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7530 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7531 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7532 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7533 | mddev->curr_resync > 2) { |
| 7534 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7535 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7536 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7537 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7538 | "md: checkpointing %s of %s.\n", |
| 7539 | desc, mdname(mddev)); |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7540 | mddev->recovery_cp = |
| 7541 | mddev->curr_resync_completed; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7542 | } |
| 7543 | } else |
| 7544 | mddev->recovery_cp = MaxSector; |
| 7545 | } else { |
| 7546 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7547 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7548 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7549 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7550 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7551 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7552 | !test_bit(Faulty, &rdev->flags) && |
| 7553 | !test_bit(In_sync, &rdev->flags) && |
| 7554 | rdev->recovery_offset < mddev->curr_resync) |
| 7555 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7556 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7558 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7559 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7560 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7562 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7563 | /* We completed so min/max setting can be forgotten if used. */ |
| 7564 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7565 | mddev->resync_min = 0; |
| 7566 | mddev->resync_max = MaxSector; |
| 7567 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7568 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7569 | mddev->curr_resync = 0; |
| 7570 | wake_up(&resync_wait); |
| 7571 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7572 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7573 | return; |
| 7574 | |
| 7575 | interrupted: |
| 7576 | /* |
| 7577 | * got a signal, exit. |
| 7578 | */ |
| 7579 | printk(KERN_INFO |
| 7580 | "md: md_do_sync() got signal ... exiting\n"); |
| 7581 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7582 | goto out; |
| 7583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7584 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7585 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7587 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7588 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7589 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7590 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7591 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7592 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7593 | mddev->curr_resync_completed = 0; |
| 7594 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7595 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7596 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7597 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7598 | (test_bit(Faulty, &rdev->flags) || |
| 7599 | ! test_bit(In_sync, &rdev->flags)) && |
| 7600 | atomic_read(&rdev->nr_pending)==0) { |
| 7601 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7602 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7603 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7604 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7605 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7606 | } |
| 7607 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7608 | if (removed) |
| 7609 | sysfs_notify(&mddev->kobj, NULL, |
| 7610 | "degraded"); |
| 7611 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7612 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7613 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7614 | if (rdev->raid_disk >= 0 && |
| 7615 | !test_bit(In_sync, &rdev->flags) && |
| 7616 | !test_bit(Faulty, &rdev->flags)) |
| 7617 | spares++; |
| 7618 | if (rdev->raid_disk < 0 |
| 7619 | && !test_bit(Faulty, &rdev->flags)) { |
| 7620 | rdev->recovery_offset = 0; |
| 7621 | if (mddev->pers-> |
| 7622 | hot_add_disk(mddev, rdev) == 0) { |
| 7623 | if (sysfs_link_rdev(mddev, rdev)) |
| 7624 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7625 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7626 | md_new_event(mddev); |
| 7627 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7628 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7629 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7630 | } |
| 7631 | return spares; |
| 7632 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7633 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7634 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7635 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7636 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7637 | |
| 7638 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7639 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7640 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7641 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7642 | /* success...*/ |
| 7643 | /* activate any spares */ |
| 7644 | if (mddev->pers->spare_active(mddev)) |
| 7645 | sysfs_notify(&mddev->kobj, NULL, |
| 7646 | "degraded"); |
| 7647 | } |
| 7648 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7649 | mddev->pers->finish_reshape) |
| 7650 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7651 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7652 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7653 | * information must be scrapped. Also if any device is now |
| 7654 | * In_sync we must scrape the saved_raid_disk for that device |
| 7655 | * do the superblock for an incrementally recovered device |
| 7656 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7657 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7658 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7659 | if (!mddev->degraded || |
| 7660 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7661 | rdev->saved_raid_disk = -1; |
| 7662 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7663 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7664 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7665 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7666 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7667 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7668 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7669 | /* flag recovery needed just to double check */ |
| 7670 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7671 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7672 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7673 | if (mddev->event_work.func) |
| 7674 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7675 | } |
| 7676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7677 | /* |
| 7678 | * This routine is regularly called by all per-raid-array threads to |
| 7679 | * deal with generic issues like resync and super-block update. |
| 7680 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7681 | * need this as they never do any recovery or update the superblock. |
| 7682 | * |
| 7683 | * It does not do any resync itself, but rather "forks" off other threads |
| 7684 | * to do that as needed. |
| 7685 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7686 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7687 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7688 | * and wakeups up this thread which will reap the thread and finish up. |
| 7689 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7690 | * |
| 7691 | * The overall approach is: |
| 7692 | * 1/ if the superblock needs updating, update it. |
| 7693 | * 2/ If a recovery thread is running, don't do anything else. |
| 7694 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7695 | * 4/ If there are any faulty devices, remove them. |
| 7696 | * 5/ If array is degraded, try to add spares devices |
| 7697 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7698 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7699 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7700 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7701 | if (mddev->suspended) |
| 7702 | return; |
| 7703 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7704 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7705 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7706 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7707 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7708 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7709 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7710 | mdname(mddev)); |
| 7711 | mddev->safemode = 2; |
| 7712 | } |
| 7713 | flush_signals(current); |
| 7714 | } |
| 7715 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7716 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7717 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7719 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7720 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7721 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7722 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7723 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7724 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7725 | )) |
| 7726 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7727 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7728 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7729 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7730 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7731 | if (mddev->ro) { |
| 7732 | /* Only thing we do on a ro array is remove |
| 7733 | * failed devices. |
| 7734 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7735 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7736 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7737 | if (rdev->raid_disk >= 0 && |
| 7738 | !test_bit(Blocked, &rdev->flags) && |
| 7739 | test_bit(Faulty, &rdev->flags) && |
| 7740 | atomic_read(&rdev->nr_pending)==0) { |
| 7741 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7742 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7743 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7744 | rdev->raid_disk = -1; |
| 7745 | } |
| 7746 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7747 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7748 | goto unlock; |
| 7749 | } |
| 7750 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7751 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7752 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7753 | spin_lock_irq(&mddev->write_lock); |
| 7754 | if (mddev->safemode && |
| 7755 | !atomic_read(&mddev->writes_pending) && |
| 7756 | !mddev->in_sync && |
| 7757 | mddev->recovery_cp == MaxSector) { |
| 7758 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7759 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7760 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7761 | } |
| 7762 | if (mddev->safemode == 1) |
| 7763 | mddev->safemode = 0; |
| 7764 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7765 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7766 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7767 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7768 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7769 | if (mddev->flags) |
| 7770 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7773 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7774 | /* resync/recovery still happening */ |
| 7775 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7776 | goto unlock; |
| 7777 | } |
| 7778 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7779 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7780 | goto unlock; |
| 7781 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7782 | /* Set RUNNING before clearing NEEDED to avoid |
| 7783 | * any transients in the value of "sync_action". |
| 7784 | */ |
| 7785 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7786 | /* Clear some bits that don't mean anything, but |
| 7787 | * might be left set |
| 7788 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7789 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7790 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7791 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7792 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7793 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7794 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | /* no recovery is running. |
| 7796 | * remove any failed drives, then |
| 7797 | * add spares if possible. |
| 7798 | * Spare are also removed and re-added, to allow |
| 7799 | * the personality to fail the re-add. |
| 7800 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7801 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7802 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7803 | if (mddev->pers->check_reshape == NULL || |
| 7804 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7805 | /* Cannot proceed */ |
| 7806 | goto unlock; |
| 7807 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7808 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7809 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7810 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7811 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7812 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7813 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7814 | } else if (mddev->recovery_cp < MaxSector) { |
| 7815 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7816 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7817 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7818 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7819 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7821 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7822 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7823 | /* We are adding a device or devices to an array |
| 7824 | * which has the bitmap stored on all devices. |
| 7825 | * So make sure all bitmap pages get written |
| 7826 | */ |
| 7827 | bitmap_write_all(mddev->bitmap); |
| 7828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7830 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7831 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | if (!mddev->sync_thread) { |
| 7833 | printk(KERN_ERR "%s: could not start resync" |
| 7834 | " thread...\n", |
| 7835 | mdname(mddev)); |
| 7836 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7837 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7838 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7839 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7840 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7841 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7842 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7844 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7845 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7846 | } |
| 7847 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7848 | if (!mddev->sync_thread) { |
| 7849 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7850 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7851 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7852 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7853 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | mddev_unlock(mddev); |
| 7856 | } |
| 7857 | } |
| 7858 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7859 | 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] | 7860 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7861 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7862 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7863 | !test_bit(Blocked, &rdev->flags) && |
| 7864 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7865 | msecs_to_jiffies(5000)); |
| 7866 | rdev_dec_pending(rdev, mddev); |
| 7867 | } |
| 7868 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7869 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7870 | void md_finish_reshape(struct mddev *mddev) |
| 7871 | { |
| 7872 | /* called be personality module when reshape completes. */ |
| 7873 | struct md_rdev *rdev; |
| 7874 | |
| 7875 | rdev_for_each(rdev, mddev) { |
| 7876 | if (rdev->data_offset > rdev->new_data_offset) |
| 7877 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7878 | else |
| 7879 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7880 | rdev->data_offset = rdev->new_data_offset; |
| 7881 | } |
| 7882 | } |
| 7883 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7884 | |
| 7885 | /* Bad block management. |
| 7886 | * We can record which blocks on each device are 'bad' and so just |
| 7887 | * fail those blocks, or that stripe, rather than the whole device. |
| 7888 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7889 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7890 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7891 | * A 'shift' can be set so that larger blocks are tracked and |
| 7892 | * consequently larger devices can be covered. |
| 7893 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7894 | * |
| 7895 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7896 | * might need to retry if it is very unlucky. |
| 7897 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7898 | * so we use the write_seqlock_irq variant. |
| 7899 | * |
| 7900 | * When looking for a bad block we specify a range and want to |
| 7901 | * know if any block in the range is bad. So we binary-search |
| 7902 | * to the last range that starts at-or-before the given endpoint, |
| 7903 | * (or "before the sector after the target range") |
| 7904 | * then see if it ends after the given start. |
| 7905 | * We return |
| 7906 | * 0 if there are no known bad blocks in the range |
| 7907 | * 1 if there are known bad block which are all acknowledged |
| 7908 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7909 | * plus the start/length of the first bad section we overlap. |
| 7910 | */ |
| 7911 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7912 | sector_t *first_bad, int *bad_sectors) |
| 7913 | { |
| 7914 | int hi; |
| 7915 | int lo = 0; |
| 7916 | u64 *p = bb->page; |
| 7917 | int rv = 0; |
| 7918 | sector_t target = s + sectors; |
| 7919 | unsigned seq; |
| 7920 | |
| 7921 | if (bb->shift > 0) { |
| 7922 | /* round the start down, and the end up */ |
| 7923 | s >>= bb->shift; |
| 7924 | target += (1<<bb->shift) - 1; |
| 7925 | target >>= bb->shift; |
| 7926 | sectors = target - s; |
| 7927 | } |
| 7928 | /* 'target' is now the first block after the bad range */ |
| 7929 | |
| 7930 | retry: |
| 7931 | seq = read_seqbegin(&bb->lock); |
| 7932 | |
| 7933 | hi = bb->count; |
| 7934 | |
| 7935 | /* Binary search between lo and hi for 'target' |
| 7936 | * i.e. for the last range that starts before 'target' |
| 7937 | */ |
| 7938 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7939 | * are known not to be the last range before target. |
| 7940 | * VARIANT: hi-lo is the number of possible |
| 7941 | * ranges, and decreases until it reaches 1 |
| 7942 | */ |
| 7943 | while (hi - lo > 1) { |
| 7944 | int mid = (lo + hi) / 2; |
| 7945 | sector_t a = BB_OFFSET(p[mid]); |
| 7946 | if (a < target) |
| 7947 | /* This could still be the one, earlier ranges |
| 7948 | * could not. */ |
| 7949 | lo = mid; |
| 7950 | else |
| 7951 | /* This and later ranges are definitely out. */ |
| 7952 | hi = mid; |
| 7953 | } |
| 7954 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7955 | if (hi > lo) { |
| 7956 | /* need to check all range that end after 's' to see if |
| 7957 | * any are unacknowledged. |
| 7958 | */ |
| 7959 | while (lo >= 0 && |
| 7960 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7961 | if (BB_OFFSET(p[lo]) < target) { |
| 7962 | /* starts before the end, and finishes after |
| 7963 | * the start, so they must overlap |
| 7964 | */ |
| 7965 | if (rv != -1 && BB_ACK(p[lo])) |
| 7966 | rv = 1; |
| 7967 | else |
| 7968 | rv = -1; |
| 7969 | *first_bad = BB_OFFSET(p[lo]); |
| 7970 | *bad_sectors = BB_LEN(p[lo]); |
| 7971 | } |
| 7972 | lo--; |
| 7973 | } |
| 7974 | } |
| 7975 | |
| 7976 | if (read_seqretry(&bb->lock, seq)) |
| 7977 | goto retry; |
| 7978 | |
| 7979 | return rv; |
| 7980 | } |
| 7981 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7982 | |
| 7983 | /* |
| 7984 | * Add a range of bad blocks to the table. |
| 7985 | * This might extend the table, or might contract it |
| 7986 | * if two adjacent ranges can be merged. |
| 7987 | * We binary-search to find the 'insertion' point, then |
| 7988 | * decide how best to handle it. |
| 7989 | */ |
| 7990 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7991 | int acknowledged) |
| 7992 | { |
| 7993 | u64 *p; |
| 7994 | int lo, hi; |
| 7995 | int rv = 1; |
| 7996 | |
| 7997 | if (bb->shift < 0) |
| 7998 | /* badblocks are disabled */ |
| 7999 | return 0; |
| 8000 | |
| 8001 | if (bb->shift) { |
| 8002 | /* round the start down, and the end up */ |
| 8003 | sector_t next = s + sectors; |
| 8004 | s >>= bb->shift; |
| 8005 | next += (1<<bb->shift) - 1; |
| 8006 | next >>= bb->shift; |
| 8007 | sectors = next - s; |
| 8008 | } |
| 8009 | |
| 8010 | write_seqlock_irq(&bb->lock); |
| 8011 | |
| 8012 | p = bb->page; |
| 8013 | lo = 0; |
| 8014 | hi = bb->count; |
| 8015 | /* Find the last range that starts at-or-before 's' */ |
| 8016 | while (hi - lo > 1) { |
| 8017 | int mid = (lo + hi) / 2; |
| 8018 | sector_t a = BB_OFFSET(p[mid]); |
| 8019 | if (a <= s) |
| 8020 | lo = mid; |
| 8021 | else |
| 8022 | hi = mid; |
| 8023 | } |
| 8024 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8025 | hi = lo; |
| 8026 | |
| 8027 | if (hi > lo) { |
| 8028 | /* we found a range that might merge with the start |
| 8029 | * of our new range |
| 8030 | */ |
| 8031 | sector_t a = BB_OFFSET(p[lo]); |
| 8032 | sector_t e = a + BB_LEN(p[lo]); |
| 8033 | int ack = BB_ACK(p[lo]); |
| 8034 | if (e >= s) { |
| 8035 | /* Yes, we can merge with a previous range */ |
| 8036 | if (s == a && s + sectors >= e) |
| 8037 | /* new range covers old */ |
| 8038 | ack = acknowledged; |
| 8039 | else |
| 8040 | ack = ack && acknowledged; |
| 8041 | |
| 8042 | if (e < s + sectors) |
| 8043 | e = s + sectors; |
| 8044 | if (e - a <= BB_MAX_LEN) { |
| 8045 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8046 | s = e; |
| 8047 | } else { |
| 8048 | /* does not all fit in one range, |
| 8049 | * make p[lo] maximal |
| 8050 | */ |
| 8051 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8052 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8053 | s = a + BB_MAX_LEN; |
| 8054 | } |
| 8055 | sectors = e - s; |
| 8056 | } |
| 8057 | } |
| 8058 | if (sectors && hi < bb->count) { |
| 8059 | /* 'hi' points to the first range that starts after 's'. |
| 8060 | * Maybe we can merge with the start of that range */ |
| 8061 | sector_t a = BB_OFFSET(p[hi]); |
| 8062 | sector_t e = a + BB_LEN(p[hi]); |
| 8063 | int ack = BB_ACK(p[hi]); |
| 8064 | if (a <= s + sectors) { |
| 8065 | /* merging is possible */ |
| 8066 | if (e <= s + sectors) { |
| 8067 | /* full overlap */ |
| 8068 | e = s + sectors; |
| 8069 | ack = acknowledged; |
| 8070 | } else |
| 8071 | ack = ack && acknowledged; |
| 8072 | |
| 8073 | a = s; |
| 8074 | if (e - a <= BB_MAX_LEN) { |
| 8075 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8076 | s = e; |
| 8077 | } else { |
| 8078 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8079 | s = a + BB_MAX_LEN; |
| 8080 | } |
| 8081 | sectors = e - s; |
| 8082 | lo = hi; |
| 8083 | hi++; |
| 8084 | } |
| 8085 | } |
| 8086 | if (sectors == 0 && hi < bb->count) { |
| 8087 | /* we might be able to combine lo and hi */ |
| 8088 | /* Note: 's' is at the end of 'lo' */ |
| 8089 | sector_t a = BB_OFFSET(p[hi]); |
| 8090 | int lolen = BB_LEN(p[lo]); |
| 8091 | int hilen = BB_LEN(p[hi]); |
| 8092 | int newlen = lolen + hilen - (s - a); |
| 8093 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8094 | /* yes, we can combine them */ |
| 8095 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8096 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8097 | memmove(p + hi, p + hi + 1, |
| 8098 | (bb->count - hi - 1) * 8); |
| 8099 | bb->count--; |
| 8100 | } |
| 8101 | } |
| 8102 | while (sectors) { |
| 8103 | /* didn't merge (it all). |
| 8104 | * Need to add a range just before 'hi' */ |
| 8105 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8106 | /* No room for more */ |
| 8107 | rv = 0; |
| 8108 | break; |
| 8109 | } else { |
| 8110 | int this_sectors = sectors; |
| 8111 | memmove(p + hi + 1, p + hi, |
| 8112 | (bb->count - hi) * 8); |
| 8113 | bb->count++; |
| 8114 | |
| 8115 | if (this_sectors > BB_MAX_LEN) |
| 8116 | this_sectors = BB_MAX_LEN; |
| 8117 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8118 | sectors -= this_sectors; |
| 8119 | s += this_sectors; |
| 8120 | } |
| 8121 | } |
| 8122 | |
| 8123 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8124 | if (!acknowledged) |
| 8125 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8126 | write_sequnlock_irq(&bb->lock); |
| 8127 | |
| 8128 | return rv; |
| 8129 | } |
| 8130 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8131 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8132 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8133 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8134 | int rv; |
| 8135 | if (is_new) |
| 8136 | s += rdev->new_data_offset; |
| 8137 | else |
| 8138 | s += rdev->data_offset; |
| 8139 | rv = md_set_badblocks(&rdev->badblocks, |
| 8140 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8141 | if (rv) { |
| 8142 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8143 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8144 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8145 | md_wakeup_thread(rdev->mddev->thread); |
| 8146 | } |
| 8147 | return rv; |
| 8148 | } |
| 8149 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8150 | |
| 8151 | /* |
| 8152 | * Remove a range of bad blocks from the table. |
| 8153 | * This may involve extending the table if we spilt a region, |
| 8154 | * but it must not fail. So if the table becomes full, we just |
| 8155 | * drop the remove request. |
| 8156 | */ |
| 8157 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8158 | { |
| 8159 | u64 *p; |
| 8160 | int lo, hi; |
| 8161 | sector_t target = s + sectors; |
| 8162 | int rv = 0; |
| 8163 | |
| 8164 | if (bb->shift > 0) { |
| 8165 | /* When clearing we round the start up and the end down. |
| 8166 | * This should not matter as the shift should align with |
| 8167 | * the block size and no rounding should ever be needed. |
| 8168 | * However it is better the think a block is bad when it |
| 8169 | * isn't than to think a block is not bad when it is. |
| 8170 | */ |
| 8171 | s += (1<<bb->shift) - 1; |
| 8172 | s >>= bb->shift; |
| 8173 | target >>= bb->shift; |
| 8174 | sectors = target - s; |
| 8175 | } |
| 8176 | |
| 8177 | write_seqlock_irq(&bb->lock); |
| 8178 | |
| 8179 | p = bb->page; |
| 8180 | lo = 0; |
| 8181 | hi = bb->count; |
| 8182 | /* Find the last range that starts before 'target' */ |
| 8183 | while (hi - lo > 1) { |
| 8184 | int mid = (lo + hi) / 2; |
| 8185 | sector_t a = BB_OFFSET(p[mid]); |
| 8186 | if (a < target) |
| 8187 | lo = mid; |
| 8188 | else |
| 8189 | hi = mid; |
| 8190 | } |
| 8191 | if (hi > lo) { |
| 8192 | /* p[lo] is the last range that could overlap the |
| 8193 | * current range. Earlier ranges could also overlap, |
| 8194 | * but only this one can overlap the end of the range. |
| 8195 | */ |
| 8196 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8197 | /* Partial overlap, leave the tail of this range */ |
| 8198 | int ack = BB_ACK(p[lo]); |
| 8199 | sector_t a = BB_OFFSET(p[lo]); |
| 8200 | sector_t end = a + BB_LEN(p[lo]); |
| 8201 | |
| 8202 | if (a < s) { |
| 8203 | /* we need to split this range */ |
| 8204 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8205 | rv = 0; |
| 8206 | goto out; |
| 8207 | } |
| 8208 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8209 | bb->count++; |
| 8210 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8211 | lo++; |
| 8212 | } |
| 8213 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8214 | /* there is no longer an overlap */ |
| 8215 | hi = lo; |
| 8216 | lo--; |
| 8217 | } |
| 8218 | while (lo >= 0 && |
| 8219 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8220 | /* This range does overlap */ |
| 8221 | if (BB_OFFSET(p[lo]) < s) { |
| 8222 | /* Keep the early parts of this range. */ |
| 8223 | int ack = BB_ACK(p[lo]); |
| 8224 | sector_t start = BB_OFFSET(p[lo]); |
| 8225 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8226 | /* now low doesn't overlap, so.. */ |
| 8227 | break; |
| 8228 | } |
| 8229 | lo--; |
| 8230 | } |
| 8231 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8232 | * anything between needs to be discarded |
| 8233 | */ |
| 8234 | if (hi - lo > 1) { |
| 8235 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8236 | bb->count -= (hi - lo - 1); |
| 8237 | } |
| 8238 | } |
| 8239 | |
| 8240 | bb->changed = 1; |
| 8241 | out: |
| 8242 | write_sequnlock_irq(&bb->lock); |
| 8243 | return rv; |
| 8244 | } |
| 8245 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8246 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8247 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8248 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8249 | if (is_new) |
| 8250 | s += rdev->new_data_offset; |
| 8251 | else |
| 8252 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8253 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8254 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8255 | } |
| 8256 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8257 | |
| 8258 | /* |
| 8259 | * Acknowledge all bad blocks in a list. |
| 8260 | * This only succeeds if ->changed is clear. It is used by |
| 8261 | * in-kernel metadata updates |
| 8262 | */ |
| 8263 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8264 | { |
| 8265 | if (bb->page == NULL || bb->changed) |
| 8266 | /* no point even trying */ |
| 8267 | return; |
| 8268 | write_seqlock_irq(&bb->lock); |
| 8269 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8270 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8271 | u64 *p = bb->page; |
| 8272 | int i; |
| 8273 | for (i = 0; i < bb->count ; i++) { |
| 8274 | if (!BB_ACK(p[i])) { |
| 8275 | sector_t start = BB_OFFSET(p[i]); |
| 8276 | int len = BB_LEN(p[i]); |
| 8277 | p[i] = BB_MAKE(start, len, 1); |
| 8278 | } |
| 8279 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8280 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8281 | } |
| 8282 | write_sequnlock_irq(&bb->lock); |
| 8283 | } |
| 8284 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8285 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8286 | /* sysfs access to bad-blocks list. |
| 8287 | * We present two files. |
| 8288 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8289 | * are recorded as bad. The list is truncated to fit within |
| 8290 | * the one-page limit of sysfs. |
| 8291 | * Writing "sector length" to this file adds an acknowledged |
| 8292 | * bad block list. |
| 8293 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8294 | * been acknowledged. Writing to this file adds bad blocks |
| 8295 | * without acknowledging them. This is largely for testing. |
| 8296 | */ |
| 8297 | |
| 8298 | static ssize_t |
| 8299 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8300 | { |
| 8301 | size_t len; |
| 8302 | int i; |
| 8303 | u64 *p = bb->page; |
| 8304 | unsigned seq; |
| 8305 | |
| 8306 | if (bb->shift < 0) |
| 8307 | return 0; |
| 8308 | |
| 8309 | retry: |
| 8310 | seq = read_seqbegin(&bb->lock); |
| 8311 | |
| 8312 | len = 0; |
| 8313 | i = 0; |
| 8314 | |
| 8315 | while (len < PAGE_SIZE && i < bb->count) { |
| 8316 | sector_t s = BB_OFFSET(p[i]); |
| 8317 | unsigned int length = BB_LEN(p[i]); |
| 8318 | int ack = BB_ACK(p[i]); |
| 8319 | i++; |
| 8320 | |
| 8321 | if (unack && ack) |
| 8322 | continue; |
| 8323 | |
| 8324 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8325 | (unsigned long long)s << bb->shift, |
| 8326 | length << bb->shift); |
| 8327 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8328 | if (unack && len == 0) |
| 8329 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8330 | |
| 8331 | if (read_seqretry(&bb->lock, seq)) |
| 8332 | goto retry; |
| 8333 | |
| 8334 | return len; |
| 8335 | } |
| 8336 | |
| 8337 | #define DO_DEBUG 1 |
| 8338 | |
| 8339 | static ssize_t |
| 8340 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8341 | { |
| 8342 | unsigned long long sector; |
| 8343 | int length; |
| 8344 | char newline; |
| 8345 | #ifdef DO_DEBUG |
| 8346 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8347 | * Normally only a successful write may clear a badblock |
| 8348 | */ |
| 8349 | int clear = 0; |
| 8350 | if (page[0] == '-') { |
| 8351 | clear = 1; |
| 8352 | page++; |
| 8353 | } |
| 8354 | #endif /* DO_DEBUG */ |
| 8355 | |
| 8356 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8357 | case 3: |
| 8358 | if (newline != '\n') |
| 8359 | return -EINVAL; |
| 8360 | case 2: |
| 8361 | if (length <= 0) |
| 8362 | return -EINVAL; |
| 8363 | break; |
| 8364 | default: |
| 8365 | return -EINVAL; |
| 8366 | } |
| 8367 | |
| 8368 | #ifdef DO_DEBUG |
| 8369 | if (clear) { |
| 8370 | md_clear_badblocks(bb, sector, length); |
| 8371 | return len; |
| 8372 | } |
| 8373 | #endif /* DO_DEBUG */ |
| 8374 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8375 | return len; |
| 8376 | else |
| 8377 | return -ENOSPC; |
| 8378 | } |
| 8379 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8380 | static int md_notify_reboot(struct notifier_block *this, |
| 8381 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8382 | { |
| 8383 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8384 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8385 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8386 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8387 | for_each_mddev(mddev, tmp) { |
| 8388 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8389 | if (mddev->pers) |
| 8390 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8391 | mddev->safemode = 2; |
| 8392 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8393 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8394 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8395 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8396 | /* |
| 8397 | * certain more exotic SCSI devices are known to be |
| 8398 | * volatile wrt too early system reboots. While the |
| 8399 | * right place to handle this issue is the given |
| 8400 | * driver, we do want to have a safe RAID driver ... |
| 8401 | */ |
| 8402 | if (need_delay) |
| 8403 | mdelay(1000*1); |
| 8404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | return NOTIFY_DONE; |
| 8406 | } |
| 8407 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8408 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8409 | .notifier_call = md_notify_reboot, |
| 8410 | .next = NULL, |
| 8411 | .priority = INT_MAX, /* before any real devices */ |
| 8412 | }; |
| 8413 | |
| 8414 | static void md_geninit(void) |
| 8415 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8416 | 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] | 8417 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8418 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8419 | } |
| 8420 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8421 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8422 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8423 | int ret = -ENOMEM; |
| 8424 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8425 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8426 | if (!md_wq) |
| 8427 | goto err_wq; |
| 8428 | |
| 8429 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8430 | if (!md_misc_wq) |
| 8431 | goto err_misc_wq; |
| 8432 | |
| 8433 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8434 | goto err_md; |
| 8435 | |
| 8436 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8437 | goto err_mdp; |
| 8438 | mdp_major = ret; |
| 8439 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8440 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8441 | md_probe, NULL, NULL); |
| 8442 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8443 | md_probe, NULL, NULL); |
| 8444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8445 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8446 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8447 | |
| 8448 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8449 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8450 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8451 | err_mdp: |
| 8452 | unregister_blkdev(MD_MAJOR, "md"); |
| 8453 | err_md: |
| 8454 | destroy_workqueue(md_misc_wq); |
| 8455 | err_misc_wq: |
| 8456 | destroy_workqueue(md_wq); |
| 8457 | err_wq: |
| 8458 | return ret; |
| 8459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8460 | |
| 8461 | #ifndef MODULE |
| 8462 | |
| 8463 | /* |
| 8464 | * Searches all registered partitions for autorun RAID arrays |
| 8465 | * at boot time. |
| 8466 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8467 | |
| 8468 | static LIST_HEAD(all_detected_devices); |
| 8469 | struct detected_devices_node { |
| 8470 | struct list_head list; |
| 8471 | dev_t dev; |
| 8472 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8473 | |
| 8474 | void md_autodetect_dev(dev_t dev) |
| 8475 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8476 | struct detected_devices_node *node_detected_dev; |
| 8477 | |
| 8478 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8479 | if (node_detected_dev) { |
| 8480 | node_detected_dev->dev = dev; |
| 8481 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8482 | } else { |
| 8483 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8484 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8486 | } |
| 8487 | |
| 8488 | |
| 8489 | static void autostart_arrays(int part) |
| 8490 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8491 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8492 | struct detected_devices_node *node_detected_dev; |
| 8493 | dev_t dev; |
| 8494 | int i_scanned, i_passed; |
| 8495 | |
| 8496 | i_scanned = 0; |
| 8497 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8498 | |
| 8499 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8500 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8501 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8502 | i_scanned++; |
| 8503 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8504 | struct detected_devices_node, list); |
| 8505 | list_del(&node_detected_dev->list); |
| 8506 | dev = node_detected_dev->dev; |
| 8507 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8508 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8509 | if (IS_ERR(rdev)) |
| 8510 | continue; |
| 8511 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8512 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8513 | MD_BUG(); |
| 8514 | continue; |
| 8515 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8516 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8517 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8518 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8519 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8520 | |
| 8521 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8522 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8523 | |
| 8524 | autorun_devices(part); |
| 8525 | } |
| 8526 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8527 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8528 | |
| 8529 | static __exit void md_exit(void) |
| 8530 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8531 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8532 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8533 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8534 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8535 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8536 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8537 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8538 | unregister_blkdev(mdp_major, "mdp"); |
| 8539 | unregister_reboot_notifier(&md_notifier); |
| 8540 | unregister_sysctl_table(raid_table_header); |
| 8541 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8542 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8543 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8544 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8545 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8546 | destroy_workqueue(md_misc_wq); |
| 8547 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8548 | } |
| 8549 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8550 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8551 | module_exit(md_exit) |
| 8552 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8553 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8554 | { |
| 8555 | return sprintf(buffer, "%d", start_readonly); |
| 8556 | } |
| 8557 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8558 | { |
| 8559 | char *e; |
| 8560 | int num = simple_strtoul(val, &e, 10); |
| 8561 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8562 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8563 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8564 | } |
| 8565 | return -EINVAL; |
| 8566 | } |
| 8567 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8568 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8569 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8570 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8571 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8573 | EXPORT_SYMBOL(register_md_personality); |
| 8574 | EXPORT_SYMBOL(unregister_md_personality); |
| 8575 | EXPORT_SYMBOL(md_error); |
| 8576 | EXPORT_SYMBOL(md_done_sync); |
| 8577 | EXPORT_SYMBOL(md_write_start); |
| 8578 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8579 | EXPORT_SYMBOL(md_register_thread); |
| 8580 | EXPORT_SYMBOL(md_unregister_thread); |
| 8581 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8582 | EXPORT_SYMBOL(md_check_recovery); |
| 8583 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8584 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8585 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8586 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |