Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 61 | /* pers_list is a list of registered personalities protected |
| 62 | * by pers_lock. |
| 63 | * pers_lock does extra service to protect accesses to |
| 64 | * mddev->thread when the mutex cannot be held. |
| 65 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 66 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(pers_lock); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | static void md_print_devices(void); |
| 70 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 72 | static struct workqueue_struct *md_wq; |
| 73 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 78 | * Default number of read corrections we'll attempt on an rdev |
| 79 | * before ejecting it from the array. We divide the read error |
| 80 | * count by 2 for every hour elapsed between read errors. |
| 81 | */ |
| 82 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 85 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 86 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 87 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 89 | * speed limit - in case reconstruction slows down your system despite |
| 90 | * idle IO detection. |
| 91 | * |
| 92 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 93 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | |
| 96 | static int sysctl_speed_limit_min = 1000; |
| 97 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 98 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | { |
| 100 | return mddev->sync_speed_min ? |
| 101 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 102 | } |
| 103 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_max ? |
| 107 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | static struct ctl_table_header *raid_table_header; |
| 111 | |
| 112 | static ctl_table raid_table[] = { |
| 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .procname = "speed_limit_min", |
| 115 | .data = &sysctl_speed_limit_min, |
| 116 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 117 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 118 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }, |
| 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 125 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 127 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .procname = "raid", |
| 133 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 134 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .child = raid_table, |
| 136 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 137 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static ctl_table raid_root_table[] = { |
| 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .procname = "dev", |
| 143 | .maxlen = 0, |
| 144 | .mode = 0555, |
| 145 | .child = raid_dir_table, |
| 146 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 147 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 150 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 152 | static int start_readonly; |
| 153 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 154 | /* bio_clone_mddev |
| 155 | * like bio_clone, but with a local bio set |
| 156 | */ |
| 157 | |
| 158 | static void mddev_bio_destructor(struct bio *bio) |
| 159 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 160 | struct mddev *mddev, **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | |
| 162 | mddevp = (void*)bio; |
| 163 | mddev = mddevp[-1]; |
| 164 | |
| 165 | bio_free(bio, mddev->bio_set); |
| 166 | } |
| 167 | |
| 168 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 169 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 170 | { |
| 171 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 172 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | |
| 174 | if (!mddev || !mddev->bio_set) |
| 175 | return bio_alloc(gfp_mask, nr_iovecs); |
| 176 | |
| 177 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 178 | mddev->bio_set); |
| 179 | if (!b) |
| 180 | return NULL; |
| 181 | mddevp = (void*)b; |
| 182 | mddevp[-1] = mddev; |
| 183 | b->bi_destructor = mddev_bio_destructor; |
| 184 | return b; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 187 | |
| 188 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 189 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 190 | { |
| 191 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 192 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 193 | |
| 194 | if (!mddev || !mddev->bio_set) |
| 195 | return bio_clone(bio, gfp_mask); |
| 196 | |
| 197 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 198 | mddev->bio_set); |
| 199 | if (!b) |
| 200 | return NULL; |
| 201 | mddevp = (void*)b; |
| 202 | mddevp[-1] = mddev; |
| 203 | b->bi_destructor = mddev_bio_destructor; |
| 204 | __bio_clone(b, bio); |
| 205 | if (bio_integrity(bio)) { |
| 206 | int ret; |
| 207 | |
| 208 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 209 | |
| 210 | if (ret < 0) { |
| 211 | bio_put(b); |
| 212 | return NULL; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return b; |
| 217 | } |
| 218 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 219 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 220 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 221 | { |
| 222 | /* 'bio' is a cloned bio which we need to trim to match |
| 223 | * the given offset and size. |
| 224 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 225 | */ |
| 226 | int i; |
| 227 | struct bio_vec *bvec; |
| 228 | int sofar = 0; |
| 229 | |
| 230 | size <<= 9; |
| 231 | if (offset == 0 && size == bio->bi_size) |
| 232 | return; |
| 233 | |
| 234 | bio->bi_sector += offset; |
| 235 | bio->bi_size = size; |
| 236 | offset <<= 9; |
| 237 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 238 | |
| 239 | while (bio->bi_idx < bio->bi_vcnt && |
| 240 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 241 | /* remove this whole bio_vec */ |
| 242 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 243 | bio->bi_idx++; |
| 244 | } |
| 245 | if (bio->bi_idx < bio->bi_vcnt) { |
| 246 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 247 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 248 | } |
| 249 | /* avoid any complications with bi_idx being non-zero*/ |
| 250 | if (bio->bi_idx) { |
| 251 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 252 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 253 | bio->bi_vcnt -= bio->bi_idx; |
| 254 | bio->bi_idx = 0; |
| 255 | } |
| 256 | /* Make sure vcnt and last bv are not too big */ |
| 257 | bio_for_each_segment(bvec, bio, i) { |
| 258 | if (sofar + bvec->bv_len > size) |
| 259 | bvec->bv_len = size - sofar; |
| 260 | if (bvec->bv_len == 0) { |
| 261 | bio->bi_vcnt = i; |
| 262 | break; |
| 263 | } |
| 264 | sofar += bvec->bv_len; |
| 265 | } |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 270 | * We have a system wide 'event count' that is incremented |
| 271 | * on any 'interesting' event, and readers of /proc/mdstat |
| 272 | * can use 'poll' or 'select' to find out when the event |
| 273 | * count increases. |
| 274 | * |
| 275 | * Events are: |
| 276 | * start array, stop array, error, add device, remove device, |
| 277 | * start build, activate spare |
| 278 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 279 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 280 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 281 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 282 | { |
| 283 | atomic_inc(&md_event_count); |
| 284 | wake_up(&md_event_waiters); |
| 285 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 286 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 287 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 288 | /* Alternate version that can be called from interrupts |
| 289 | * when calling sysfs_notify isn't needed. |
| 290 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 291 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 292 | { |
| 293 | atomic_inc(&md_event_count); |
| 294 | wake_up(&md_event_waiters); |
| 295 | } |
| 296 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 297 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * Enables to iterate over all existing md arrays |
| 299 | * all_mddevs_lock protects this list. |
| 300 | */ |
| 301 | static LIST_HEAD(all_mddevs); |
| 302 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 303 | |
| 304 | |
| 305 | /* |
| 306 | * iterates through all used mddevs in the system. |
| 307 | * We take care to grab the all_mddevs_lock whenever navigating |
| 308 | * the list, and to always hold a refcount when unlocked. |
| 309 | * Any code which breaks out of this loop while own |
| 310 | * a reference to the current mddev and must mddev_put it. |
| 311 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 312 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | \ |
| 314 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 315 | _tmp = all_mddevs.next; \ |
| 316 | _mddev = NULL;}); \ |
| 317 | ({ if (_tmp != &all_mddevs) \ |
| 318 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 320 | if (_mddev) mddev_put(_mddev); \ |
| 321 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 322 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 324 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | ) |
| 326 | |
| 327 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 328 | /* Rather than calling directly into the personality make_request function, |
| 329 | * IO requests come here first so that we can check if the device is |
| 330 | * being suspended pending a reconfiguration. |
| 331 | * We hold a refcount over the call to ->make_request. By the time that |
| 332 | * call has finished, the bio has been linked into some internal structure |
| 333 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 334 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 335 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 337 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 338 | struct mddev *mddev = q->queuedata; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 339 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 340 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 341 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 342 | if (mddev == NULL || mddev->pers == NULL |
| 343 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 344 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 345 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 346 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 347 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 348 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 349 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 350 | DEFINE_WAIT(__wait); |
| 351 | for (;;) { |
| 352 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 353 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 354 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 355 | break; |
| 356 | rcu_read_unlock(); |
| 357 | schedule(); |
| 358 | rcu_read_lock(); |
| 359 | } |
| 360 | finish_wait(&mddev->sb_wait, &__wait); |
| 361 | } |
| 362 | atomic_inc(&mddev->active_io); |
| 363 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 364 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 365 | /* |
| 366 | * save the sectors now since our bio can |
| 367 | * go away inside make_request |
| 368 | */ |
| 369 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 370 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 371 | |
| 372 | cpu = part_stat_lock(); |
| 373 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 374 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 375 | part_stat_unlock(); |
| 376 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 377 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 378 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 381 | /* mddev_suspend makes sure no new requests are submitted |
| 382 | * to the device, and that any requests that have been submitted |
| 383 | * are completely handled. |
| 384 | * Once ->stop is called and completes, the module will be completely |
| 385 | * unused. |
| 386 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 387 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 388 | { |
| 389 | BUG_ON(mddev->suspended); |
| 390 | mddev->suspended = 1; |
| 391 | synchronize_rcu(); |
| 392 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 393 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 394 | |
| 395 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 396 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 398 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 399 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 400 | { |
| 401 | mddev->suspended = 0; |
| 402 | wake_up(&mddev->sb_wait); |
| 403 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 404 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 405 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 406 | md_wakeup_thread(mddev->thread); |
| 407 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 408 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 409 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 410 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 411 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 412 | { |
| 413 | return mddev->suspended; |
| 414 | } |
| 415 | EXPORT_SYMBOL(mddev_congested); |
| 416 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | */ |
| 420 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 423 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 424 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | |
| 426 | rdev_dec_pending(rdev, mddev); |
| 427 | |
| 428 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 429 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 430 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
| 432 | bio_put(bio); |
| 433 | } |
| 434 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 435 | static void md_submit_flush_data(struct work_struct *ws); |
| 436 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 437 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 439 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 440 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 442 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 443 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 445 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 446 | if (rdev->raid_disk >= 0 && |
| 447 | !test_bit(Faulty, &rdev->flags)) { |
| 448 | /* Take two references, one is dropped |
| 449 | * when request finishes, one after |
| 450 | * we reclaim rcu_read_lock |
| 451 | */ |
| 452 | struct bio *bi; |
| 453 | atomic_inc(&rdev->nr_pending); |
| 454 | atomic_inc(&rdev->nr_pending); |
| 455 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 456 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 457 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 458 | bi->bi_private = rdev; |
| 459 | bi->bi_bdev = rdev->bdev; |
| 460 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 461 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 462 | rcu_read_lock(); |
| 463 | rdev_dec_pending(rdev, mddev); |
| 464 | } |
| 465 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 466 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 467 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 468 | } |
| 469 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 470 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 471 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 472 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 473 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 475 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 476 | /* an empty barrier - all done */ |
| 477 | bio_endio(bio, 0); |
| 478 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 479 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 480 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 481 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 482 | |
| 483 | mddev->flush_bio = NULL; |
| 484 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 485 | } |
| 486 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 487 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 488 | { |
| 489 | spin_lock_irq(&mddev->write_lock); |
| 490 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 491 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 492 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 493 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 494 | spin_unlock_irq(&mddev->write_lock); |
| 495 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 496 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 497 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 498 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 499 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 500 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 501 | /* Support for plugging. |
| 502 | * This mirrors the plugging support in request_queue, but does not |
| 503 | * require having a whole queue or request structures. |
| 504 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 505 | * plugged on. This links tot the private plug_handle structure in the |
| 506 | * personality data where we keep a count of the number of outstanding |
| 507 | * plugs so other code can see if a plug is active. |
| 508 | */ |
| 509 | struct md_plug_cb { |
| 510 | struct blk_plug_cb cb; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 511 | struct mddev *mddev; |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 512 | }; |
| 513 | |
| 514 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 515 | { |
| 516 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 517 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 518 | md_wakeup_thread(mdcb->mddev->thread); |
| 519 | kfree(mdcb); |
| 520 | } |
| 521 | |
| 522 | /* Check that an unplug wakeup will come shortly. |
| 523 | * If not, wakeup the md thread immediately |
| 524 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 525 | int mddev_check_plugged(struct mddev *mddev) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 526 | { |
| 527 | struct blk_plug *plug = current->plug; |
| 528 | struct md_plug_cb *mdcb; |
| 529 | |
| 530 | if (!plug) |
| 531 | return 0; |
| 532 | |
| 533 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 534 | if (mdcb->cb.callback == plugger_unplug && |
| 535 | mdcb->mddev == mddev) { |
| 536 | /* Already on the list, move to top */ |
| 537 | if (mdcb != list_first_entry(&plug->cb_list, |
| 538 | struct md_plug_cb, |
| 539 | cb.list)) |
| 540 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 541 | return 1; |
| 542 | } |
| 543 | } |
| 544 | /* Not currently on the callback list */ |
| 545 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 546 | if (!mdcb) |
| 547 | return 0; |
| 548 | |
| 549 | mdcb->mddev = mddev; |
| 550 | mdcb->cb.callback = plugger_unplug; |
| 551 | atomic_inc(&mddev->plug_cnt); |
| 552 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 553 | return 1; |
| 554 | } |
| 555 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 556 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 557 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | atomic_inc(&mddev->active); |
| 560 | return mddev; |
| 561 | } |
| 562 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 563 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 564 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 565 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 567 | struct bio_set *bs = NULL; |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 570 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 571 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 572 | mddev->ctime == 0 && !mddev->hold_active) { |
| 573 | /* Array is not configured at all, and not held active, |
| 574 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 575 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 576 | bs = mddev->bio_set; |
| 577 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 578 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 579 | /* We did a probe so need to clean up. Call |
| 580 | * queue_work inside the spinlock so that |
| 581 | * flush_workqueue() after mddev_find will |
| 582 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 583 | */ |
| 584 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 585 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 586 | } else |
| 587 | kfree(mddev); |
| 588 | } |
| 589 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 590 | if (bs) |
| 591 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 594 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 595 | { |
| 596 | mutex_init(&mddev->open_mutex); |
| 597 | mutex_init(&mddev->reconfig_mutex); |
| 598 | mutex_init(&mddev->bitmap_info.mutex); |
| 599 | INIT_LIST_HEAD(&mddev->disks); |
| 600 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 601 | init_timer(&mddev->safemode_timer); |
| 602 | atomic_set(&mddev->active, 1); |
| 603 | atomic_set(&mddev->openers, 0); |
| 604 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 605 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 606 | spin_lock_init(&mddev->write_lock); |
| 607 | atomic_set(&mddev->flush_pending, 0); |
| 608 | init_waitqueue_head(&mddev->sb_wait); |
| 609 | init_waitqueue_head(&mddev->recovery_wait); |
| 610 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 611 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 612 | mddev->resync_min = 0; |
| 613 | mddev->resync_max = MaxSector; |
| 614 | mddev->level = LEVEL_NONE; |
| 615 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 616 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 617 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 618 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 620 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 622 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 623 | unit &= ~((1<<MdpMinorShift)-1); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | retry: |
| 626 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 628 | if (unit) { |
| 629 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 630 | if (mddev->unit == unit) { |
| 631 | mddev_get(mddev); |
| 632 | spin_unlock(&all_mddevs_lock); |
| 633 | kfree(new); |
| 634 | return mddev; |
| 635 | } |
| 636 | |
| 637 | if (new) { |
| 638 | list_add(&new->all_mddevs, &all_mddevs); |
| 639 | spin_unlock(&all_mddevs_lock); |
| 640 | new->hold_active = UNTIL_IOCTL; |
| 641 | return new; |
| 642 | } |
| 643 | } else if (new) { |
| 644 | /* find an unused unit number */ |
| 645 | static int next_minor = 512; |
| 646 | int start = next_minor; |
| 647 | int is_free = 0; |
| 648 | int dev = 0; |
| 649 | while (!is_free) { |
| 650 | dev = MKDEV(MD_MAJOR, next_minor); |
| 651 | next_minor++; |
| 652 | if (next_minor > MINORMASK) |
| 653 | next_minor = 0; |
| 654 | if (next_minor == start) { |
| 655 | /* Oh dear, all in use. */ |
| 656 | spin_unlock(&all_mddevs_lock); |
| 657 | kfree(new); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | is_free = 1; |
| 662 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 663 | if (mddev->unit == dev) { |
| 664 | is_free = 0; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | new->unit = dev; |
| 669 | new->md_minor = MINOR(dev); |
| 670 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | list_add(&new->all_mddevs, &all_mddevs); |
| 672 | spin_unlock(&all_mddevs_lock); |
| 673 | return new; |
| 674 | } |
| 675 | spin_unlock(&all_mddevs_lock); |
| 676 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 677 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (!new) |
| 679 | return NULL; |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | new->unit = unit; |
| 682 | if (MAJOR(unit) == MD_MAJOR) |
| 683 | new->md_minor = MINOR(unit); |
| 684 | else |
| 685 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 686 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 687 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | goto retry; |
| 690 | } |
| 691 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 692 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 694 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 697 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 698 | { |
| 699 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 700 | } |
| 701 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 702 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 704 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 707 | static struct attribute_group md_redundancy_group; |
| 708 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 709 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 711 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 712 | /* These cannot be removed under reconfig_mutex as |
| 713 | * an access to the files will try to take reconfig_mutex |
| 714 | * while holding the file unremovable, which leads to |
| 715 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 716 | * So hold set sysfs_active while the remove in happeing, |
| 717 | * and anything else which might set ->to_remove or my |
| 718 | * otherwise change the sysfs namespace will fail with |
| 719 | * -EBUSY if sysfs_active is still set. |
| 720 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 721 | * test it under the same mutex to ensure its correct value |
| 722 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 723 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 724 | struct attribute_group *to_remove = mddev->to_remove; |
| 725 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 726 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 727 | mutex_unlock(&mddev->reconfig_mutex); |
| 728 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 729 | if (mddev->kobj.sd) { |
| 730 | if (to_remove != &md_redundancy_group) |
| 731 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 732 | if (mddev->pers == NULL || |
| 733 | mddev->pers->sync_request == NULL) { |
| 734 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 735 | if (mddev->sysfs_action) |
| 736 | sysfs_put(mddev->sysfs_action); |
| 737 | mddev->sysfs_action = NULL; |
| 738 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 739 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 740 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 741 | } else |
| 742 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 744 | /* As we've dropped the mutex we need a spinlock to |
| 745 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 746 | */ |
| 747 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 748 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 749 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 752 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 754 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 756 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | if (rdev->desc_nr == nr) |
| 758 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return NULL; |
| 761 | } |
| 762 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 763 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 765 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 767 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | if (rdev->bdev->bd_dev == dev) |
| 769 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return NULL; |
| 772 | } |
| 773 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 774 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 775 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 776 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 777 | list_for_each_entry(pers, &pers_list, list) { |
| 778 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 779 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 780 | if (strcmp(pers->name, clevel)==0) |
| 781 | return pers; |
| 782 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
| 785 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 786 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 787 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 789 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 790 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 793 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
| 795 | if (rdev->sb_page) |
| 796 | MD_BUG(); |
| 797 | |
| 798 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 799 | if (!rdev->sb_page) { |
| 800 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 801 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 807 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
| 809 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 810 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | rdev->sb_loaded = 0; |
| 812 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 813 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 814 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 816 | if (rdev->bb_page) { |
| 817 | put_page(rdev->bb_page); |
| 818 | rdev->bb_page = NULL; |
| 819 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 820 | kfree(rdev->badblocks.page); |
| 821 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 823 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 825 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 826 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 827 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 828 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 829 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 830 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 831 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 832 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 833 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 834 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 835 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 836 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 837 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 838 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 839 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 840 | } |
| 841 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 842 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 843 | sector_t sector, int size, struct page *page) |
| 844 | { |
| 845 | /* write first size bytes of page to sector of rdev |
| 846 | * Increment mddev->pending_writes before returning |
| 847 | * and decrement it on completion, waking up sb_wait |
| 848 | * if zero is reached. |
| 849 | * If an error occurred, call md_error |
| 850 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 851 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 852 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 853 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 854 | bio->bi_sector = sector; |
| 855 | bio_add_page(bio, page, size, 0); |
| 856 | bio->bi_private = rdev; |
| 857 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 858 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 859 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 860 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 861 | } |
| 862 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 863 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 864 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 865 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 866 | DEFINE_WAIT(wq); |
| 867 | for(;;) { |
| 868 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 869 | if (atomic_read(&mddev->pending_writes)==0) |
| 870 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 871 | schedule(); |
| 872 | } |
| 873 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 874 | } |
| 875 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 876 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 881 | 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] | 882 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 884 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | struct completion event; |
| 886 | int ret; |
| 887 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 888 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 890 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 891 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 892 | if (metadata_op) |
| 893 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 894 | else if (rdev->mddev->reshape_position != MaxSector && |
| 895 | (rdev->mddev->reshape_backwards == |
| 896 | (sector >= rdev->mddev->reshape_position))) |
| 897 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 898 | else |
| 899 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | bio_add_page(bio, page, size, 0); |
| 901 | init_completion(&event); |
| 902 | bio->bi_private = &event; |
| 903 | bio->bi_end_io = bi_complete; |
| 904 | submit_bio(rw, bio); |
| 905 | wait_for_completion(&event); |
| 906 | |
| 907 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 908 | bio_put(bio); |
| 909 | return ret; |
| 910 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 911 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 913 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
| 915 | char b[BDEVNAME_SIZE]; |
| 916 | if (!rdev->sb_page) { |
| 917 | MD_BUG(); |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | if (rdev->sb_loaded) |
| 921 | return 0; |
| 922 | |
| 923 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 924 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | goto fail; |
| 926 | rdev->sb_loaded = 1; |
| 927 | return 0; |
| 928 | |
| 929 | fail: |
| 930 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 931 | bdevname(rdev->bdev,b)); |
| 932 | return -EINVAL; |
| 933 | } |
| 934 | |
| 935 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 936 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 937 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 938 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 939 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 940 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 944 | { |
| 945 | int ret; |
| 946 | mdp_super_t *tmp1, *tmp2; |
| 947 | |
| 948 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 949 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 950 | |
| 951 | if (!tmp1 || !tmp2) { |
| 952 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 953 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | goto abort; |
| 955 | } |
| 956 | |
| 957 | *tmp1 = *sb1; |
| 958 | *tmp2 = *sb2; |
| 959 | |
| 960 | /* |
| 961 | * nr_disks is not constant |
| 962 | */ |
| 963 | tmp1->nr_disks = 0; |
| 964 | tmp2->nr_disks = 0; |
| 965 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 966 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 968 | kfree(tmp1); |
| 969 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return ret; |
| 971 | } |
| 972 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 973 | |
| 974 | static u32 md_csum_fold(u32 csum) |
| 975 | { |
| 976 | csum = (csum & 0xffff) + (csum >> 16); |
| 977 | return (csum & 0xffff) + (csum >> 16); |
| 978 | } |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 981 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 982 | u64 newcsum = 0; |
| 983 | u32 *sb32 = (u32*)sb; |
| 984 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | unsigned int disk_csum, csum; |
| 986 | |
| 987 | disk_csum = sb->sb_csum; |
| 988 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 989 | |
| 990 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 991 | newcsum += sb32[i]; |
| 992 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 993 | |
| 994 | |
| 995 | #ifdef CONFIG_ALPHA |
| 996 | /* This used to use csum_partial, which was wrong for several |
| 997 | * reasons including that different results are returned on |
| 998 | * different architectures. It isn't critical that we get exactly |
| 999 | * the same return value as before (we always csum_fold before |
| 1000 | * testing, and that removes any differences). However as we |
| 1001 | * know that csum_partial always returned a 16bit value on |
| 1002 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1003 | */ |
| 1004 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1005 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1007 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return csum; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | /* |
| 1013 | * Handle superblock details. |
| 1014 | * We want to be able to handle multiple superblock formats |
| 1015 | * so we have a common interface to them all, and an array of |
| 1016 | * different handlers. |
| 1017 | * We rely on user-space to write the initial superblock, and support |
| 1018 | * reading and updating of superblocks. |
| 1019 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1020 | * 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] | 1021 | * loads and validates a superblock on dev. |
| 1022 | * if refdev != NULL, compare superblocks on both devices |
| 1023 | * Return: |
| 1024 | * 0 - dev has a superblock that is compatible with refdev |
| 1025 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1026 | * so dev should be used as the refdev in future |
| 1027 | * -EINVAL superblock incompatible or invalid |
| 1028 | * -othererror e.g. -EIO |
| 1029 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1030 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * Verify that dev is acceptable into mddev. |
| 1032 | * The first time, mddev->raid_disks will be 0, and data from |
| 1033 | * dev should be merged in. Subsequent calls check that dev |
| 1034 | * is new enough. Return 0 or -EINVAL |
| 1035 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1036 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | * Update the superblock for rdev with data in mddev |
| 1038 | * This does not write to disc. |
| 1039 | * |
| 1040 | */ |
| 1041 | |
| 1042 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1043 | char *name; |
| 1044 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1045 | int (*load_super)(struct md_rdev *rdev, |
| 1046 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1047 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1048 | int (*validate_super)(struct mddev *mddev, |
| 1049 | struct md_rdev *rdev); |
| 1050 | void (*sync_super)(struct mddev *mddev, |
| 1051 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1052 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1053 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1054 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1055 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1059 | * Check that the given mddev has no bitmap. |
| 1060 | * |
| 1061 | * This function is called from the run method of all personalities that do not |
| 1062 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1063 | * has a bitmap. Otherwise, it returns 0. |
| 1064 | * |
| 1065 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1066 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1067 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1068 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1069 | return 0; |
| 1070 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1071 | mdname(mddev), mddev->pers->name); |
| 1072 | return 1; |
| 1073 | } |
| 1074 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1075 | |
| 1076 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | * load_super for 0.90.0 |
| 1078 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1079 | 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] | 1080 | { |
| 1081 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1082 | mdp_super_t *sb; |
| 1083 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1086 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | * it's at the end of the disk. |
| 1088 | * |
| 1089 | * It also happens to be a multiple of 4Kb. |
| 1090 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1091 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1093 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | if (ret) return ret; |
| 1095 | |
| 1096 | ret = -EINVAL; |
| 1097 | |
| 1098 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1099 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
| 1101 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1102 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1103 | b); |
| 1104 | goto abort; |
| 1105 | } |
| 1106 | |
| 1107 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1108 | sb->minor_version < 90 || |
| 1109 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1111 | sb->major_version, sb->minor_version, |
| 1112 | b); |
| 1113 | goto abort; |
| 1114 | } |
| 1115 | |
| 1116 | if (sb->raid_disks <= 0) |
| 1117 | goto abort; |
| 1118 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1119 | 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] | 1120 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1121 | b); |
| 1122 | goto abort; |
| 1123 | } |
| 1124 | |
| 1125 | rdev->preferred_minor = sb->md_minor; |
| 1126 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1127 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1128 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1129 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (sb->level == LEVEL_MULTIPATH) |
| 1132 | rdev->desc_nr = -1; |
| 1133 | else |
| 1134 | rdev->desc_nr = sb->this_disk.number; |
| 1135 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1136 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1138 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1140 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | if (!uuid_equal(refsb, sb)) { |
| 1142 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1143 | b, bdevname(refdev->bdev,b2)); |
| 1144 | goto abort; |
| 1145 | } |
| 1146 | if (!sb_equal(refsb, sb)) { |
| 1147 | printk(KERN_WARNING "md: %s has same UUID" |
| 1148 | " but different superblock to %s\n", |
| 1149 | b, bdevname(refdev->bdev, b2)); |
| 1150 | goto abort; |
| 1151 | } |
| 1152 | ev1 = md_event(sb); |
| 1153 | ev2 = md_event(refsb); |
| 1154 | if (ev1 > ev2) |
| 1155 | ret = 1; |
| 1156 | else |
| 1157 | ret = 0; |
| 1158 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1159 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1160 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1161 | if (rdev->sectors >= (2ULL << 32)) |
| 1162 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1164 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1165 | /* "this cannot possibly happen" ... */ |
| 1166 | ret = -EINVAL; |
| 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | abort: |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * validate_super for 0.90.0 |
| 1174 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1175 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
| 1177 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1178 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1179 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1181 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1182 | clear_bit(Faulty, &rdev->flags); |
| 1183 | clear_bit(In_sync, &rdev->flags); |
| 1184 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | if (mddev->raid_disks == 0) { |
| 1187 | mddev->major_version = 0; |
| 1188 | mddev->minor_version = sb->minor_version; |
| 1189 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1190 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1191 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | mddev->ctime = sb->ctime; |
| 1193 | mddev->utime = sb->utime; |
| 1194 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1195 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | mddev->layout = sb->layout; |
| 1197 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1198 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1199 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1200 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1201 | mddev->bitmap_info.space = 0; |
| 1202 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1203 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1204 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1205 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1207 | if (mddev->minor_version >= 91) { |
| 1208 | mddev->reshape_position = sb->reshape_position; |
| 1209 | mddev->delta_disks = sb->delta_disks; |
| 1210 | mddev->new_level = sb->new_level; |
| 1211 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1212 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1213 | if (mddev->delta_disks < 0) |
| 1214 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1215 | } else { |
| 1216 | mddev->reshape_position = MaxSector; |
| 1217 | mddev->delta_disks = 0; |
| 1218 | mddev->new_level = mddev->level; |
| 1219 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1220 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1224 | mddev->recovery_cp = MaxSector; |
| 1225 | else { |
| 1226 | if (sb->events_hi == sb->cp_events_hi && |
| 1227 | sb->events_lo == sb->cp_events_lo) { |
| 1228 | mddev->recovery_cp = sb->recovery_cp; |
| 1229 | } else |
| 1230 | mddev->recovery_cp = 0; |
| 1231 | } |
| 1232 | |
| 1233 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1234 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1235 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1236 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1237 | |
| 1238 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1241 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1242 | mddev->bitmap_info.offset = |
| 1243 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1244 | mddev->bitmap_info.space = |
| 1245 | mddev->bitmap_info.space; |
| 1246 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1247 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1248 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1249 | /* Insist on good event counter while assembling, except |
| 1250 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1252 | if (sb->disks[rdev->desc_nr].state & ( |
| 1253 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1254 | if (ev1 < mddev->events) |
| 1255 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1256 | } else if (mddev->bitmap) { |
| 1257 | /* if adding to array with a bitmap, then we can accept an |
| 1258 | * older device ... but not too old. |
| 1259 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1260 | if (ev1 < mddev->bitmap->events_cleared) |
| 1261 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1262 | } else { |
| 1263 | if (ev1 < mddev->events) |
| 1264 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1265 | return 0; |
| 1266 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | desc = sb->disks + rdev->desc_nr; |
| 1270 | |
| 1271 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1272 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1273 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1274 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1275 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1277 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1278 | /* active but not in sync implies recovery up to |
| 1279 | * reshape position. We don't know exactly where |
| 1280 | * that is, so set to zero for now */ |
| 1281 | if (mddev->minor_version >= 91) { |
| 1282 | rdev->recovery_offset = 0; |
| 1283 | rdev->raid_disk = desc->raid_disk; |
| 1284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1286 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1287 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1288 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1289 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * sync_super for 0.90.0 |
| 1295 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1296 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | { |
| 1298 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1299 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | /* make rdev->sb match mddev data.. |
| 1304 | * |
| 1305 | * 1/ zero out disks |
| 1306 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1307 | * 3/ any empty disks < next_spare become removed |
| 1308 | * |
| 1309 | * disks[0] gets initialised to REMOVED because |
| 1310 | * we cannot be sure from other fields if it has |
| 1311 | * been initialised or not. |
| 1312 | */ |
| 1313 | int i; |
| 1314 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1315 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1316 | rdev->sb_size = MD_SB_BYTES; |
| 1317 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1318 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
| 1320 | memset(sb, 0, sizeof(*sb)); |
| 1321 | |
| 1322 | sb->md_magic = MD_SB_MAGIC; |
| 1323 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | sb->patch_version = mddev->patch_version; |
| 1325 | sb->gvalid_words = 0; /* ignored */ |
| 1326 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1327 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1328 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1329 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1330 | |
| 1331 | sb->ctime = mddev->ctime; |
| 1332 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1333 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | sb->raid_disks = mddev->raid_disks; |
| 1335 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1336 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | sb->utime = mddev->utime; |
| 1338 | sb->state = 0; |
| 1339 | sb->events_hi = (mddev->events>>32); |
| 1340 | sb->events_lo = (u32)mddev->events; |
| 1341 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1342 | if (mddev->reshape_position == MaxSector) |
| 1343 | sb->minor_version = 90; |
| 1344 | else { |
| 1345 | sb->minor_version = 91; |
| 1346 | sb->reshape_position = mddev->reshape_position; |
| 1347 | sb->new_level = mddev->new_level; |
| 1348 | sb->delta_disks = mddev->delta_disks; |
| 1349 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1350 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1351 | } |
| 1352 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | if (mddev->in_sync) |
| 1354 | { |
| 1355 | sb->recovery_cp = mddev->recovery_cp; |
| 1356 | sb->cp_events_hi = (mddev->events>>32); |
| 1357 | sb->cp_events_lo = (u32)mddev->events; |
| 1358 | if (mddev->recovery_cp == MaxSector) |
| 1359 | sb->state = (1<< MD_SB_CLEAN); |
| 1360 | } else |
| 1361 | sb->recovery_cp = 0; |
| 1362 | |
| 1363 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1364 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1366 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1367 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1370 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1372 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1373 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1374 | |
| 1375 | if (rdev2->raid_disk >= 0 && |
| 1376 | sb->minor_version >= 91) |
| 1377 | /* we have nowhere to store the recovery_offset, |
| 1378 | * but if it is not below the reshape_position, |
| 1379 | * we can piggy-back on that. |
| 1380 | */ |
| 1381 | is_active = 1; |
| 1382 | if (rdev2->raid_disk < 0 || |
| 1383 | test_bit(Faulty, &rdev2->flags)) |
| 1384 | is_active = 0; |
| 1385 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1386 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1388 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1389 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | d = &sb->disks[rdev2->desc_nr]; |
| 1391 | nr_disks++; |
| 1392 | d->number = rdev2->desc_nr; |
| 1393 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1394 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1395 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | d->raid_disk = rdev2->raid_disk; |
| 1397 | else |
| 1398 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1399 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1401 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1403 | if (test_bit(In_sync, &rdev2->flags)) |
| 1404 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | active++; |
| 1406 | working++; |
| 1407 | } else { |
| 1408 | d->state = 0; |
| 1409 | spare++; |
| 1410 | working++; |
| 1411 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1412 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1413 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1416 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1417 | mdp_disk_t *d = &sb->disks[i]; |
| 1418 | if (d->state == 0 && d->number == 0) { |
| 1419 | d->number = i; |
| 1420 | d->raid_disk = i; |
| 1421 | d->state = (1<<MD_DISK_REMOVED); |
| 1422 | d->state |= (1<<MD_DISK_FAULTY); |
| 1423 | failed++; |
| 1424 | } |
| 1425 | } |
| 1426 | sb->nr_disks = nr_disks; |
| 1427 | sb->active_disks = active; |
| 1428 | sb->working_disks = working; |
| 1429 | sb->failed_disks = failed; |
| 1430 | sb->spare_disks = spare; |
| 1431 | |
| 1432 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1433 | sb->sb_csum = calc_sb_csum(sb); |
| 1434 | } |
| 1435 | |
| 1436 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1437 | * rdev_size_change for 0.90.0 |
| 1438 | */ |
| 1439 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1440 | 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] | 1441 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1442 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1443 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1444 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1445 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1446 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1447 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1448 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1449 | /* Limit to 4TB as metadata cannot record more than that. |
| 1450 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1451 | */ |
| 1452 | if (num_sectors >= (2ULL << 32)) |
| 1453 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1454 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1455 | rdev->sb_page); |
| 1456 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1457 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1458 | } |
| 1459 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1460 | static int |
| 1461 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1462 | { |
| 1463 | /* non-zero offset changes not possible with v0.90 */ |
| 1464 | return new_offset == 0; |
| 1465 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1466 | |
| 1467 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | * version 1 superblock |
| 1469 | */ |
| 1470 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1471 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1473 | __le32 disk_csum; |
| 1474 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | unsigned long long newcsum; |
| 1476 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1477 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | int i; |
| 1479 | |
| 1480 | disk_csum = sb->sb_csum; |
| 1481 | sb->sb_csum = 0; |
| 1482 | newcsum = 0; |
| 1483 | for (i=0; size>=4; size -= 4 ) |
| 1484 | newcsum += le32_to_cpu(*isuper++); |
| 1485 | |
| 1486 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1487 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
| 1489 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1490 | sb->sb_csum = disk_csum; |
| 1491 | return cpu_to_le32(csum); |
| 1492 | } |
| 1493 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1494 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1495 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1496 | 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] | 1497 | { |
| 1498 | struct mdp_superblock_1 *sb; |
| 1499 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1500 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1501 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1503 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1506 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | * It is always aligned to a 4K boundary and |
| 1508 | * depeding on minor_version, it can be: |
| 1509 | * 0: At least 8K, but less than 12K, from end of device |
| 1510 | * 1: At start of device |
| 1511 | * 2: 4K from start of device. |
| 1512 | */ |
| 1513 | switch(minor_version) { |
| 1514 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1515 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1516 | sb_start -= 8*2; |
| 1517 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1520 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | break; |
| 1522 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1523 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | default: |
| 1526 | return -EINVAL; |
| 1527 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1528 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1530 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1531 | * and it is safe to read 4k, so we do that |
| 1532 | */ |
| 1533 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | if (ret) return ret; |
| 1535 | |
| 1536 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1537 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1540 | sb->major_version != cpu_to_le32(1) || |
| 1541 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1542 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1543 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | return -EINVAL; |
| 1545 | |
| 1546 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1547 | printk("md: invalid superblock checksum on %s\n", |
| 1548 | bdevname(rdev->bdev,b)); |
| 1549 | return -EINVAL; |
| 1550 | } |
| 1551 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1552 | printk("md: data_size too small on %s\n", |
| 1553 | bdevname(rdev->bdev,b)); |
| 1554 | return -EINVAL; |
| 1555 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1556 | if (sb->pad0 || |
| 1557 | sb->pad3[0] || |
| 1558 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1559 | /* Some padding is non-zero, might be a new feature */ |
| 1560 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | rdev->preferred_minor = 0xffff; |
| 1563 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1564 | rdev->new_data_offset = rdev->data_offset; |
| 1565 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1566 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1567 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1568 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1570 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1571 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1572 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1573 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1574 | |
| 1575 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1576 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1577 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1578 | if (minor_version |
| 1579 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1580 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1581 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1582 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1583 | rdev->desc_nr = -1; |
| 1584 | else |
| 1585 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1586 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1587 | if (!rdev->bb_page) { |
| 1588 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1589 | if (!rdev->bb_page) |
| 1590 | return -ENOMEM; |
| 1591 | } |
| 1592 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1593 | rdev->badblocks.count == 0) { |
| 1594 | /* need to load the bad block list. |
| 1595 | * Currently we limit it to one page. |
| 1596 | */ |
| 1597 | s32 offset; |
| 1598 | sector_t bb_sector; |
| 1599 | u64 *bbp; |
| 1600 | int i; |
| 1601 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1602 | if (sectors > (PAGE_SIZE / 512)) |
| 1603 | return -EINVAL; |
| 1604 | offset = le32_to_cpu(sb->bblog_offset); |
| 1605 | if (offset == 0) |
| 1606 | return -EINVAL; |
| 1607 | bb_sector = (long long)offset; |
| 1608 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1609 | rdev->bb_page, READ, true)) |
| 1610 | return -EIO; |
| 1611 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1612 | rdev->badblocks.shift = sb->bblog_shift; |
| 1613 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1614 | u64 bb = le64_to_cpu(*bbp); |
| 1615 | int count = bb & (0x3ff); |
| 1616 | u64 sector = bb >> 10; |
| 1617 | sector <<= sb->bblog_shift; |
| 1618 | count <<= sb->bblog_shift; |
| 1619 | if (bb + 1 == 0) |
| 1620 | break; |
| 1621 | if (md_set_badblocks(&rdev->badblocks, |
| 1622 | sector, count, 1) == 0) |
| 1623 | return -EINVAL; |
| 1624 | } |
| 1625 | } else if (sb->bblog_offset == 0) |
| 1626 | rdev->badblocks.shift = -1; |
| 1627 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1628 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1629 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1630 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1632 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
| 1634 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1635 | sb->level != refsb->level || |
| 1636 | sb->layout != refsb->layout || |
| 1637 | sb->chunksize != refsb->chunksize) { |
| 1638 | printk(KERN_WARNING "md: %s has strangely different" |
| 1639 | " superblock to %s\n", |
| 1640 | bdevname(rdev->bdev,b), |
| 1641 | bdevname(refdev->bdev,b2)); |
| 1642 | return -EINVAL; |
| 1643 | } |
| 1644 | ev1 = le64_to_cpu(sb->events); |
| 1645 | ev2 = le64_to_cpu(refsb->events); |
| 1646 | |
| 1647 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1648 | ret = 1; |
| 1649 | else |
| 1650 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1652 | if (minor_version) { |
| 1653 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1654 | sectors -= rdev->data_offset; |
| 1655 | } else |
| 1656 | sectors = rdev->sb_start; |
| 1657 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1659 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1660 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1663 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1665 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1666 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1668 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1669 | clear_bit(Faulty, &rdev->flags); |
| 1670 | clear_bit(In_sync, &rdev->flags); |
| 1671 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | if (mddev->raid_disks == 0) { |
| 1674 | mddev->major_version = 1; |
| 1675 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1676 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1677 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1679 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1680 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1681 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | mddev->layout = le32_to_cpu(sb->layout); |
| 1683 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1684 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1685 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1686 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1687 | mddev->bitmap_info.space = 0; |
| 1688 | /* Default location for bitmap is 1K after superblock |
| 1689 | * using 3K - total of 4K |
| 1690 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1691 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1692 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1693 | mddev->reshape_backwards = 0; |
| 1694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1696 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1697 | |
| 1698 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1699 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1700 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1701 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1702 | mddev->bitmap_info.offset = |
| 1703 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1704 | /* Metadata doesn't record how much space is available. |
| 1705 | * For 1.0, we assume we can use up to the superblock |
| 1706 | * if before, else to 4K beyond superblock. |
| 1707 | * For others, assume no change is possible. |
| 1708 | */ |
| 1709 | if (mddev->minor_version > 0) |
| 1710 | mddev->bitmap_info.space = 0; |
| 1711 | else if (mddev->bitmap_info.offset > 0) |
| 1712 | mddev->bitmap_info.space = |
| 1713 | 8 - mddev->bitmap_info.offset; |
| 1714 | else |
| 1715 | mddev->bitmap_info.space = |
| 1716 | -mddev->bitmap_info.offset; |
| 1717 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1718 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1719 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1720 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1721 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1722 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1723 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1724 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1725 | if (mddev->delta_disks < 0 || |
| 1726 | (mddev->delta_disks == 0 && |
| 1727 | (le32_to_cpu(sb->feature_map) |
| 1728 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1729 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1730 | } else { |
| 1731 | mddev->reshape_position = MaxSector; |
| 1732 | mddev->delta_disks = 0; |
| 1733 | mddev->new_level = mddev->level; |
| 1734 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1735 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1738 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1739 | /* Insist of good event counter while assembling, except for |
| 1740 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1742 | if (rdev->desc_nr >= 0 && |
| 1743 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1744 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1745 | if (ev1 < mddev->events) |
| 1746 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1747 | } else if (mddev->bitmap) { |
| 1748 | /* If adding to array with a bitmap, then we can accept an |
| 1749 | * older device, but not too old. |
| 1750 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1751 | if (ev1 < mddev->bitmap->events_cleared) |
| 1752 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1753 | } else { |
| 1754 | if (ev1 < mddev->events) |
| 1755 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1756 | return 0; |
| 1757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1759 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1760 | if (rdev->desc_nr < 0 || |
| 1761 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1762 | role = 0xffff; |
| 1763 | rdev->desc_nr = -1; |
| 1764 | } else |
| 1765 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | switch(role) { |
| 1767 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | break; |
| 1769 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1770 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | break; |
| 1772 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1773 | if ((le32_to_cpu(sb->feature_map) & |
| 1774 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1775 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1776 | else |
| 1777 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | rdev->raid_disk = role; |
| 1779 | break; |
| 1780 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1781 | if (sb->devflags & WriteMostly1) |
| 1782 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1783 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1784 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1785 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1786 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | return 0; |
| 1789 | } |
| 1790 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1791 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | { |
| 1793 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1794 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | int max_dev, i; |
| 1796 | /* make rdev->sb match mddev and rdev data. */ |
| 1797 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1798 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
| 1800 | sb->feature_map = 0; |
| 1801 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1802 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1804 | |
| 1805 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1806 | sb->events = cpu_to_le64(mddev->events); |
| 1807 | if (mddev->in_sync) |
| 1808 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1809 | else |
| 1810 | sb->resync_offset = cpu_to_le64(0); |
| 1811 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1812 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1813 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1814 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1815 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1816 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1817 | sb->level = cpu_to_le32(mddev->level); |
| 1818 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1819 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1820 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1821 | sb->devflags |= WriteMostly1; |
| 1822 | else |
| 1823 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1824 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1825 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1826 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1827 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1828 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1829 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1830 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1831 | |
| 1832 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1833 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1834 | sb->feature_map |= |
| 1835 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1836 | sb->recovery_offset = |
| 1837 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1838 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1839 | if (test_bit(Replacement, &rdev->flags)) |
| 1840 | sb->feature_map |= |
| 1841 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1842 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1843 | if (mddev->reshape_position != MaxSector) { |
| 1844 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1845 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1846 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1847 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1848 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1849 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1850 | if (mddev->delta_disks == 0 && |
| 1851 | mddev->reshape_backwards) |
| 1852 | sb->feature_map |
| 1853 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1854 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1855 | sb->feature_map |
| 1856 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1857 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1858 | - rdev->data_offset)); |
| 1859 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1860 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1861 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1862 | if (rdev->badblocks.count == 0) |
| 1863 | /* Nothing to do for bad blocks*/ ; |
| 1864 | else if (sb->bblog_offset == 0) |
| 1865 | /* Cannot record bad blocks on this device */ |
| 1866 | md_error(mddev, rdev); |
| 1867 | else { |
| 1868 | struct badblocks *bb = &rdev->badblocks; |
| 1869 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1870 | u64 *p = bb->page; |
| 1871 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1872 | if (bb->changed) { |
| 1873 | unsigned seq; |
| 1874 | |
| 1875 | retry: |
| 1876 | seq = read_seqbegin(&bb->lock); |
| 1877 | |
| 1878 | memset(bbp, 0xff, PAGE_SIZE); |
| 1879 | |
| 1880 | for (i = 0 ; i < bb->count ; i++) { |
| 1881 | u64 internal_bb = *p++; |
| 1882 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1883 | | BB_LEN(internal_bb)); |
| 1884 | *bbp++ = cpu_to_le64(store_bb); |
| 1885 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1886 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1887 | if (read_seqretry(&bb->lock, seq)) |
| 1888 | goto retry; |
| 1889 | |
| 1890 | bb->sector = (rdev->sb_start + |
| 1891 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1892 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1897 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | if (rdev2->desc_nr+1 > max_dev) |
| 1899 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1900 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1901 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1902 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1903 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1904 | rdev->sb_size = max_dev * 2 + 256; |
| 1905 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1906 | if (rdev->sb_size & bmask) |
| 1907 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1908 | } else |
| 1909 | max_dev = le32_to_cpu(sb->max_dev); |
| 1910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | for (i=0; i<max_dev;i++) |
| 1912 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1913 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1914 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1916 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1918 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1920 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1921 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | else |
| 1923 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1924 | } |
| 1925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1927 | } |
| 1928 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1929 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1930 | 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] | 1931 | { |
| 1932 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1933 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1934 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1935 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1936 | if (rdev->data_offset != rdev->new_data_offset) |
| 1937 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1938 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1939 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1940 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1941 | max_sectors -= rdev->data_offset; |
| 1942 | if (!num_sectors || num_sectors > max_sectors) |
| 1943 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1944 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1945 | /* minor version 0 with bitmap we can't move */ |
| 1946 | return 0; |
| 1947 | } else { |
| 1948 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1949 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1950 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1951 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1952 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1953 | if (!num_sectors || num_sectors > max_sectors) |
| 1954 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1955 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1956 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1957 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1958 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1959 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1960 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1961 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1962 | rdev->sb_page); |
| 1963 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1964 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1965 | |
| 1966 | } |
| 1967 | |
| 1968 | static int |
| 1969 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1970 | unsigned long long new_offset) |
| 1971 | { |
| 1972 | /* All necessary checks on new >= old have been done */ |
| 1973 | struct bitmap *bitmap; |
| 1974 | if (new_offset >= rdev->data_offset) |
| 1975 | return 1; |
| 1976 | |
| 1977 | /* with 1.0 metadata, there is no metadata to tread on |
| 1978 | * so we can always move back */ |
| 1979 | if (rdev->mddev->minor_version == 0) |
| 1980 | return 1; |
| 1981 | |
| 1982 | /* otherwise we must be sure not to step on |
| 1983 | * any metadata, so stay: |
| 1984 | * 36K beyond start of superblock |
| 1985 | * beyond end of badblocks |
| 1986 | * beyond write-intent bitmap |
| 1987 | */ |
| 1988 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1989 | return 0; |
| 1990 | bitmap = rdev->mddev->bitmap; |
| 1991 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1992 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1993 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1994 | return 0; |
| 1995 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1996 | return 0; |
| 1997 | |
| 1998 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2001 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | [0] = { |
| 2003 | .name = "0.90.0", |
| 2004 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2005 | .load_super = super_90_load, |
| 2006 | .validate_super = super_90_validate, |
| 2007 | .sync_super = super_90_sync, |
| 2008 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2009 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | }, |
| 2011 | [1] = { |
| 2012 | .name = "md-1", |
| 2013 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2014 | .load_super = super_1_load, |
| 2015 | .validate_super = super_1_validate, |
| 2016 | .sync_super = super_1_sync, |
| 2017 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2018 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | }, |
| 2020 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2022 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2023 | { |
| 2024 | if (mddev->sync_super) { |
| 2025 | mddev->sync_super(mddev, rdev); |
| 2026 | return; |
| 2027 | } |
| 2028 | |
| 2029 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2030 | |
| 2031 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2032 | } |
| 2033 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2034 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2036 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2038 | rcu_read_lock(); |
| 2039 | rdev_for_each_rcu(rdev, mddev1) |
| 2040 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2041 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2042 | rdev2->bdev->bd_contains) { |
| 2043 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2044 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2045 | } |
| 2046 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | static LIST_HEAD(pending_raid_disks); |
| 2051 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2052 | /* |
| 2053 | * Try to register data integrity profile for an mddev |
| 2054 | * |
| 2055 | * This is called when an array is started and after a disk has been kicked |
| 2056 | * from the array. It only succeeds if all working and active component devices |
| 2057 | * are integrity capable with matching profiles. |
| 2058 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2059 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2060 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2061 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2062 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2063 | if (list_empty(&mddev->disks)) |
| 2064 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2065 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2066 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2067 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2068 | /* skip spares and non-functional disks */ |
| 2069 | if (test_bit(Faulty, &rdev->flags)) |
| 2070 | continue; |
| 2071 | if (rdev->raid_disk < 0) |
| 2072 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2073 | if (!reference) { |
| 2074 | /* Use the first rdev as the reference */ |
| 2075 | reference = rdev; |
| 2076 | continue; |
| 2077 | } |
| 2078 | /* does this rdev's profile match the reference profile? */ |
| 2079 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2080 | rdev->bdev->bd_disk) < 0) |
| 2081 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2082 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2083 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2084 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2085 | /* |
| 2086 | * All component devices are integrity capable and have matching |
| 2087 | * profiles, register the common profile for the md device. |
| 2088 | */ |
| 2089 | if (blk_integrity_register(mddev->gendisk, |
| 2090 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2091 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2092 | mdname(mddev)); |
| 2093 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2094 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2095 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2096 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2097 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2098 | mdname(mddev)); |
| 2099 | return -EINVAL; |
| 2100 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2101 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2102 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2103 | EXPORT_SYMBOL(md_integrity_register); |
| 2104 | |
| 2105 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2106 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2107 | { |
| 2108 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2109 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 2110 | |
| 2111 | if (!bi_mddev) /* nothing to do */ |
| 2112 | return; |
| 2113 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2114 | return; |
| 2115 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2116 | rdev->bdev->bd_disk) >= 0) |
| 2117 | return; |
| 2118 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2119 | blk_integrity_unregister(mddev->gendisk); |
| 2120 | } |
| 2121 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2123 | 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] | 2124 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2125 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2126 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2127 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2128 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | if (rdev->mddev) { |
| 2131 | MD_BUG(); |
| 2132 | return -EINVAL; |
| 2133 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2134 | |
| 2135 | /* prevent duplicates */ |
| 2136 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2137 | return -EEXIST; |
| 2138 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2139 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2140 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2141 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2142 | if (mddev->pers) { |
| 2143 | /* Cannot change size, so fail |
| 2144 | * If mddev->level <= 0, then we don't care |
| 2145 | * about aligning sizes (e.g. linear) |
| 2146 | */ |
| 2147 | if (mddev->level > 0) |
| 2148 | return -ENOSPC; |
| 2149 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2150 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
| 2153 | /* Verify rdev->desc_nr is unique. |
| 2154 | * If it is -1, assign a free number, else |
| 2155 | * check number is not in use |
| 2156 | */ |
| 2157 | if (rdev->desc_nr < 0) { |
| 2158 | int choice = 0; |
| 2159 | if (mddev->pers) choice = mddev->raid_disks; |
| 2160 | while (find_rdev_nr(mddev, choice)) |
| 2161 | choice++; |
| 2162 | rdev->desc_nr = choice; |
| 2163 | } else { |
| 2164 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2165 | return -EBUSY; |
| 2166 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2167 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2168 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2169 | mdname(mddev), mddev->max_disks); |
| 2170 | return -EBUSY; |
| 2171 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2172 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2173 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2174 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2177 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2178 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2179 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2180 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2181 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2182 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2183 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2184 | /* failure here is OK */; |
| 2185 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2186 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2187 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2188 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2189 | |
| 2190 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2191 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2194 | |
| 2195 | fail: |
| 2196 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2197 | b, mdname(mddev)); |
| 2198 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2201 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2202 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2203 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2204 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2205 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2208 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | { |
| 2210 | char b[BDEVNAME_SIZE]; |
| 2211 | if (!rdev->mddev) { |
| 2212 | MD_BUG(); |
| 2213 | return; |
| 2214 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2215 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2216 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2218 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2219 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2220 | sysfs_put(rdev->sysfs_state); |
| 2221 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2222 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2223 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2224 | * writing to 'remove' to "dev/state". We also need |
| 2225 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2226 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2227 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2228 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2229 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2230 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | * prevent the device from being mounted, repartitioned or |
| 2235 | * otherwise reused by a RAID array (or any other kernel |
| 2236 | * subsystem), by bd_claiming the device. |
| 2237 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2238 | 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] | 2239 | { |
| 2240 | int err = 0; |
| 2241 | struct block_device *bdev; |
| 2242 | char b[BDEVNAME_SIZE]; |
| 2243 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2244 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2245 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | if (IS_ERR(bdev)) { |
| 2247 | printk(KERN_ERR "md: could not open %s.\n", |
| 2248 | __bdevname(dev, b)); |
| 2249 | return PTR_ERR(bdev); |
| 2250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | rdev->bdev = bdev; |
| 2252 | return err; |
| 2253 | } |
| 2254 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2255 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | { |
| 2257 | struct block_device *bdev = rdev->bdev; |
| 2258 | rdev->bdev = NULL; |
| 2259 | if (!bdev) |
| 2260 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2261 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | void md_autodetect_dev(dev_t dev); |
| 2265 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2266 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | { |
| 2268 | char b[BDEVNAME_SIZE]; |
| 2269 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2270 | bdevname(rdev->bdev,b)); |
| 2271 | if (rdev->mddev) |
| 2272 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2273 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2275 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2276 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | #endif |
| 2278 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2279 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2282 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | { |
| 2284 | unbind_rdev_from_array(rdev); |
| 2285 | export_rdev(rdev); |
| 2286 | } |
| 2287 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2288 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2290 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2292 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | if (!rdev->mddev) { |
| 2294 | MD_BUG(); |
| 2295 | continue; |
| 2296 | } |
| 2297 | kick_rdev_from_array(rdev); |
| 2298 | } |
| 2299 | if (!list_empty(&mddev->disks)) |
| 2300 | MD_BUG(); |
| 2301 | mddev->raid_disks = 0; |
| 2302 | mddev->major_version = 0; |
| 2303 | } |
| 2304 | |
| 2305 | static void print_desc(mdp_disk_t *desc) |
| 2306 | { |
| 2307 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2308 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2309 | } |
| 2310 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2311 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | int i; |
| 2314 | |
| 2315 | printk(KERN_INFO |
| 2316 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2317 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2318 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2319 | sb->ctime); |
| 2320 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2321 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2322 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2323 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2324 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2325 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2326 | sb->failed_disks, sb->spare_disks, |
| 2327 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2328 | |
| 2329 | printk(KERN_INFO); |
| 2330 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2331 | mdp_disk_t *desc; |
| 2332 | |
| 2333 | desc = sb->disks + i; |
| 2334 | if (desc->number || desc->major || desc->minor || |
| 2335 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2336 | printk(" D %2d: ", i); |
| 2337 | print_desc(desc); |
| 2338 | } |
| 2339 | } |
| 2340 | printk(KERN_INFO "md: THIS: "); |
| 2341 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2344 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2345 | { |
| 2346 | __u8 *uuid; |
| 2347 | |
| 2348 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2349 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2350 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2351 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2352 | le32_to_cpu(sb->major_version), |
| 2353 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2354 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2355 | sb->set_name, |
| 2356 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2357 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2358 | |
| 2359 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2360 | printk(KERN_INFO |
| 2361 | "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] | 2362 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2363 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2364 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2365 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2366 | le32_to_cpu(sb->level), |
| 2367 | (unsigned long long)le64_to_cpu(sb->size), |
| 2368 | le32_to_cpu(sb->raid_disks), |
| 2369 | le32_to_cpu(sb->layout), |
| 2370 | le32_to_cpu(sb->chunksize), |
| 2371 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2372 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2373 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2374 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2375 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2376 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2377 | sb->devflags, |
| 2378 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2379 | (unsigned long long)le64_to_cpu(sb->events), |
| 2380 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2381 | le32_to_cpu(sb->sb_csum), |
| 2382 | le32_to_cpu(sb->max_dev) |
| 2383 | ); |
| 2384 | } |
| 2385 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2386 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | { |
| 2388 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2389 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2390 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2391 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2392 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2394 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2395 | switch (major_version) { |
| 2396 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2397 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2398 | break; |
| 2399 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2400 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2401 | break; |
| 2402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | } else |
| 2404 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2405 | } |
| 2406 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2407 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2409 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2410 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2411 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | char b[BDEVNAME_SIZE]; |
| 2413 | |
| 2414 | printk("\n"); |
| 2415 | printk("md: **********************************\n"); |
| 2416 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2417 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2418 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2420 | if (mddev->bitmap) |
| 2421 | bitmap_print_sb(mddev->bitmap); |
| 2422 | else |
| 2423 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2424 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2426 | printk("\n"); |
| 2427 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2428 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2429 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | } |
| 2431 | printk("md: **********************************\n"); |
| 2432 | printk("\n"); |
| 2433 | } |
| 2434 | |
| 2435 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2436 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2438 | /* Update each superblock (in-memory image), but |
| 2439 | * if we are allowed to, skip spares which already |
| 2440 | * have the right event counter, or have one earlier |
| 2441 | * (which would mean they aren't being marked as dirty |
| 2442 | * with the rest of the array) |
| 2443 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2444 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2445 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2446 | if (rdev->sb_events == mddev->events || |
| 2447 | (nospares && |
| 2448 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2449 | rdev->sb_events+1 == mddev->events)) { |
| 2450 | /* Don't update this superblock */ |
| 2451 | rdev->sb_loaded = 2; |
| 2452 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2453 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2454 | rdev->sb_loaded = 1; |
| 2455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
| 2457 | } |
| 2458 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2459 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2461 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2462 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2463 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2464 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2467 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2468 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2469 | if (rdev->raid_disk >= 0 && |
| 2470 | mddev->delta_disks >= 0 && |
| 2471 | !test_bit(In_sync, &rdev->flags) && |
| 2472 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2473 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2474 | |
| 2475 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2476 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2477 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2478 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2479 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2480 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2481 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2482 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2483 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2484 | md_ack_all_badblocks(&rdev->badblocks); |
| 2485 | md_error(mddev, rdev); |
| 2486 | } |
| 2487 | clear_bit(Blocked, &rdev->flags); |
| 2488 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2489 | wake_up(&rdev->blocked_wait); |
| 2490 | } |
| 2491 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2492 | wake_up(&mddev->sb_wait); |
| 2493 | return; |
| 2494 | } |
| 2495 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2496 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2497 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2498 | mddev->utime = get_seconds(); |
| 2499 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2500 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2501 | force_change = 1; |
| 2502 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2503 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2504 | * though if events isn't the right even/odd, we will have to do |
| 2505 | * spares after all |
| 2506 | */ |
| 2507 | nospares = 1; |
| 2508 | if (force_change) |
| 2509 | nospares = 0; |
| 2510 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2511 | /* If the array is degraded, then skipping spares is both |
| 2512 | * dangerous and fairly pointless. |
| 2513 | * Dangerous because a device that was removed from the array |
| 2514 | * might have a event_count that still looks up-to-date, |
| 2515 | * so it can be re-added without a resync. |
| 2516 | * Pointless because if there are any spares to skip, |
| 2517 | * then a recovery will happen and soon that array won't |
| 2518 | * be degraded any more and the spare can go back to sleep then. |
| 2519 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2520 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2521 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2522 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2523 | |
| 2524 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2525 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2526 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2527 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2528 | && mddev->can_decrease_events |
| 2529 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2530 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2531 | mddev->can_decrease_events = 0; |
| 2532 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2533 | /* otherwise we have to go forward and ... */ |
| 2534 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2535 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | if (!mddev->events) { |
| 2539 | /* |
| 2540 | * oops, this 64-bit counter should never wrap. |
| 2541 | * Either we are in around ~1 trillion A.C., assuming |
| 2542 | * 1 reboot per second, or we have a bug: |
| 2543 | */ |
| 2544 | MD_BUG(); |
| 2545 | mddev->events --; |
| 2546 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2547 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2548 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2549 | if (rdev->badblocks.changed) |
| 2550 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2551 | if (test_bit(Faulty, &rdev->flags)) |
| 2552 | set_bit(FaultRecorded, &rdev->flags); |
| 2553 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2554 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2555 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2556 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2558 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2559 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2561 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2562 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2564 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2565 | if (rdev->sb_loaded != 1) |
| 2566 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2568 | if (!test_bit(Faulty, &rdev->flags) && |
| 2569 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2570 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2571 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2572 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2573 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2574 | bdevname(rdev->bdev, b), |
| 2575 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2576 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2577 | if (rdev->badblocks.size) { |
| 2578 | md_super_write(mddev, rdev, |
| 2579 | rdev->badblocks.sector, |
| 2580 | rdev->badblocks.size << 9, |
| 2581 | rdev->bb_page); |
| 2582 | rdev->badblocks.size = 0; |
| 2583 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2584 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2585 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2586 | pr_debug("md: %s (skipping faulty)\n", |
| 2587 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2588 | else |
| 2589 | pr_debug("(skipping incremental s/r "); |
| 2590 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2591 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | /* only need to write one superblock... */ |
| 2593 | break; |
| 2594 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2595 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2596 | /* 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] | 2597 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2598 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2599 | if (mddev->in_sync != sync_req || |
| 2600 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2601 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2602 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2603 | goto repeat; |
| 2604 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2605 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2606 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2607 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2608 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2609 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2610 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2611 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2612 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2613 | clear_bit(Blocked, &rdev->flags); |
| 2614 | |
| 2615 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2616 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2617 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2618 | wake_up(&rdev->blocked_wait); |
| 2619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2622 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2623 | * We want to accept with case. For this we use cmd_match. |
| 2624 | */ |
| 2625 | static int cmd_match(const char *cmd, const char *str) |
| 2626 | { |
| 2627 | /* See if cmd, written into a sysfs file, matches |
| 2628 | * str. They must either be the same, or cmd can |
| 2629 | * have a trailing newline |
| 2630 | */ |
| 2631 | while (*cmd && *str && *cmd == *str) { |
| 2632 | cmd++; |
| 2633 | str++; |
| 2634 | } |
| 2635 | if (*cmd == '\n') |
| 2636 | cmd++; |
| 2637 | if (*str || *cmd) |
| 2638 | return 0; |
| 2639 | return 1; |
| 2640 | } |
| 2641 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2642 | struct rdev_sysfs_entry { |
| 2643 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2644 | ssize_t (*show)(struct md_rdev *, char *); |
| 2645 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2646 | }; |
| 2647 | |
| 2648 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2649 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2650 | { |
| 2651 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2652 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2653 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2654 | if (test_bit(Faulty, &rdev->flags) || |
| 2655 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2656 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2657 | sep = ","; |
| 2658 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2659 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2660 | len += sprintf(page+len, "%sin_sync",sep); |
| 2661 | sep = ","; |
| 2662 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2663 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2664 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2665 | sep = ","; |
| 2666 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2667 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2668 | (rdev->badblocks.unacked_exist |
| 2669 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2670 | len += sprintf(page+len, "%sblocked", sep); |
| 2671 | sep = ","; |
| 2672 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2673 | if (!test_bit(Faulty, &rdev->flags) && |
| 2674 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2675 | len += sprintf(page+len, "%sspare", sep); |
| 2676 | sep = ","; |
| 2677 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2678 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2679 | len += sprintf(page+len, "%swrite_error", sep); |
| 2680 | sep = ","; |
| 2681 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2682 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2683 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2684 | sep = ","; |
| 2685 | } |
| 2686 | if (test_bit(Replacement, &rdev->flags)) { |
| 2687 | len += sprintf(page+len, "%sreplacement", sep); |
| 2688 | sep = ","; |
| 2689 | } |
| 2690 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2691 | return len+sprintf(page+len, "\n"); |
| 2692 | } |
| 2693 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2694 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2695 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2696 | { |
| 2697 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2698 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2699 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2700 | * writemostly - sets write_mostly |
| 2701 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2702 | * blocked - sets the Blocked flags |
| 2703 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2704 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2705 | * write_error - sets WriteErrorSeen |
| 2706 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2707 | */ |
| 2708 | int err = -EINVAL; |
| 2709 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2710 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2711 | if (test_bit(Faulty, &rdev->flags)) |
| 2712 | err = 0; |
| 2713 | else |
| 2714 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2715 | } else if (cmd_match(buf, "remove")) { |
| 2716 | if (rdev->raid_disk >= 0) |
| 2717 | err = -EBUSY; |
| 2718 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2719 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2720 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2721 | if (mddev->pers) |
| 2722 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2723 | md_new_event(mddev); |
| 2724 | err = 0; |
| 2725 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2726 | } else if (cmd_match(buf, "writemostly")) { |
| 2727 | set_bit(WriteMostly, &rdev->flags); |
| 2728 | err = 0; |
| 2729 | } else if (cmd_match(buf, "-writemostly")) { |
| 2730 | clear_bit(WriteMostly, &rdev->flags); |
| 2731 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2732 | } else if (cmd_match(buf, "blocked")) { |
| 2733 | set_bit(Blocked, &rdev->flags); |
| 2734 | err = 0; |
| 2735 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2736 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2737 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2738 | /* metadata handler doesn't understand badblocks, |
| 2739 | * so we need to fail the device |
| 2740 | */ |
| 2741 | md_error(rdev->mddev, rdev); |
| 2742 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2743 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2744 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2745 | wake_up(&rdev->blocked_wait); |
| 2746 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2747 | md_wakeup_thread(rdev->mddev->thread); |
| 2748 | |
| 2749 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2750 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2751 | set_bit(In_sync, &rdev->flags); |
| 2752 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2753 | } else if (cmd_match(buf, "write_error")) { |
| 2754 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2755 | err = 0; |
| 2756 | } else if (cmd_match(buf, "-write_error")) { |
| 2757 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2758 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2759 | } else if (cmd_match(buf, "want_replacement")) { |
| 2760 | /* Any non-spare device that is not a replacement can |
| 2761 | * become want_replacement at any time, but we then need to |
| 2762 | * check if recovery is needed. |
| 2763 | */ |
| 2764 | if (rdev->raid_disk >= 0 && |
| 2765 | !test_bit(Replacement, &rdev->flags)) |
| 2766 | set_bit(WantReplacement, &rdev->flags); |
| 2767 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2768 | md_wakeup_thread(rdev->mddev->thread); |
| 2769 | err = 0; |
| 2770 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2771 | /* Clearing 'want_replacement' is always allowed. |
| 2772 | * Once replacements starts it is too late though. |
| 2773 | */ |
| 2774 | err = 0; |
| 2775 | clear_bit(WantReplacement, &rdev->flags); |
| 2776 | } else if (cmd_match(buf, "replacement")) { |
| 2777 | /* Can only set a device as a replacement when array has not |
| 2778 | * yet been started. Once running, replacement is automatic |
| 2779 | * from spares, or by assigning 'slot'. |
| 2780 | */ |
| 2781 | if (rdev->mddev->pers) |
| 2782 | err = -EBUSY; |
| 2783 | else { |
| 2784 | set_bit(Replacement, &rdev->flags); |
| 2785 | err = 0; |
| 2786 | } |
| 2787 | } else if (cmd_match(buf, "-replacement")) { |
| 2788 | /* Similarly, can only clear Replacement before start */ |
| 2789 | if (rdev->mddev->pers) |
| 2790 | err = -EBUSY; |
| 2791 | else { |
| 2792 | clear_bit(Replacement, &rdev->flags); |
| 2793 | err = 0; |
| 2794 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2795 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2796 | if (!err) |
| 2797 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2798 | return err ? err : len; |
| 2799 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2800 | static struct rdev_sysfs_entry rdev_state = |
| 2801 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2802 | |
| 2803 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2804 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2805 | { |
| 2806 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2807 | } |
| 2808 | |
| 2809 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2810 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2811 | { |
| 2812 | char *e; |
| 2813 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2814 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2815 | atomic_set(&rdev->corrected_errors, n); |
| 2816 | return len; |
| 2817 | } |
| 2818 | return -EINVAL; |
| 2819 | } |
| 2820 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2821 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2822 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2823 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2824 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2825 | { |
| 2826 | if (rdev->raid_disk < 0) |
| 2827 | return sprintf(page, "none\n"); |
| 2828 | else |
| 2829 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2830 | } |
| 2831 | |
| 2832 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2833 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2834 | { |
| 2835 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2836 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2837 | int slot = simple_strtoul(buf, &e, 10); |
| 2838 | if (strncmp(buf, "none", 4)==0) |
| 2839 | slot = -1; |
| 2840 | else if (e==buf || (*e && *e!= '\n')) |
| 2841 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2842 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2843 | /* Setting 'slot' on an active array requires also |
| 2844 | * updating the 'rd%d' link, and communicating |
| 2845 | * with the personality with ->hot_*_disk. |
| 2846 | * For now we only support removing |
| 2847 | * failed/spare devices. This normally happens automatically, |
| 2848 | * but not when the metadata is externally managed. |
| 2849 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2850 | if (rdev->raid_disk == -1) |
| 2851 | return -EEXIST; |
| 2852 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2853 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2854 | return -EINVAL; |
| 2855 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2856 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2857 | if (err) |
| 2858 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2859 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2860 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2861 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2862 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2863 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2864 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2865 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2866 | */ |
| 2867 | |
| 2868 | if (rdev->raid_disk != -1) |
| 2869 | return -EBUSY; |
| 2870 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2871 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2872 | return -EBUSY; |
| 2873 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2874 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2875 | return -EINVAL; |
| 2876 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2877 | if (slot >= rdev->mddev->raid_disks && |
| 2878 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2879 | return -ENOSPC; |
| 2880 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2881 | rdev->raid_disk = slot; |
| 2882 | if (test_bit(In_sync, &rdev->flags)) |
| 2883 | rdev->saved_raid_disk = slot; |
| 2884 | else |
| 2885 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2886 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2887 | err = rdev->mddev->pers-> |
| 2888 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2889 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2890 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2891 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2892 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2893 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2894 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2895 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2896 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2897 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2898 | if (slot >= rdev->mddev->raid_disks && |
| 2899 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2900 | return -ENOSPC; |
| 2901 | rdev->raid_disk = slot; |
| 2902 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2903 | clear_bit(Faulty, &rdev->flags); |
| 2904 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2905 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2906 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2907 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2908 | return len; |
| 2909 | } |
| 2910 | |
| 2911 | |
| 2912 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2913 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2914 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2915 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2916 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2917 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2918 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2922 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2923 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2924 | unsigned long long offset; |
| 2925 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2926 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2927 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2928 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2929 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2930 | /* Must set offset before size, so overlap checks |
| 2931 | * can be sane */ |
| 2932 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2933 | rdev->data_offset = offset; |
| 2934 | return len; |
| 2935 | } |
| 2936 | |
| 2937 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2938 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2939 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2940 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2941 | { |
| 2942 | return sprintf(page, "%llu\n", |
| 2943 | (unsigned long long)rdev->new_data_offset); |
| 2944 | } |
| 2945 | |
| 2946 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2947 | const char *buf, size_t len) |
| 2948 | { |
| 2949 | unsigned long long new_offset; |
| 2950 | struct mddev *mddev = rdev->mddev; |
| 2951 | |
| 2952 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2953 | return -EINVAL; |
| 2954 | |
| 2955 | if (mddev->sync_thread) |
| 2956 | return -EBUSY; |
| 2957 | if (new_offset == rdev->data_offset) |
| 2958 | /* reset is always permitted */ |
| 2959 | ; |
| 2960 | else if (new_offset > rdev->data_offset) { |
| 2961 | /* must not push array size beyond rdev_sectors */ |
| 2962 | if (new_offset - rdev->data_offset |
| 2963 | + mddev->dev_sectors > rdev->sectors) |
| 2964 | return -E2BIG; |
| 2965 | } |
| 2966 | /* Metadata worries about other space details. */ |
| 2967 | |
| 2968 | /* decreasing the offset is inconsistent with a backwards |
| 2969 | * reshape. |
| 2970 | */ |
| 2971 | if (new_offset < rdev->data_offset && |
| 2972 | mddev->reshape_backwards) |
| 2973 | return -EINVAL; |
| 2974 | /* Increasing offset is inconsistent with forwards |
| 2975 | * reshape. reshape_direction should be set to |
| 2976 | * 'backwards' first. |
| 2977 | */ |
| 2978 | if (new_offset > rdev->data_offset && |
| 2979 | !mddev->reshape_backwards) |
| 2980 | return -EINVAL; |
| 2981 | |
| 2982 | if (mddev->pers && mddev->persistent && |
| 2983 | !super_types[mddev->major_version] |
| 2984 | .allow_new_offset(rdev, new_offset)) |
| 2985 | return -E2BIG; |
| 2986 | rdev->new_data_offset = new_offset; |
| 2987 | if (new_offset > rdev->data_offset) |
| 2988 | mddev->reshape_backwards = 1; |
| 2989 | else if (new_offset < rdev->data_offset) |
| 2990 | mddev->reshape_backwards = 0; |
| 2991 | |
| 2992 | return len; |
| 2993 | } |
| 2994 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2995 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2996 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2997 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2998 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2999 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3000 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3001 | } |
| 3002 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3003 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3004 | { |
| 3005 | /* check if two start/length pairs overlap */ |
| 3006 | if (s1+l1 <= s2) |
| 3007 | return 0; |
| 3008 | if (s2+l2 <= s1) |
| 3009 | return 0; |
| 3010 | return 1; |
| 3011 | } |
| 3012 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3013 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3014 | { |
| 3015 | unsigned long long blocks; |
| 3016 | sector_t new; |
| 3017 | |
| 3018 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 3019 | return -EINVAL; |
| 3020 | |
| 3021 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3022 | return -EINVAL; /* sector conversion overflow */ |
| 3023 | |
| 3024 | new = blocks * 2; |
| 3025 | if (new != blocks * 2) |
| 3026 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3027 | |
| 3028 | *sectors = new; |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3032 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3033 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3034 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3035 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3036 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3037 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3038 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3039 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3040 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3041 | if (rdev->data_offset != rdev->new_data_offset) |
| 3042 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3043 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3044 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3045 | sectors = super_types[my_mddev->major_version]. |
| 3046 | rdev_size_change(rdev, sectors); |
| 3047 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3048 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3049 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3050 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3051 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3052 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3053 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3054 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3055 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3056 | rdev->sectors = sectors; |
| 3057 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3058 | /* need to check that all other rdevs with the same ->bdev |
| 3059 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3060 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3061 | * we have to change it back, we will have the lock again. |
| 3062 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3063 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3064 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3065 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3066 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3067 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3068 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3069 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3070 | |
| 3071 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3072 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3073 | if (rdev->bdev == rdev2->bdev && |
| 3074 | rdev != rdev2 && |
| 3075 | overlaps(rdev->data_offset, rdev->sectors, |
| 3076 | rdev2->data_offset, |
| 3077 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3078 | overlap = 1; |
| 3079 | break; |
| 3080 | } |
| 3081 | mddev_unlock(mddev); |
| 3082 | if (overlap) { |
| 3083 | mddev_put(mddev); |
| 3084 | break; |
| 3085 | } |
| 3086 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3087 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3088 | if (overlap) { |
| 3089 | /* Someone else could have slipped in a size |
| 3090 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3091 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3092 | * safe, and trust userspace not to race with |
| 3093 | * itself |
| 3094 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3095 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3096 | return -EBUSY; |
| 3097 | } |
| 3098 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3099 | return len; |
| 3100 | } |
| 3101 | |
| 3102 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3103 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3104 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3105 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3106 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3107 | { |
| 3108 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3109 | |
| 3110 | if (test_bit(In_sync, &rdev->flags) || |
| 3111 | recovery_start == MaxSector) |
| 3112 | return sprintf(page, "none\n"); |
| 3113 | |
| 3114 | return sprintf(page, "%llu\n", recovery_start); |
| 3115 | } |
| 3116 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3117 | 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] | 3118 | { |
| 3119 | unsigned long long recovery_start; |
| 3120 | |
| 3121 | if (cmd_match(buf, "none")) |
| 3122 | recovery_start = MaxSector; |
| 3123 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3124 | return -EINVAL; |
| 3125 | |
| 3126 | if (rdev->mddev->pers && |
| 3127 | rdev->raid_disk >= 0) |
| 3128 | return -EBUSY; |
| 3129 | |
| 3130 | rdev->recovery_offset = recovery_start; |
| 3131 | if (recovery_start == MaxSector) |
| 3132 | set_bit(In_sync, &rdev->flags); |
| 3133 | else |
| 3134 | clear_bit(In_sync, &rdev->flags); |
| 3135 | return len; |
| 3136 | } |
| 3137 | |
| 3138 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3139 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3140 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3141 | |
| 3142 | static ssize_t |
| 3143 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3144 | static ssize_t |
| 3145 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3146 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3147 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3148 | { |
| 3149 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3150 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3151 | 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] | 3152 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3153 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3154 | /* Maybe that ack was all we needed */ |
| 3155 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3156 | wake_up(&rdev->blocked_wait); |
| 3157 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3158 | } |
| 3159 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3160 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3161 | |
| 3162 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3163 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3164 | { |
| 3165 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3166 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3167 | 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] | 3168 | { |
| 3169 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3170 | } |
| 3171 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3172 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3173 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3174 | static struct attribute *rdev_default_attrs[] = { |
| 3175 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3176 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3177 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3178 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3179 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3180 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3181 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3182 | &rdev_bad_blocks.attr, |
| 3183 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3184 | NULL, |
| 3185 | }; |
| 3186 | static ssize_t |
| 3187 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3188 | { |
| 3189 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3190 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3191 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3192 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3193 | |
| 3194 | if (!entry->show) |
| 3195 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3196 | |
| 3197 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3198 | if (!rv) { |
| 3199 | if (rdev->mddev == NULL) |
| 3200 | rv = -EBUSY; |
| 3201 | else |
| 3202 | rv = entry->show(rdev, page); |
| 3203 | mddev_unlock(mddev); |
| 3204 | } |
| 3205 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | static ssize_t |
| 3209 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3210 | const char *page, size_t length) |
| 3211 | { |
| 3212 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3213 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3214 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3215 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3216 | |
| 3217 | if (!entry->store) |
| 3218 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3219 | if (!capable(CAP_SYS_ADMIN)) |
| 3220 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3221 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3222 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3223 | if (rdev->mddev == NULL) |
| 3224 | rv = -EBUSY; |
| 3225 | else |
| 3226 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3227 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3228 | } |
| 3229 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | static void rdev_free(struct kobject *ko) |
| 3233 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3234 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3235 | kfree(rdev); |
| 3236 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3237 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3238 | .show = rdev_attr_show, |
| 3239 | .store = rdev_attr_store, |
| 3240 | }; |
| 3241 | static struct kobj_type rdev_ktype = { |
| 3242 | .release = rdev_free, |
| 3243 | .sysfs_ops = &rdev_sysfs_ops, |
| 3244 | .default_attrs = rdev_default_attrs, |
| 3245 | }; |
| 3246 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3247 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3248 | { |
| 3249 | rdev->desc_nr = -1; |
| 3250 | rdev->saved_raid_disk = -1; |
| 3251 | rdev->raid_disk = -1; |
| 3252 | rdev->flags = 0; |
| 3253 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3254 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3255 | rdev->sb_events = 0; |
| 3256 | rdev->last_read_error.tv_sec = 0; |
| 3257 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3258 | rdev->sb_loaded = 0; |
| 3259 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3260 | atomic_set(&rdev->nr_pending, 0); |
| 3261 | atomic_set(&rdev->read_errors, 0); |
| 3262 | atomic_set(&rdev->corrected_errors, 0); |
| 3263 | |
| 3264 | INIT_LIST_HEAD(&rdev->same_set); |
| 3265 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3266 | |
| 3267 | /* Add space to store bad block list. |
| 3268 | * This reserves the space even on arrays where it cannot |
| 3269 | * be used - I wonder if that matters |
| 3270 | */ |
| 3271 | rdev->badblocks.count = 0; |
| 3272 | rdev->badblocks.shift = 0; |
| 3273 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3274 | seqlock_init(&rdev->badblocks.lock); |
| 3275 | if (rdev->badblocks.page == NULL) |
| 3276 | return -ENOMEM; |
| 3277 | |
| 3278 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3279 | } |
| 3280 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | /* |
| 3282 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3283 | * |
| 3284 | * mark the device faulty if: |
| 3285 | * |
| 3286 | * - the device is nonexistent (zero size) |
| 3287 | * - the device has no valid superblock |
| 3288 | * |
| 3289 | * a faulty rdev _never_ has rdev->sb set. |
| 3290 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3291 | 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] | 3292 | { |
| 3293 | char b[BDEVNAME_SIZE]; |
| 3294 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3295 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | sector_t size; |
| 3297 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3298 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | if (!rdev) { |
| 3300 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3301 | return ERR_PTR(-ENOMEM); |
| 3302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3304 | err = md_rdev_init(rdev); |
| 3305 | if (err) |
| 3306 | goto abort_free; |
| 3307 | err = alloc_disk_sb(rdev); |
| 3308 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | goto abort_free; |
| 3310 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3311 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | if (err) |
| 3313 | goto abort_free; |
| 3314 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3315 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3316 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3317 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | if (!size) { |
| 3319 | printk(KERN_WARNING |
| 3320 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3321 | bdevname(rdev->bdev,b)); |
| 3322 | err = -EINVAL; |
| 3323 | goto abort_free; |
| 3324 | } |
| 3325 | |
| 3326 | if (super_format >= 0) { |
| 3327 | err = super_types[super_format]. |
| 3328 | load_super(rdev, NULL, super_minor); |
| 3329 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3330 | printk(KERN_WARNING |
| 3331 | "md: %s does not have a valid v%d.%d " |
| 3332 | "superblock, not importing!\n", |
| 3333 | bdevname(rdev->bdev,b), |
| 3334 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | goto abort_free; |
| 3336 | } |
| 3337 | if (err < 0) { |
| 3338 | printk(KERN_WARNING |
| 3339 | "md: could not read %s's sb, not importing!\n", |
| 3340 | bdevname(rdev->bdev,b)); |
| 3341 | goto abort_free; |
| 3342 | } |
| 3343 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3344 | if (super_format == -1) |
| 3345 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3346 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | return rdev; |
| 3349 | |
| 3350 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3351 | if (rdev->bdev) |
| 3352 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3353 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | kfree(rdev); |
| 3355 | return ERR_PTR(err); |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * Check a full RAID array for plausibility |
| 3360 | */ |
| 3361 | |
| 3362 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3363 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | { |
| 3365 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3366 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | char b[BDEVNAME_SIZE]; |
| 3368 | |
| 3369 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3370 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | switch (super_types[mddev->major_version]. |
| 3372 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3373 | case 1: |
| 3374 | freshest = rdev; |
| 3375 | break; |
| 3376 | case 0: |
| 3377 | break; |
| 3378 | default: |
| 3379 | printk( KERN_ERR \ |
| 3380 | "md: fatal superblock inconsistency in %s" |
| 3381 | " -- removing from array\n", |
| 3382 | bdevname(rdev->bdev,b)); |
| 3383 | kick_rdev_from_array(rdev); |
| 3384 | } |
| 3385 | |
| 3386 | |
| 3387 | super_types[mddev->major_version]. |
| 3388 | validate_super(mddev, freshest); |
| 3389 | |
| 3390 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3391 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3392 | if (mddev->max_disks && |
| 3393 | (rdev->desc_nr >= mddev->max_disks || |
| 3394 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3395 | printk(KERN_WARNING |
| 3396 | "md: %s: %s: only %d devices permitted\n", |
| 3397 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3398 | mddev->max_disks); |
| 3399 | kick_rdev_from_array(rdev); |
| 3400 | continue; |
| 3401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | if (rdev != freshest) |
| 3403 | if (super_types[mddev->major_version]. |
| 3404 | validate_super(mddev, rdev)) { |
| 3405 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3406 | " from array!\n", |
| 3407 | bdevname(rdev->bdev,b)); |
| 3408 | kick_rdev_from_array(rdev); |
| 3409 | continue; |
| 3410 | } |
| 3411 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3412 | rdev->desc_nr = i++; |
| 3413 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3414 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3415 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3416 | rdev->raid_disk = -1; |
| 3417 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | } |
| 3419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | } |
| 3421 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3422 | /* Read a fixed-point number. |
| 3423 | * Numbers in sysfs attributes should be in "standard" units where |
| 3424 | * possible, so time should be in seconds. |
| 3425 | * However we internally use a a much smaller unit such as |
| 3426 | * milliseconds or jiffies. |
| 3427 | * This function takes a decimal number with a possible fractional |
| 3428 | * component, and produces an integer which is the result of |
| 3429 | * multiplying that number by 10^'scale'. |
| 3430 | * all without any floating-point arithmetic. |
| 3431 | */ |
| 3432 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3433 | { |
| 3434 | unsigned long result = 0; |
| 3435 | long decimals = -1; |
| 3436 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3437 | if (*cp == '.') |
| 3438 | decimals = 0; |
| 3439 | else if (decimals < scale) { |
| 3440 | unsigned int value; |
| 3441 | value = *cp - '0'; |
| 3442 | result = result * 10 + value; |
| 3443 | if (decimals >= 0) |
| 3444 | decimals++; |
| 3445 | } |
| 3446 | cp++; |
| 3447 | } |
| 3448 | if (*cp == '\n') |
| 3449 | cp++; |
| 3450 | if (*cp) |
| 3451 | return -EINVAL; |
| 3452 | if (decimals < 0) |
| 3453 | decimals = 0; |
| 3454 | while (decimals < scale) { |
| 3455 | result *= 10; |
| 3456 | decimals ++; |
| 3457 | } |
| 3458 | *res = result; |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
| 3462 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3463 | static void md_safemode_timeout(unsigned long data); |
| 3464 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3465 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3466 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3467 | { |
| 3468 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3469 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3470 | } |
| 3471 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3472 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3473 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3474 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3475 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3476 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3477 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3478 | if (msec == 0) |
| 3479 | mddev->safemode_delay = 0; |
| 3480 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3481 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3482 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3483 | if (mddev->safemode_delay == 0) |
| 3484 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3485 | if (mddev->safemode_delay < old_delay) |
| 3486 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3487 | } |
| 3488 | return len; |
| 3489 | } |
| 3490 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3491 | __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] | 3492 | |
| 3493 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3494 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3495 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3496 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3497 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3498 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3499 | else if (mddev->clevel[0]) |
| 3500 | return sprintf(page, "%s\n", mddev->clevel); |
| 3501 | else if (mddev->level != LEVEL_NONE) |
| 3502 | return sprintf(page, "%d\n", mddev->level); |
| 3503 | else |
| 3504 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3505 | } |
| 3506 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3507 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3508 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3509 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3510 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3511 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3512 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3513 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3514 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3515 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3516 | |
| 3517 | if (mddev->pers == NULL) { |
| 3518 | if (len == 0) |
| 3519 | return 0; |
| 3520 | if (len >= sizeof(mddev->clevel)) |
| 3521 | return -ENOSPC; |
| 3522 | strncpy(mddev->clevel, buf, len); |
| 3523 | if (mddev->clevel[len-1] == '\n') |
| 3524 | len--; |
| 3525 | mddev->clevel[len] = 0; |
| 3526 | mddev->level = LEVEL_NONE; |
| 3527 | return rv; |
| 3528 | } |
| 3529 | |
| 3530 | /* request to change the personality. Need to ensure: |
| 3531 | * - array is not engaged in resync/recovery/reshape |
| 3532 | * - old personality can be suspended |
| 3533 | * - new personality will access other array. |
| 3534 | */ |
| 3535 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3536 | if (mddev->sync_thread || |
| 3537 | mddev->reshape_position != MaxSector || |
| 3538 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3539 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3540 | |
| 3541 | if (!mddev->pers->quiesce) { |
| 3542 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3543 | mdname(mddev), mddev->pers->name); |
| 3544 | return -EINVAL; |
| 3545 | } |
| 3546 | |
| 3547 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3548 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3549 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3550 | strncpy(clevel, buf, len); |
| 3551 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3552 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3553 | clevel[len] = 0; |
| 3554 | if (strict_strtol(clevel, 10, &level)) |
| 3555 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3556 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3557 | if (request_module("md-%s", clevel) != 0) |
| 3558 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3559 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3560 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3561 | if (!pers || !try_module_get(pers->owner)) { |
| 3562 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3563 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3564 | return -EINVAL; |
| 3565 | } |
| 3566 | spin_unlock(&pers_lock); |
| 3567 | |
| 3568 | if (pers == mddev->pers) { |
| 3569 | /* Nothing to do! */ |
| 3570 | module_put(pers->owner); |
| 3571 | return rv; |
| 3572 | } |
| 3573 | if (!pers->takeover) { |
| 3574 | module_put(pers->owner); |
| 3575 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3576 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3577 | return -EINVAL; |
| 3578 | } |
| 3579 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3580 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3581 | rdev->new_raid_disk = rdev->raid_disk; |
| 3582 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3583 | /* ->takeover must set new_* and/or delta_disks |
| 3584 | * if it succeeds, and may set them when it fails. |
| 3585 | */ |
| 3586 | priv = pers->takeover(mddev); |
| 3587 | if (IS_ERR(priv)) { |
| 3588 | mddev->new_level = mddev->level; |
| 3589 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3590 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3591 | mddev->raid_disks -= mddev->delta_disks; |
| 3592 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3593 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3594 | module_put(pers->owner); |
| 3595 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3596 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3597 | return PTR_ERR(priv); |
| 3598 | } |
| 3599 | |
| 3600 | /* Looks like we have a winner */ |
| 3601 | mddev_suspend(mddev); |
| 3602 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3603 | |
| 3604 | if (mddev->pers->sync_request == NULL && |
| 3605 | pers->sync_request != NULL) { |
| 3606 | /* need to add the md_redundancy_group */ |
| 3607 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3608 | printk(KERN_WARNING |
| 3609 | "md: cannot register extra attributes for %s\n", |
| 3610 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3611 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3612 | } |
| 3613 | if (mddev->pers->sync_request != NULL && |
| 3614 | pers->sync_request == NULL) { |
| 3615 | /* need to remove the md_redundancy_group */ |
| 3616 | if (mddev->to_remove == NULL) |
| 3617 | mddev->to_remove = &md_redundancy_group; |
| 3618 | } |
| 3619 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3620 | if (mddev->pers->sync_request == NULL && |
| 3621 | mddev->external) { |
| 3622 | /* We are converting from a no-redundancy array |
| 3623 | * to a redundancy array and metadata is managed |
| 3624 | * externally so we need to be sure that writes |
| 3625 | * won't block due to a need to transition |
| 3626 | * clean->dirty |
| 3627 | * until external management is started. |
| 3628 | */ |
| 3629 | mddev->in_sync = 0; |
| 3630 | mddev->safemode_delay = 0; |
| 3631 | mddev->safemode = 0; |
| 3632 | } |
| 3633 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3634 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3635 | if (rdev->raid_disk < 0) |
| 3636 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3637 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3638 | rdev->new_raid_disk = -1; |
| 3639 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3640 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3641 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3642 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3643 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3644 | if (rdev->raid_disk < 0) |
| 3645 | continue; |
| 3646 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3647 | continue; |
| 3648 | rdev->raid_disk = rdev->new_raid_disk; |
| 3649 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3650 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3651 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3652 | if (sysfs_link_rdev(mddev, rdev)) |
| 3653 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3654 | " for %s after level change\n", |
| 3655 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3656 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3660 | mddev->pers = pers; |
| 3661 | mddev->private = priv; |
| 3662 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3663 | mddev->level = mddev->new_level; |
| 3664 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3665 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3666 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3667 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3668 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3669 | if (mddev->pers->sync_request == NULL) { |
| 3670 | /* this is now an array without redundancy, so |
| 3671 | * it must always be in_sync |
| 3672 | */ |
| 3673 | mddev->in_sync = 1; |
| 3674 | del_timer_sync(&mddev->safemode_timer); |
| 3675 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3676 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3677 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3678 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3679 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3680 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3681 | return rv; |
| 3682 | } |
| 3683 | |
| 3684 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3685 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3686 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3687 | |
| 3688 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3689 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3690 | { |
| 3691 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3692 | if (mddev->reshape_position != MaxSector && |
| 3693 | mddev->layout != mddev->new_layout) |
| 3694 | return sprintf(page, "%d (%d)\n", |
| 3695 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3696 | return sprintf(page, "%d\n", mddev->layout); |
| 3697 | } |
| 3698 | |
| 3699 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3700 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3701 | { |
| 3702 | char *e; |
| 3703 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3704 | |
| 3705 | if (!*buf || (*e && *e != '\n')) |
| 3706 | return -EINVAL; |
| 3707 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3708 | if (mddev->pers) { |
| 3709 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3710 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3711 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3712 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3713 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3714 | if (err) { |
| 3715 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3716 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3717 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3718 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3719 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3720 | if (mddev->reshape_position == MaxSector) |
| 3721 | mddev->layout = n; |
| 3722 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3723 | return len; |
| 3724 | } |
| 3725 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3726 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3727 | |
| 3728 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3729 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3730 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3731 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3732 | if (mddev->raid_disks == 0) |
| 3733 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3734 | if (mddev->reshape_position != MaxSector && |
| 3735 | mddev->delta_disks != 0) |
| 3736 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3737 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3738 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3739 | } |
| 3740 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3741 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3745 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3746 | char *e; |
| 3747 | int rv = 0; |
| 3748 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3749 | |
| 3750 | if (!*buf || (*e && *e != '\n')) |
| 3751 | return -EINVAL; |
| 3752 | |
| 3753 | if (mddev->pers) |
| 3754 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3755 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3756 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3757 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3758 | |
| 3759 | rdev_for_each(rdev, mddev) { |
| 3760 | if (olddisks < n && |
| 3761 | rdev->data_offset < rdev->new_data_offset) |
| 3762 | return -EINVAL; |
| 3763 | if (olddisks > n && |
| 3764 | rdev->data_offset > rdev->new_data_offset) |
| 3765 | return -EINVAL; |
| 3766 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3767 | mddev->delta_disks = n - olddisks; |
| 3768 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3769 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3770 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3771 | mddev->raid_disks = n; |
| 3772 | return rv ? rv : len; |
| 3773 | } |
| 3774 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3775 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3776 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3777 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3778 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3779 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3780 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3781 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3782 | return sprintf(page, "%d (%d)\n", |
| 3783 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3784 | mddev->chunk_sectors << 9); |
| 3785 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
| 3788 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3789 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3790 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3791 | char *e; |
| 3792 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3793 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3794 | if (!*buf || (*e && *e != '\n')) |
| 3795 | return -EINVAL; |
| 3796 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3797 | if (mddev->pers) { |
| 3798 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3799 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3800 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3801 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3802 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3803 | if (err) { |
| 3804 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3805 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3806 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3807 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3808 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3809 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3810 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3811 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3812 | return len; |
| 3813 | } |
| 3814 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3815 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3816 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3817 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3818 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3819 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3820 | if (mddev->recovery_cp == MaxSector) |
| 3821 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3822 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3823 | } |
| 3824 | |
| 3825 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3826 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3827 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3828 | char *e; |
| 3829 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3830 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3831 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3832 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3833 | if (cmd_match(buf, "none")) |
| 3834 | n = MaxSector; |
| 3835 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3836 | return -EINVAL; |
| 3837 | |
| 3838 | mddev->recovery_cp = n; |
| 3839 | return len; |
| 3840 | } |
| 3841 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3842 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3843 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3844 | /* |
| 3845 | * The array state can be: |
| 3846 | * |
| 3847 | * clear |
| 3848 | * No devices, no size, no level |
| 3849 | * Equivalent to STOP_ARRAY ioctl |
| 3850 | * inactive |
| 3851 | * May have some settings, but array is not active |
| 3852 | * all IO results in error |
| 3853 | * When written, doesn't tear down array, but just stops it |
| 3854 | * suspended (not supported yet) |
| 3855 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3856 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3857 | * readonly |
| 3858 | * no resync can happen. no superblocks get written. |
| 3859 | * write requests fail |
| 3860 | * read-auto |
| 3861 | * like readonly, but behaves like 'clean' on a write request. |
| 3862 | * |
| 3863 | * clean - no pending writes, but otherwise active. |
| 3864 | * When written to inactive array, starts without resync |
| 3865 | * If a write request arrives then |
| 3866 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3867 | * if not known, block and switch to write-pending |
| 3868 | * If written to an active array that has pending writes, then fails. |
| 3869 | * active |
| 3870 | * fully active: IO and resync can be happening. |
| 3871 | * When written to inactive array, starts with resync |
| 3872 | * |
| 3873 | * write-pending |
| 3874 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3875 | * |
| 3876 | * active-idle |
| 3877 | * like active, but no writes have been seen for a while (100msec). |
| 3878 | * |
| 3879 | */ |
| 3880 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3881 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3882 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3883 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3884 | "write-pending", "active-idle", NULL }; |
| 3885 | |
| 3886 | static int match_word(const char *word, char **list) |
| 3887 | { |
| 3888 | int n; |
| 3889 | for (n=0; list[n]; n++) |
| 3890 | if (cmd_match(word, list[n])) |
| 3891 | break; |
| 3892 | return n; |
| 3893 | } |
| 3894 | |
| 3895 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3896 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3897 | { |
| 3898 | enum array_state st = inactive; |
| 3899 | |
| 3900 | if (mddev->pers) |
| 3901 | switch(mddev->ro) { |
| 3902 | case 1: |
| 3903 | st = readonly; |
| 3904 | break; |
| 3905 | case 2: |
| 3906 | st = read_auto; |
| 3907 | break; |
| 3908 | case 0: |
| 3909 | if (mddev->in_sync) |
| 3910 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3911 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3912 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3913 | else if (mddev->safemode) |
| 3914 | st = active_idle; |
| 3915 | else |
| 3916 | st = active; |
| 3917 | } |
| 3918 | else { |
| 3919 | if (list_empty(&mddev->disks) && |
| 3920 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3921 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3922 | st = clear; |
| 3923 | else |
| 3924 | st = inactive; |
| 3925 | } |
| 3926 | return sprintf(page, "%s\n", array_states[st]); |
| 3927 | } |
| 3928 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3929 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3930 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3931 | static int do_md_run(struct mddev * mddev); |
| 3932 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3933 | |
| 3934 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3935 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3936 | { |
| 3937 | int err = -EINVAL; |
| 3938 | enum array_state st = match_word(buf, array_states); |
| 3939 | switch(st) { |
| 3940 | case bad_word: |
| 3941 | break; |
| 3942 | case clear: |
| 3943 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3944 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3945 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3946 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3947 | break; |
| 3948 | case inactive: |
| 3949 | /* stopping an active array */ |
| 3950 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3951 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3952 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3953 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3954 | } else |
| 3955 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3956 | break; |
| 3957 | case suspended: |
| 3958 | break; /* not supported yet */ |
| 3959 | case readonly: |
| 3960 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3961 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3962 | else { |
| 3963 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3964 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3965 | err = do_md_run(mddev); |
| 3966 | } |
| 3967 | break; |
| 3968 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3969 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3970 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3971 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3972 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3973 | err = restart_array(mddev); |
| 3974 | if (err == 0) { |
| 3975 | mddev->ro = 2; |
| 3976 | set_disk_ro(mddev->gendisk, 0); |
| 3977 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3978 | } else { |
| 3979 | mddev->ro = 2; |
| 3980 | err = do_md_run(mddev); |
| 3981 | } |
| 3982 | break; |
| 3983 | case clean: |
| 3984 | if (mddev->pers) { |
| 3985 | restart_array(mddev); |
| 3986 | spin_lock_irq(&mddev->write_lock); |
| 3987 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3988 | if (mddev->in_sync == 0) { |
| 3989 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3990 | if (mddev->safemode == 1) |
| 3991 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3992 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3993 | } |
| 3994 | err = 0; |
| 3995 | } else |
| 3996 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3997 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3998 | } else |
| 3999 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4000 | break; |
| 4001 | case active: |
| 4002 | if (mddev->pers) { |
| 4003 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4004 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4005 | wake_up(&mddev->sb_wait); |
| 4006 | err = 0; |
| 4007 | } else { |
| 4008 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4009 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4010 | err = do_md_run(mddev); |
| 4011 | } |
| 4012 | break; |
| 4013 | case write_pending: |
| 4014 | case active_idle: |
| 4015 | /* these cannot be set */ |
| 4016 | break; |
| 4017 | } |
| 4018 | if (err) |
| 4019 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4020 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4021 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4022 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4023 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4024 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4025 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4026 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4027 | static struct md_sysfs_entry md_array_state = |
| 4028 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4029 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4030 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4031 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4032 | return sprintf(page, "%d\n", |
| 4033 | atomic_read(&mddev->max_corr_read_errors)); |
| 4034 | } |
| 4035 | |
| 4036 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4037 | 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] | 4038 | { |
| 4039 | char *e; |
| 4040 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 4041 | |
| 4042 | if (*buf && (*e == 0 || *e == '\n')) { |
| 4043 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4044 | return len; |
| 4045 | } |
| 4046 | return -EINVAL; |
| 4047 | } |
| 4048 | |
| 4049 | static struct md_sysfs_entry max_corr_read_errors = |
| 4050 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4051 | max_corrected_read_errors_store); |
| 4052 | |
| 4053 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4054 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4055 | { |
| 4056 | return -EINVAL; |
| 4057 | } |
| 4058 | |
| 4059 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4060 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4061 | { |
| 4062 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4063 | /* The new device is added to the array. |
| 4064 | * If the array has a persistent superblock, we read the |
| 4065 | * superblock to initialise info and check validity. |
| 4066 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4067 | * which mainly checks size. |
| 4068 | */ |
| 4069 | char *e; |
| 4070 | int major = simple_strtoul(buf, &e, 10); |
| 4071 | int minor; |
| 4072 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4073 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4074 | int err; |
| 4075 | |
| 4076 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4077 | return -EINVAL; |
| 4078 | minor = simple_strtoul(e+1, &e, 10); |
| 4079 | if (*e && *e != '\n') |
| 4080 | return -EINVAL; |
| 4081 | dev = MKDEV(major, minor); |
| 4082 | if (major != MAJOR(dev) || |
| 4083 | minor != MINOR(dev)) |
| 4084 | return -EOVERFLOW; |
| 4085 | |
| 4086 | |
| 4087 | if (mddev->persistent) { |
| 4088 | rdev = md_import_device(dev, mddev->major_version, |
| 4089 | mddev->minor_version); |
| 4090 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4091 | struct md_rdev *rdev0 |
| 4092 | = list_entry(mddev->disks.next, |
| 4093 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4094 | err = super_types[mddev->major_version] |
| 4095 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4096 | if (err < 0) |
| 4097 | goto out; |
| 4098 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4099 | } else if (mddev->external) |
| 4100 | rdev = md_import_device(dev, -2, -1); |
| 4101 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4102 | rdev = md_import_device(dev, -1, -1); |
| 4103 | |
| 4104 | if (IS_ERR(rdev)) |
| 4105 | return PTR_ERR(rdev); |
| 4106 | err = bind_rdev_to_array(rdev, mddev); |
| 4107 | out: |
| 4108 | if (err) |
| 4109 | export_rdev(rdev); |
| 4110 | return err ? err : len; |
| 4111 | } |
| 4112 | |
| 4113 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4114 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4115 | |
| 4116 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4117 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4118 | { |
| 4119 | char *end; |
| 4120 | unsigned long chunk, end_chunk; |
| 4121 | |
| 4122 | if (!mddev->bitmap) |
| 4123 | goto out; |
| 4124 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4125 | while (*buf) { |
| 4126 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4127 | if (buf == end) break; |
| 4128 | if (*end == '-') { /* range */ |
| 4129 | buf = end + 1; |
| 4130 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4131 | if (buf == end) break; |
| 4132 | } |
| 4133 | if (*end && !isspace(*end)) break; |
| 4134 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4135 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4136 | } |
| 4137 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4138 | out: |
| 4139 | return len; |
| 4140 | } |
| 4141 | |
| 4142 | static struct md_sysfs_entry md_bitmap = |
| 4143 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4144 | |
| 4145 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4146 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4147 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4148 | return sprintf(page, "%llu\n", |
| 4149 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4150 | } |
| 4151 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4152 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4153 | |
| 4154 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4155 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4156 | { |
| 4157 | /* If array is inactive, we can reduce the component size, but |
| 4158 | * not increase it (except from 0). |
| 4159 | * If array is active, we can try an on-line resize |
| 4160 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4161 | sector_t sectors; |
| 4162 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4163 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4164 | if (err < 0) |
| 4165 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4166 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4167 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4168 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4169 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4170 | if (mddev->dev_sectors == 0 || |
| 4171 | mddev->dev_sectors > sectors) |
| 4172 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4173 | else |
| 4174 | err = -ENOSPC; |
| 4175 | } |
| 4176 | return err ? err : len; |
| 4177 | } |
| 4178 | |
| 4179 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4180 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4181 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4182 | |
| 4183 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4184 | * This is one of |
| 4185 | * 'none' for arrays with no metadata (good luck...) |
| 4186 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4187 | * or N.M for internally known formats |
| 4188 | */ |
| 4189 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4190 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4191 | { |
| 4192 | if (mddev->persistent) |
| 4193 | return sprintf(page, "%d.%d\n", |
| 4194 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4195 | else if (mddev->external) |
| 4196 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4197 | else |
| 4198 | return sprintf(page, "none\n"); |
| 4199 | } |
| 4200 | |
| 4201 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4202 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4203 | { |
| 4204 | int major, minor; |
| 4205 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4206 | /* Changing the details of 'external' metadata is |
| 4207 | * always permitted. Otherwise there must be |
| 4208 | * no devices attached to the array. |
| 4209 | */ |
| 4210 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4211 | ; |
| 4212 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4213 | return -EBUSY; |
| 4214 | |
| 4215 | if (cmd_match(buf, "none")) { |
| 4216 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4217 | mddev->external = 0; |
| 4218 | mddev->major_version = 0; |
| 4219 | mddev->minor_version = 90; |
| 4220 | return len; |
| 4221 | } |
| 4222 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4223 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4224 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4225 | namelen = sizeof(mddev->metadata_type)-1; |
| 4226 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4227 | mddev->metadata_type[namelen] = 0; |
| 4228 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4229 | mddev->metadata_type[--namelen] = 0; |
| 4230 | mddev->persistent = 0; |
| 4231 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4232 | mddev->major_version = 0; |
| 4233 | mddev->minor_version = 90; |
| 4234 | return len; |
| 4235 | } |
| 4236 | major = simple_strtoul(buf, &e, 10); |
| 4237 | if (e==buf || *e != '.') |
| 4238 | return -EINVAL; |
| 4239 | buf = e+1; |
| 4240 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4241 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4242 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4243 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4244 | return -ENOENT; |
| 4245 | mddev->major_version = major; |
| 4246 | mddev->minor_version = minor; |
| 4247 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4248 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4249 | return len; |
| 4250 | } |
| 4251 | |
| 4252 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4253 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4254 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4255 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4256 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4257 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4258 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4259 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4260 | type = "frozen"; |
| 4261 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4262 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4263 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4264 | type = "reshape"; |
| 4265 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4266 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4267 | type = "resync"; |
| 4268 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4269 | type = "check"; |
| 4270 | else |
| 4271 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4272 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4273 | type = "recover"; |
| 4274 | } |
| 4275 | return sprintf(page, "%s\n", type); |
| 4276 | } |
| 4277 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4278 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4279 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4280 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4281 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4282 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4283 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4284 | return -EINVAL; |
| 4285 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4286 | if (cmd_match(page, "frozen")) |
| 4287 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4288 | else |
| 4289 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4290 | |
| 4291 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4292 | if (mddev->sync_thread) { |
| 4293 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4294 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4295 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4296 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4297 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4298 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4299 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4300 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4301 | else if (cmd_match(page, "recover")) { |
| 4302 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4303 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4304 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4305 | int err; |
| 4306 | if (mddev->pers->start_reshape == NULL) |
| 4307 | return -EINVAL; |
| 4308 | err = mddev->pers->start_reshape(mddev); |
| 4309 | if (err) |
| 4310 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4311 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4312 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4313 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4314 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4315 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4316 | return -EINVAL; |
| 4317 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4318 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4319 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4320 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4321 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4322 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4323 | return len; |
| 4324 | } |
| 4325 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4326 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4327 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4328 | { |
| 4329 | return sprintf(page, "%llu\n", |
| 4330 | (unsigned long long) mddev->resync_mismatches); |
| 4331 | } |
| 4332 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4333 | static struct md_sysfs_entry md_scan_mode = |
| 4334 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4335 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4336 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4337 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4338 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4339 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4340 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4341 | { |
| 4342 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4343 | mddev->sync_speed_min ? "local": "system"); |
| 4344 | } |
| 4345 | |
| 4346 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4347 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4348 | { |
| 4349 | int min; |
| 4350 | char *e; |
| 4351 | if (strncmp(buf, "system", 6)==0) { |
| 4352 | mddev->sync_speed_min = 0; |
| 4353 | return len; |
| 4354 | } |
| 4355 | min = simple_strtoul(buf, &e, 10); |
| 4356 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4357 | return -EINVAL; |
| 4358 | mddev->sync_speed_min = min; |
| 4359 | return len; |
| 4360 | } |
| 4361 | |
| 4362 | static struct md_sysfs_entry md_sync_min = |
| 4363 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4364 | |
| 4365 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4366 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4367 | { |
| 4368 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4369 | mddev->sync_speed_max ? "local": "system"); |
| 4370 | } |
| 4371 | |
| 4372 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4373 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4374 | { |
| 4375 | int max; |
| 4376 | char *e; |
| 4377 | if (strncmp(buf, "system", 6)==0) { |
| 4378 | mddev->sync_speed_max = 0; |
| 4379 | return len; |
| 4380 | } |
| 4381 | max = simple_strtoul(buf, &e, 10); |
| 4382 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4383 | return -EINVAL; |
| 4384 | mddev->sync_speed_max = max; |
| 4385 | return len; |
| 4386 | } |
| 4387 | |
| 4388 | static struct md_sysfs_entry md_sync_max = |
| 4389 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4390 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4391 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4392 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4393 | { |
| 4394 | return sprintf(page, "%d\n", mddev->degraded); |
| 4395 | } |
| 4396 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4397 | |
| 4398 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4399 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4400 | { |
| 4401 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4402 | } |
| 4403 | |
| 4404 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4405 | 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] | 4406 | { |
| 4407 | long n; |
| 4408 | |
| 4409 | if (strict_strtol(buf, 10, &n)) |
| 4410 | return -EINVAL; |
| 4411 | |
| 4412 | if (n != 0 && n != 1) |
| 4413 | return -EINVAL; |
| 4414 | |
| 4415 | mddev->parallel_resync = n; |
| 4416 | |
| 4417 | if (mddev->sync_thread) |
| 4418 | wake_up(&resync_wait); |
| 4419 | |
| 4420 | return len; |
| 4421 | } |
| 4422 | |
| 4423 | /* force parallel resync, even with shared block devices */ |
| 4424 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4425 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4426 | sync_force_parallel_show, sync_force_parallel_store); |
| 4427 | |
| 4428 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4429 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4430 | { |
| 4431 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4432 | if (mddev->curr_resync == 0) |
| 4433 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4434 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4435 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4436 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4437 | db = resync - mddev->resync_mark_cnt; |
| 4438 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4439 | } |
| 4440 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4441 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4442 | |
| 4443 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4444 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4445 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4446 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4447 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4448 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4449 | return sprintf(page, "none\n"); |
| 4450 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4451 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4452 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4453 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4454 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4455 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4456 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4457 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4458 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4459 | } |
| 4460 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4461 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4462 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4463 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4464 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4465 | { |
| 4466 | return sprintf(page, "%llu\n", |
| 4467 | (unsigned long long)mddev->resync_min); |
| 4468 | } |
| 4469 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4470 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4471 | { |
| 4472 | unsigned long long min; |
| 4473 | if (strict_strtoull(buf, 10, &min)) |
| 4474 | return -EINVAL; |
| 4475 | if (min > mddev->resync_max) |
| 4476 | return -EINVAL; |
| 4477 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4478 | return -EBUSY; |
| 4479 | |
| 4480 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4481 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4482 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4483 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4484 | return -EINVAL; |
| 4485 | } |
| 4486 | mddev->resync_min = min; |
| 4487 | |
| 4488 | return len; |
| 4489 | } |
| 4490 | |
| 4491 | static struct md_sysfs_entry md_min_sync = |
| 4492 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4493 | |
| 4494 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4495 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4496 | { |
| 4497 | if (mddev->resync_max == MaxSector) |
| 4498 | return sprintf(page, "max\n"); |
| 4499 | else |
| 4500 | return sprintf(page, "%llu\n", |
| 4501 | (unsigned long long)mddev->resync_max); |
| 4502 | } |
| 4503 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4504 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4505 | { |
| 4506 | if (strncmp(buf, "max", 3) == 0) |
| 4507 | mddev->resync_max = MaxSector; |
| 4508 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4509 | unsigned long long max; |
| 4510 | if (strict_strtoull(buf, 10, &max)) |
| 4511 | return -EINVAL; |
| 4512 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4513 | return -EINVAL; |
| 4514 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4515 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4516 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4517 | return -EBUSY; |
| 4518 | |
| 4519 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4520 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4521 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4522 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4523 | return -EINVAL; |
| 4524 | } |
| 4525 | mddev->resync_max = max; |
| 4526 | } |
| 4527 | wake_up(&mddev->recovery_wait); |
| 4528 | return len; |
| 4529 | } |
| 4530 | |
| 4531 | static struct md_sysfs_entry md_max_sync = |
| 4532 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4533 | |
| 4534 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4535 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4536 | { |
| 4537 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4538 | } |
| 4539 | |
| 4540 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4541 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4542 | { |
| 4543 | char *e; |
| 4544 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4545 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4546 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4547 | if (mddev->pers == NULL || |
| 4548 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4549 | return -EINVAL; |
| 4550 | if (buf == e || (*e && *e != '\n')) |
| 4551 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4552 | |
| 4553 | mddev->suspend_lo = new; |
| 4554 | if (new >= old) |
| 4555 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4556 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4557 | else { |
| 4558 | /* Expanding suspended region - need to wait */ |
| 4559 | mddev->pers->quiesce(mddev, 1); |
| 4560 | mddev->pers->quiesce(mddev, 0); |
| 4561 | } |
| 4562 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4563 | } |
| 4564 | static struct md_sysfs_entry md_suspend_lo = |
| 4565 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4566 | |
| 4567 | |
| 4568 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4569 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4570 | { |
| 4571 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4572 | } |
| 4573 | |
| 4574 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4575 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4576 | { |
| 4577 | char *e; |
| 4578 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4579 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4580 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4581 | if (mddev->pers == NULL || |
| 4582 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4583 | return -EINVAL; |
| 4584 | if (buf == e || (*e && *e != '\n')) |
| 4585 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4586 | |
| 4587 | mddev->suspend_hi = new; |
| 4588 | if (new <= old) |
| 4589 | /* Shrinking suspended region */ |
| 4590 | mddev->pers->quiesce(mddev, 2); |
| 4591 | else { |
| 4592 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4593 | mddev->pers->quiesce(mddev, 1); |
| 4594 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4595 | } |
| 4596 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4597 | } |
| 4598 | static struct md_sysfs_entry md_suspend_hi = |
| 4599 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4600 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4601 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4602 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4603 | { |
| 4604 | if (mddev->reshape_position != MaxSector) |
| 4605 | return sprintf(page, "%llu\n", |
| 4606 | (unsigned long long)mddev->reshape_position); |
| 4607 | strcpy(page, "none\n"); |
| 4608 | return 5; |
| 4609 | } |
| 4610 | |
| 4611 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4612 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4613 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4614 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4615 | char *e; |
| 4616 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4617 | if (mddev->pers) |
| 4618 | return -EBUSY; |
| 4619 | if (buf == e || (*e && *e != '\n')) |
| 4620 | return -EINVAL; |
| 4621 | mddev->reshape_position = new; |
| 4622 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4623 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4624 | mddev->new_level = mddev->level; |
| 4625 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4626 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4627 | rdev_for_each(rdev, mddev) |
| 4628 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4629 | return len; |
| 4630 | } |
| 4631 | |
| 4632 | static struct md_sysfs_entry md_reshape_position = |
| 4633 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4634 | reshape_position_store); |
| 4635 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4636 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4637 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4638 | { |
| 4639 | return sprintf(page, "%s\n", |
| 4640 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4641 | } |
| 4642 | |
| 4643 | static ssize_t |
| 4644 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4645 | { |
| 4646 | int backwards = 0; |
| 4647 | if (cmd_match(buf, "forwards")) |
| 4648 | backwards = 0; |
| 4649 | else if (cmd_match(buf, "backwards")) |
| 4650 | backwards = 1; |
| 4651 | else |
| 4652 | return -EINVAL; |
| 4653 | if (mddev->reshape_backwards == backwards) |
| 4654 | return len; |
| 4655 | |
| 4656 | /* check if we are allowed to change */ |
| 4657 | if (mddev->delta_disks) |
| 4658 | return -EBUSY; |
| 4659 | |
| 4660 | if (mddev->persistent && |
| 4661 | mddev->major_version == 0) |
| 4662 | return -EINVAL; |
| 4663 | |
| 4664 | mddev->reshape_backwards = backwards; |
| 4665 | return len; |
| 4666 | } |
| 4667 | |
| 4668 | static struct md_sysfs_entry md_reshape_direction = |
| 4669 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4670 | reshape_direction_store); |
| 4671 | |
| 4672 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4673 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4674 | { |
| 4675 | if (mddev->external_size) |
| 4676 | return sprintf(page, "%llu\n", |
| 4677 | (unsigned long long)mddev->array_sectors/2); |
| 4678 | else |
| 4679 | return sprintf(page, "default\n"); |
| 4680 | } |
| 4681 | |
| 4682 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4683 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4684 | { |
| 4685 | sector_t sectors; |
| 4686 | |
| 4687 | if (strncmp(buf, "default", 7) == 0) { |
| 4688 | if (mddev->pers) |
| 4689 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4690 | else |
| 4691 | sectors = mddev->array_sectors; |
| 4692 | |
| 4693 | mddev->external_size = 0; |
| 4694 | } else { |
| 4695 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4696 | return -EINVAL; |
| 4697 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4698 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4699 | |
| 4700 | mddev->external_size = 1; |
| 4701 | } |
| 4702 | |
| 4703 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4704 | if (mddev->pers) { |
| 4705 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4706 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4707 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4708 | return len; |
| 4709 | } |
| 4710 | |
| 4711 | static struct md_sysfs_entry md_array_size = |
| 4712 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4713 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4714 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4715 | static struct attribute *md_default_attrs[] = { |
| 4716 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4717 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4718 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4719 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4720 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4721 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4722 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4723 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4724 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4725 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4726 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4727 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4728 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4729 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4730 | NULL, |
| 4731 | }; |
| 4732 | |
| 4733 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4734 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4735 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4736 | &md_sync_min.attr, |
| 4737 | &md_sync_max.attr, |
| 4738 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4739 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4740 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4741 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4742 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4743 | &md_suspend_lo.attr, |
| 4744 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4745 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4746 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4747 | NULL, |
| 4748 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4749 | static struct attribute_group md_redundancy_group = { |
| 4750 | .name = NULL, |
| 4751 | .attrs = md_redundancy_attrs, |
| 4752 | }; |
| 4753 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4754 | |
| 4755 | static ssize_t |
| 4756 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4757 | { |
| 4758 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4759 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4760 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4761 | |
| 4762 | if (!entry->show) |
| 4763 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4764 | spin_lock(&all_mddevs_lock); |
| 4765 | if (list_empty(&mddev->all_mddevs)) { |
| 4766 | spin_unlock(&all_mddevs_lock); |
| 4767 | return -EBUSY; |
| 4768 | } |
| 4769 | mddev_get(mddev); |
| 4770 | spin_unlock(&all_mddevs_lock); |
| 4771 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4772 | rv = mddev_lock(mddev); |
| 4773 | if (!rv) { |
| 4774 | rv = entry->show(mddev, page); |
| 4775 | mddev_unlock(mddev); |
| 4776 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4777 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4778 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | static ssize_t |
| 4782 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4783 | const char *page, size_t length) |
| 4784 | { |
| 4785 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4786 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4787 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4788 | |
| 4789 | if (!entry->store) |
| 4790 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4791 | if (!capable(CAP_SYS_ADMIN)) |
| 4792 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4793 | spin_lock(&all_mddevs_lock); |
| 4794 | if (list_empty(&mddev->all_mddevs)) { |
| 4795 | spin_unlock(&all_mddevs_lock); |
| 4796 | return -EBUSY; |
| 4797 | } |
| 4798 | mddev_get(mddev); |
| 4799 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4800 | rv = mddev_lock(mddev); |
| 4801 | if (!rv) { |
| 4802 | rv = entry->store(mddev, page, length); |
| 4803 | mddev_unlock(mddev); |
| 4804 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4805 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4806 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | static void md_free(struct kobject *ko) |
| 4810 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4811 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4812 | |
| 4813 | if (mddev->sysfs_state) |
| 4814 | sysfs_put(mddev->sysfs_state); |
| 4815 | |
| 4816 | if (mddev->gendisk) { |
| 4817 | del_gendisk(mddev->gendisk); |
| 4818 | put_disk(mddev->gendisk); |
| 4819 | } |
| 4820 | if (mddev->queue) |
| 4821 | blk_cleanup_queue(mddev->queue); |
| 4822 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4823 | kfree(mddev); |
| 4824 | } |
| 4825 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4826 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4827 | .show = md_attr_show, |
| 4828 | .store = md_attr_store, |
| 4829 | }; |
| 4830 | static struct kobj_type md_ktype = { |
| 4831 | .release = md_free, |
| 4832 | .sysfs_ops = &md_sysfs_ops, |
| 4833 | .default_attrs = md_default_attrs, |
| 4834 | }; |
| 4835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | int mdp_major = 0; |
| 4837 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4838 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4839 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4840 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4841 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4842 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4843 | kobject_del(&mddev->kobj); |
| 4844 | kobject_put(&mddev->kobj); |
| 4845 | } |
| 4846 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4847 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4849 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4850 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4852 | int partitioned; |
| 4853 | int shift; |
| 4854 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4855 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | |
| 4857 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4858 | return -ENODEV; |
| 4859 | |
| 4860 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4861 | shift = partitioned ? MdpMinorShift : 0; |
| 4862 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4864 | /* wait for any previous instance of this device to be |
| 4865 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4866 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4867 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4868 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4869 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4870 | error = -EEXIST; |
| 4871 | if (mddev->gendisk) |
| 4872 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4873 | |
| 4874 | if (name) { |
| 4875 | /* Need to ensure that 'name' is not a duplicate. |
| 4876 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4877 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4878 | spin_lock(&all_mddevs_lock); |
| 4879 | |
| 4880 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4881 | if (mddev2->gendisk && |
| 4882 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4883 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4884 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4885 | } |
| 4886 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4888 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4889 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4890 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4891 | if (!mddev->queue) |
| 4892 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4893 | mddev->queue->queuedata = mddev; |
| 4894 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4895 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4896 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | disk = alloc_disk(1 << shift); |
| 4899 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4900 | blk_cleanup_queue(mddev->queue); |
| 4901 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4902 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4904 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4906 | if (name) |
| 4907 | strcpy(disk->disk_name, name); |
| 4908 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4910 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | disk->fops = &md_fops; |
| 4913 | disk->private_data = mddev; |
| 4914 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4915 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4916 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4917 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4918 | * remove it now. |
| 4919 | */ |
| 4920 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4922 | /* As soon as we call add_disk(), another thread could get |
| 4923 | * through to md_open, so make sure it doesn't get too far |
| 4924 | */ |
| 4925 | mutex_lock(&mddev->open_mutex); |
| 4926 | add_disk(disk); |
| 4927 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4928 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4929 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4930 | if (error) { |
| 4931 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4932 | * __must_check, we must do something with the result |
| 4933 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4934 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4935 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4936 | error = 0; |
| 4937 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4938 | if (mddev->kobj.sd && |
| 4939 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4940 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4941 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4942 | abort: |
| 4943 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4944 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4945 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4946 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4947 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4948 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4949 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4953 | { |
| 4954 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | return NULL; |
| 4956 | } |
| 4957 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4958 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4959 | { |
| 4960 | /* val must be "md_*" where * is not all digits. |
| 4961 | * We allocate an array with a large free minor number, and |
| 4962 | * set the name to val. val must not already be an active name. |
| 4963 | */ |
| 4964 | int len = strlen(val); |
| 4965 | char buf[DISK_NAME_LEN]; |
| 4966 | |
| 4967 | while (len && val[len-1] == '\n') |
| 4968 | len--; |
| 4969 | if (len >= DISK_NAME_LEN) |
| 4970 | return -E2BIG; |
| 4971 | strlcpy(buf, val, len+1); |
| 4972 | if (strncmp(buf, "md_", 3) != 0) |
| 4973 | return -EINVAL; |
| 4974 | return md_alloc(0, buf); |
| 4975 | } |
| 4976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | static void md_safemode_timeout(unsigned long data) |
| 4978 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4979 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4981 | if (!atomic_read(&mddev->writes_pending)) { |
| 4982 | mddev->safemode = 1; |
| 4983 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4984 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | md_wakeup_thread(mddev->thread); |
| 4987 | } |
| 4988 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4989 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4991 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4993 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4994 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4995 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4997 | if (list_empty(&mddev->disks)) |
| 4998 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | |
| 5001 | if (mddev->pers) |
| 5002 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5003 | /* Cannot run until previous stop completes properly */ |
| 5004 | if (mddev->sysfs_active) |
| 5005 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | /* |
| 5008 | * Analyze all RAID superblock(s) |
| 5009 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5010 | if (!mddev->raid_disks) { |
| 5011 | if (!mddev->persistent) |
| 5012 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5013 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5016 | if (mddev->level != LEVEL_NONE) |
| 5017 | request_module("md-level-%d", mddev->level); |
| 5018 | else if (mddev->clevel[0]) |
| 5019 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | |
| 5021 | /* |
| 5022 | * Drop all container device buffers, from now on |
| 5023 | * the only valid external interface is through the md |
| 5024 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5026 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5027 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | continue; |
| 5029 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5030 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5031 | |
| 5032 | /* perform some consistency tests on the device. |
| 5033 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5034 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5035 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5036 | if (rdev->meta_bdev) { |
| 5037 | /* Nothing to check */; |
| 5038 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5039 | if (mddev->dev_sectors && |
| 5040 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5041 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5042 | printk("md: %s: data overlaps metadata\n", |
| 5043 | mdname(mddev)); |
| 5044 | return -EINVAL; |
| 5045 | } |
| 5046 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5047 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5048 | > rdev->data_offset) { |
| 5049 | printk("md: %s: metadata overlaps data\n", |
| 5050 | mdname(mddev)); |
| 5051 | return -EINVAL; |
| 5052 | } |
| 5053 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5054 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | } |
| 5056 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5057 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 5058 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5059 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5062 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5063 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5065 | if (mddev->level != LEVEL_NONE) |
| 5066 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5067 | mddev->level); |
| 5068 | else |
| 5069 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5070 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | return -EINVAL; |
| 5072 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5073 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5075 | if (mddev->level != pers->level) { |
| 5076 | mddev->level = pers->level; |
| 5077 | mddev->new_level = pers->level; |
| 5078 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5079 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5081 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5082 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5083 | /* This personality cannot handle reshaping... */ |
| 5084 | mddev->pers = NULL; |
| 5085 | module_put(pers->owner); |
| 5086 | return -EINVAL; |
| 5087 | } |
| 5088 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5089 | if (pers->sync_request) { |
| 5090 | /* Warn if this is a potentially silly |
| 5091 | * configuration. |
| 5092 | */ |
| 5093 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5094 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5095 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5096 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5097 | rdev_for_each(rdev, mddev) |
| 5098 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5099 | if (rdev < rdev2 && |
| 5100 | rdev->bdev->bd_contains == |
| 5101 | rdev2->bdev->bd_contains) { |
| 5102 | printk(KERN_WARNING |
| 5103 | "%s: WARNING: %s appears to be" |
| 5104 | " on the same physical disk as" |
| 5105 | " %s.\n", |
| 5106 | mdname(mddev), |
| 5107 | bdevname(rdev->bdev,b), |
| 5108 | bdevname(rdev2->bdev,b2)); |
| 5109 | warned = 1; |
| 5110 | } |
| 5111 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5112 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5113 | if (warned) |
| 5114 | printk(KERN_WARNING |
| 5115 | "True protection against single-disk" |
| 5116 | " failure might be compromised.\n"); |
| 5117 | } |
| 5118 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5119 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5120 | /* may be over-ridden by personality */ |
| 5121 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5122 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5123 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5125 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5126 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5127 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5128 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5129 | if (err) |
| 5130 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5131 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5132 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5133 | " but 'external_size' not in effect?\n", __func__); |
| 5134 | printk(KERN_ERR |
| 5135 | "md: invalid array_size %llu > default size %llu\n", |
| 5136 | (unsigned long long)mddev->array_sectors / 2, |
| 5137 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5138 | err = -EINVAL; |
| 5139 | mddev->pers->stop(mddev); |
| 5140 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5141 | if (err == 0 && mddev->pers->sync_request && |
| 5142 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5143 | err = bitmap_create(mddev); |
| 5144 | if (err) { |
| 5145 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5146 | mdname(mddev), err); |
| 5147 | mddev->pers->stop(mddev); |
| 5148 | } |
| 5149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | module_put(mddev->pers->owner); |
| 5152 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5153 | bitmap_destroy(mddev); |
| 5154 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5156 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5157 | if (mddev->kobj.sd && |
| 5158 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5159 | printk(KERN_WARNING |
| 5160 | "md: cannot register extra attributes for %s\n", |
| 5161 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5162 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5163 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5164 | mddev->ro = 0; |
| 5165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5167 | atomic_set(&mddev->max_corr_read_errors, |
| 5168 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | mddev->safemode = 0; |
| 5170 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5171 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5172 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5174 | smp_wmb(); |
| 5175 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5176 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5177 | if (rdev->raid_disk >= 0) |
| 5178 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5179 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
| 5181 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5182 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5183 | if (mddev->flags) |
| 5184 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5186 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5187 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5188 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5189 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | return 0; |
| 5191 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5192 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5194 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5195 | { |
| 5196 | int err; |
| 5197 | |
| 5198 | err = md_run(mddev); |
| 5199 | if (err) |
| 5200 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5201 | err = bitmap_load(mddev); |
| 5202 | if (err) { |
| 5203 | bitmap_destroy(mddev); |
| 5204 | goto out; |
| 5205 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5206 | |
| 5207 | md_wakeup_thread(mddev->thread); |
| 5208 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5209 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5210 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5211 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5212 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5213 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5214 | out: |
| 5215 | return err; |
| 5216 | } |
| 5217 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5218 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | { |
| 5220 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5222 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5224 | return -ENXIO; |
| 5225 | if (!mddev->pers) |
| 5226 | return -EINVAL; |
| 5227 | if (!mddev->ro) |
| 5228 | return -EBUSY; |
| 5229 | mddev->safemode = 0; |
| 5230 | mddev->ro = 0; |
| 5231 | set_disk_ro(disk, 0); |
| 5232 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5233 | mdname(mddev)); |
| 5234 | /* Kick recovery or resync if necessary */ |
| 5235 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5236 | md_wakeup_thread(mddev->thread); |
| 5237 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5238 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5239 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5242 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5243 | * to the file ourselves */ |
| 5244 | static int deny_bitmap_write_access(struct file * file) |
| 5245 | { |
| 5246 | struct inode *inode = file->f_mapping->host; |
| 5247 | |
| 5248 | spin_lock(&inode->i_lock); |
| 5249 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5250 | spin_unlock(&inode->i_lock); |
| 5251 | return -ETXTBSY; |
| 5252 | } |
| 5253 | atomic_set(&inode->i_writecount, -1); |
| 5254 | spin_unlock(&inode->i_lock); |
| 5255 | |
| 5256 | return 0; |
| 5257 | } |
| 5258 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5259 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5260 | { |
| 5261 | struct inode *inode = file->f_mapping->host; |
| 5262 | |
| 5263 | spin_lock(&inode->i_lock); |
| 5264 | atomic_set(&inode->i_writecount, 1); |
| 5265 | spin_unlock(&inode->i_lock); |
| 5266 | } |
| 5267 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5268 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5269 | { |
| 5270 | mddev->array_sectors = 0; |
| 5271 | mddev->external_size = 0; |
| 5272 | mddev->dev_sectors = 0; |
| 5273 | mddev->raid_disks = 0; |
| 5274 | mddev->recovery_cp = 0; |
| 5275 | mddev->resync_min = 0; |
| 5276 | mddev->resync_max = MaxSector; |
| 5277 | mddev->reshape_position = MaxSector; |
| 5278 | mddev->external = 0; |
| 5279 | mddev->persistent = 0; |
| 5280 | mddev->level = LEVEL_NONE; |
| 5281 | mddev->clevel[0] = 0; |
| 5282 | mddev->flags = 0; |
| 5283 | mddev->ro = 0; |
| 5284 | mddev->metadata_type[0] = 0; |
| 5285 | mddev->chunk_sectors = 0; |
| 5286 | mddev->ctime = mddev->utime = 0; |
| 5287 | mddev->layout = 0; |
| 5288 | mddev->max_disks = 0; |
| 5289 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5290 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5291 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5292 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5293 | mddev->new_level = LEVEL_NONE; |
| 5294 | mddev->new_layout = 0; |
| 5295 | mddev->new_chunk_sectors = 0; |
| 5296 | mddev->curr_resync = 0; |
| 5297 | mddev->resync_mismatches = 0; |
| 5298 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5299 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5300 | mddev->recovery = 0; |
| 5301 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5302 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5303 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5304 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5305 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5306 | mddev->bitmap_info.offset = 0; |
| 5307 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5308 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5309 | mddev->bitmap_info.chunksize = 0; |
| 5310 | mddev->bitmap_info.daemon_sleep = 0; |
| 5311 | mddev->bitmap_info.max_write_behind = 0; |
| 5312 | } |
| 5313 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5314 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5315 | { |
| 5316 | if (mddev->sync_thread) { |
| 5317 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5318 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5319 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | del_timer_sync(&mddev->safemode_timer); |
| 5323 | |
| 5324 | bitmap_flush(mddev); |
| 5325 | md_super_wait(mddev); |
| 5326 | |
| 5327 | if (!mddev->in_sync || mddev->flags) { |
| 5328 | /* mark array as shutdown cleanly */ |
| 5329 | mddev->in_sync = 1; |
| 5330 | md_update_sb(mddev, 1); |
| 5331 | } |
| 5332 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5333 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5334 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5335 | { |
| 5336 | mddev_lock(mddev); |
| 5337 | __md_stop_writes(mddev); |
| 5338 | mddev_unlock(mddev); |
| 5339 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5340 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5341 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5342 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5343 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5344 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5345 | mddev->pers->stop(mddev); |
| 5346 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5347 | mddev->to_remove = &md_redundancy_group; |
| 5348 | module_put(mddev->pers->owner); |
| 5349 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5350 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5351 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5352 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5353 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5354 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5355 | { |
| 5356 | int err = 0; |
| 5357 | mutex_lock(&mddev->open_mutex); |
| 5358 | if (atomic_read(&mddev->openers) > is_open) { |
| 5359 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5360 | err = -EBUSY; |
| 5361 | goto out; |
| 5362 | } |
| 5363 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5364 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5365 | |
| 5366 | err = -ENXIO; |
| 5367 | if (mddev->ro==1) |
| 5368 | goto out; |
| 5369 | mddev->ro = 1; |
| 5370 | set_disk_ro(mddev->gendisk, 1); |
| 5371 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5372 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5373 | err = 0; |
| 5374 | } |
| 5375 | out: |
| 5376 | mutex_unlock(&mddev->open_mutex); |
| 5377 | return err; |
| 5378 | } |
| 5379 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5380 | /* mode: |
| 5381 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5382 | * 2 - stop but do not disassemble array |
| 5383 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5384 | 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] | 5385 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5387 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5389 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5390 | if (atomic_read(&mddev->openers) > is_open || |
| 5391 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5392 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5393 | mutex_unlock(&mddev->open_mutex); |
| 5394 | return -EBUSY; |
| 5395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5397 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5398 | if (mddev->ro) |
| 5399 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5400 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5401 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5402 | md_stop(mddev); |
| 5403 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5404 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5405 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5406 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5407 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5408 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5409 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5410 | if (rdev->raid_disk >= 0) |
| 5411 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5412 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5413 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5414 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5415 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5416 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5417 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5418 | if (mddev->ro) |
| 5419 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5420 | } else |
| 5421 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | /* |
| 5423 | * Free resources if final stop |
| 5424 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5425 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5427 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5428 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5429 | if (mddev->bitmap_info.file) { |
| 5430 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5431 | fput(mddev->bitmap_info.file); |
| 5432 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5433 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5434 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | export_array(mddev); |
| 5437 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5438 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5439 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5440 | if (mddev->hold_active == UNTIL_STOP) |
| 5441 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5442 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5443 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5444 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5445 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5446 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | } |
| 5448 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5449 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5450 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5452 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | int err; |
| 5454 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5455 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | |
| 5458 | printk(KERN_INFO "md: running: "); |
| 5459 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5460 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | char b[BDEVNAME_SIZE]; |
| 5462 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5463 | } |
| 5464 | printk("\n"); |
| 5465 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5466 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | if (err) { |
| 5468 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5469 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | /* |
| 5474 | * lets try to run arrays based on all disks that have arrived |
| 5475 | * until now. (those are in pending_raid_disks) |
| 5476 | * |
| 5477 | * the method: pick the first pending disk, collect all disks with |
| 5478 | * the same UUID, remove all from the pending list and put them into |
| 5479 | * the 'same_array' list. Then order this list based on superblock |
| 5480 | * update time (freshest comes first), kick out 'old' disks and |
| 5481 | * compare superblocks. If everything's fine then run it. |
| 5482 | * |
| 5483 | * If "unit" is allocated, then bump its reference count |
| 5484 | */ |
| 5485 | static void autorun_devices(int part) |
| 5486 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5487 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5488 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | char b[BDEVNAME_SIZE]; |
| 5490 | |
| 5491 | printk(KERN_INFO "md: autorun ...\n"); |
| 5492 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5493 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5495 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5497 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | |
| 5499 | printk(KERN_INFO "md: considering %s ...\n", |
| 5500 | bdevname(rdev0->bdev,b)); |
| 5501 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5502 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5504 | printk(KERN_INFO "md: adding %s ...\n", |
| 5505 | bdevname(rdev->bdev,b)); |
| 5506 | list_move(&rdev->same_set, &candidates); |
| 5507 | } |
| 5508 | /* |
| 5509 | * now we have a set of devices, with all of them having |
| 5510 | * mostly sane superblocks. It's time to allocate the |
| 5511 | * mddev. |
| 5512 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5513 | if (part) { |
| 5514 | dev = MKDEV(mdp_major, |
| 5515 | rdev0->preferred_minor << MdpMinorShift); |
| 5516 | unit = MINOR(dev) >> MdpMinorShift; |
| 5517 | } else { |
| 5518 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5519 | unit = MINOR(dev); |
| 5520 | } |
| 5521 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5523 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5524 | break; |
| 5525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | |
| 5527 | md_probe(dev, NULL, NULL); |
| 5528 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5529 | if (!mddev || !mddev->gendisk) { |
| 5530 | if (mddev) |
| 5531 | mddev_put(mddev); |
| 5532 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | "md: cannot allocate memory for md drive.\n"); |
| 5534 | break; |
| 5535 | } |
| 5536 | if (mddev_lock(mddev)) |
| 5537 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5538 | mdname(mddev)); |
| 5539 | else if (mddev->raid_disks || mddev->major_version |
| 5540 | || !list_empty(&mddev->disks)) { |
| 5541 | printk(KERN_WARNING |
| 5542 | "md: %s already running, cannot run %s\n", |
| 5543 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5544 | mddev_unlock(mddev); |
| 5545 | } else { |
| 5546 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5547 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5548 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | list_del_init(&rdev->same_set); |
| 5550 | if (bind_rdev_to_array(rdev, mddev)) |
| 5551 | export_rdev(rdev); |
| 5552 | } |
| 5553 | autorun_array(mddev); |
| 5554 | mddev_unlock(mddev); |
| 5555 | } |
| 5556 | /* on success, candidates will be empty, on error |
| 5557 | * it won't... |
| 5558 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5559 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5560 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | mddev_put(mddev); |
| 5564 | } |
| 5565 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5566 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5567 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | static int get_version(void __user * arg) |
| 5570 | { |
| 5571 | mdu_version_t ver; |
| 5572 | |
| 5573 | ver.major = MD_MAJOR_VERSION; |
| 5574 | ver.minor = MD_MINOR_VERSION; |
| 5575 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5576 | |
| 5577 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5578 | return -EFAULT; |
| 5579 | |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5583 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | { |
| 5585 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5586 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5587 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5589 | nr=working=insync=failed=spare=0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5590 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5592 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | failed++; |
| 5594 | else { |
| 5595 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5596 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5597 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | else |
| 5599 | spare++; |
| 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | info.major_version = mddev->major_version; |
| 5604 | info.minor_version = mddev->minor_version; |
| 5605 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5606 | info.ctime = mddev->ctime; |
| 5607 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5608 | info.size = mddev->dev_sectors / 2; |
| 5609 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5610 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | info.nr_disks = nr; |
| 5612 | info.raid_disks = mddev->raid_disks; |
| 5613 | info.md_minor = mddev->md_minor; |
| 5614 | info.not_persistent= !mddev->persistent; |
| 5615 | |
| 5616 | info.utime = mddev->utime; |
| 5617 | info.state = 0; |
| 5618 | if (mddev->in_sync) |
| 5619 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5620 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5621 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5622 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | info.working_disks = working; |
| 5624 | info.failed_disks = failed; |
| 5625 | info.spare_disks = spare; |
| 5626 | |
| 5627 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5628 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | |
| 5630 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5631 | return -EFAULT; |
| 5632 | |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5636 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5637 | { |
| 5638 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5639 | char *ptr, *buf = NULL; |
| 5640 | int err = -ENOMEM; |
| 5641 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5642 | if (md_allow_write(mddev)) |
| 5643 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5644 | else |
| 5645 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5646 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5647 | if (!file) |
| 5648 | goto out; |
| 5649 | |
| 5650 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5651 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5652 | file->pathname[0] = '\0'; |
| 5653 | goto copy_out; |
| 5654 | } |
| 5655 | |
| 5656 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5657 | if (!buf) |
| 5658 | goto out; |
| 5659 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5660 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5661 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5662 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5663 | goto out; |
| 5664 | |
| 5665 | strcpy(file->pathname, ptr); |
| 5666 | |
| 5667 | copy_out: |
| 5668 | err = 0; |
| 5669 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5670 | err = -EFAULT; |
| 5671 | out: |
| 5672 | kfree(buf); |
| 5673 | kfree(file); |
| 5674 | return err; |
| 5675 | } |
| 5676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5677 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | { |
| 5679 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5680 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | |
| 5682 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5683 | return -EFAULT; |
| 5684 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5685 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | if (rdev) { |
| 5687 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5688 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5689 | info.raid_disk = rdev->raid_disk; |
| 5690 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5691 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5693 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5695 | info.state |= (1<<MD_DISK_SYNC); |
| 5696 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5697 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5698 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | } else { |
| 5700 | info.major = info.minor = 0; |
| 5701 | info.raid_disk = -1; |
| 5702 | info.state = (1<<MD_DISK_REMOVED); |
| 5703 | } |
| 5704 | |
| 5705 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5706 | return -EFAULT; |
| 5707 | |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5711 | 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] | 5712 | { |
| 5713 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5714 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | dev_t dev = MKDEV(info->major,info->minor); |
| 5716 | |
| 5717 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5718 | return -EOVERFLOW; |
| 5719 | |
| 5720 | if (!mddev->raid_disks) { |
| 5721 | int err; |
| 5722 | /* expecting a device which has a superblock */ |
| 5723 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5724 | if (IS_ERR(rdev)) { |
| 5725 | printk(KERN_WARNING |
| 5726 | "md: md_import_device returned %ld\n", |
| 5727 | PTR_ERR(rdev)); |
| 5728 | return PTR_ERR(rdev); |
| 5729 | } |
| 5730 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5731 | struct md_rdev *rdev0 |
| 5732 | = list_entry(mddev->disks.next, |
| 5733 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5734 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5736 | if (err < 0) { |
| 5737 | printk(KERN_WARNING |
| 5738 | "md: %s has different UUID to %s\n", |
| 5739 | bdevname(rdev->bdev,b), |
| 5740 | bdevname(rdev0->bdev,b2)); |
| 5741 | export_rdev(rdev); |
| 5742 | return -EINVAL; |
| 5743 | } |
| 5744 | } |
| 5745 | err = bind_rdev_to_array(rdev, mddev); |
| 5746 | if (err) |
| 5747 | export_rdev(rdev); |
| 5748 | return err; |
| 5749 | } |
| 5750 | |
| 5751 | /* |
| 5752 | * add_new_disk can be used once the array is assembled |
| 5753 | * to add "hot spares". They must already have a superblock |
| 5754 | * written |
| 5755 | */ |
| 5756 | if (mddev->pers) { |
| 5757 | int err; |
| 5758 | if (!mddev->pers->hot_add_disk) { |
| 5759 | printk(KERN_WARNING |
| 5760 | "%s: personality does not support diskops!\n", |
| 5761 | mdname(mddev)); |
| 5762 | return -EINVAL; |
| 5763 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5764 | if (mddev->persistent) |
| 5765 | rdev = md_import_device(dev, mddev->major_version, |
| 5766 | mddev->minor_version); |
| 5767 | else |
| 5768 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | if (IS_ERR(rdev)) { |
| 5770 | printk(KERN_WARNING |
| 5771 | "md: md_import_device returned %ld\n", |
| 5772 | PTR_ERR(rdev)); |
| 5773 | return PTR_ERR(rdev); |
| 5774 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5775 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5776 | if (!mddev->persistent) { |
| 5777 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5778 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5779 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5780 | set_bit(In_sync, &rdev->flags); |
| 5781 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5782 | rdev->raid_disk = -1; |
| 5783 | } else |
| 5784 | super_types[mddev->major_version]. |
| 5785 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5786 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5787 | (!test_bit(In_sync, &rdev->flags) || |
| 5788 | rdev->raid_disk != info->raid_disk)) { |
| 5789 | /* This was a hot-add request, but events doesn't |
| 5790 | * match, so reject it. |
| 5791 | */ |
| 5792 | export_rdev(rdev); |
| 5793 | return -EINVAL; |
| 5794 | } |
| 5795 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5796 | if (test_bit(In_sync, &rdev->flags)) |
| 5797 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5798 | else |
| 5799 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5800 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5801 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5802 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5803 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5804 | else |
| 5805 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | rdev->raid_disk = -1; |
| 5808 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5809 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5810 | /* If there is hot_add_disk but no hot_remove_disk |
| 5811 | * then added disks for geometry changes, |
| 5812 | * and should be added immediately. |
| 5813 | */ |
| 5814 | super_types[mddev->major_version]. |
| 5815 | validate_super(mddev, rdev); |
| 5816 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5817 | if (err) |
| 5818 | unbind_rdev_from_array(rdev); |
| 5819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | if (err) |
| 5821 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5822 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5823 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5824 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5825 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5826 | if (mddev->degraded) |
| 5827 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5828 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5829 | if (!err) |
| 5830 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5831 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | return err; |
| 5833 | } |
| 5834 | |
| 5835 | /* otherwise, add_new_disk is only allowed |
| 5836 | * for major_version==0 superblocks |
| 5837 | */ |
| 5838 | if (mddev->major_version != 0) { |
| 5839 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5840 | mdname(mddev)); |
| 5841 | return -EINVAL; |
| 5842 | } |
| 5843 | |
| 5844 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5845 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5846 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | if (IS_ERR(rdev)) { |
| 5848 | printk(KERN_WARNING |
| 5849 | "md: error, md_import_device() returned %ld\n", |
| 5850 | PTR_ERR(rdev)); |
| 5851 | return PTR_ERR(rdev); |
| 5852 | } |
| 5853 | rdev->desc_nr = info->number; |
| 5854 | if (info->raid_disk < mddev->raid_disks) |
| 5855 | rdev->raid_disk = info->raid_disk; |
| 5856 | else |
| 5857 | rdev->raid_disk = -1; |
| 5858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5860 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5861 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5863 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5864 | set_bit(WriteMostly, &rdev->flags); |
| 5865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | if (!mddev->persistent) { |
| 5867 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5868 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5869 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5870 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5871 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5873 | err = bind_rdev_to_array(rdev, mddev); |
| 5874 | if (err) { |
| 5875 | export_rdev(rdev); |
| 5876 | return err; |
| 5877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | return 0; |
| 5881 | } |
| 5882 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5883 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | { |
| 5885 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5886 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | rdev = find_rdev(mddev, dev); |
| 5889 | if (!rdev) |
| 5890 | return -ENXIO; |
| 5891 | |
| 5892 | if (rdev->raid_disk >= 0) |
| 5893 | goto busy; |
| 5894 | |
| 5895 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5896 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5897 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | |
| 5899 | return 0; |
| 5900 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5901 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5903 | return -EBUSY; |
| 5904 | } |
| 5905 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5906 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | { |
| 5908 | char b[BDEVNAME_SIZE]; |
| 5909 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5910 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | |
| 5912 | if (!mddev->pers) |
| 5913 | return -ENODEV; |
| 5914 | |
| 5915 | if (mddev->major_version != 0) { |
| 5916 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5917 | " version-0 superblocks.\n", |
| 5918 | mdname(mddev)); |
| 5919 | return -EINVAL; |
| 5920 | } |
| 5921 | if (!mddev->pers->hot_add_disk) { |
| 5922 | printk(KERN_WARNING |
| 5923 | "%s: personality does not support diskops!\n", |
| 5924 | mdname(mddev)); |
| 5925 | return -EINVAL; |
| 5926 | } |
| 5927 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5928 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | if (IS_ERR(rdev)) { |
| 5930 | printk(KERN_WARNING |
| 5931 | "md: error, md_import_device() returned %ld\n", |
| 5932 | PTR_ERR(rdev)); |
| 5933 | return -EINVAL; |
| 5934 | } |
| 5935 | |
| 5936 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5937 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5939 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5941 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5943 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | printk(KERN_WARNING |
| 5945 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5946 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5947 | err = -EINVAL; |
| 5948 | goto abort_export; |
| 5949 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5950 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5952 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5953 | err = bind_rdev_to_array(rdev, mddev); |
| 5954 | if (err) |
| 5955 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | |
| 5957 | /* |
| 5958 | * The rest should better be atomic, we can have disk failures |
| 5959 | * noticed in interrupt contexts ... |
| 5960 | */ |
| 5961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | rdev->raid_disk = -1; |
| 5963 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5964 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | |
| 5966 | /* |
| 5967 | * Kick recovery, maybe this spare has to be added to the |
| 5968 | * array immediately. |
| 5969 | */ |
| 5970 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5971 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5972 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | return 0; |
| 5974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | abort_export: |
| 5976 | export_rdev(rdev); |
| 5977 | return err; |
| 5978 | } |
| 5979 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5980 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5981 | { |
| 5982 | int err; |
| 5983 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5984 | if (mddev->pers) { |
| 5985 | if (!mddev->pers->quiesce) |
| 5986 | return -EBUSY; |
| 5987 | if (mddev->recovery || mddev->sync_thread) |
| 5988 | return -EBUSY; |
| 5989 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5990 | } |
| 5991 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5992 | |
| 5993 | if (fd >= 0) { |
| 5994 | if (mddev->bitmap) |
| 5995 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5996 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5997 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5998 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5999 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6000 | mdname(mddev)); |
| 6001 | return -EBADF; |
| 6002 | } |
| 6003 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6004 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6005 | if (err) { |
| 6006 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6007 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6008 | fput(mddev->bitmap_info.file); |
| 6009 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6010 | return err; |
| 6011 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6012 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6013 | } else if (mddev->bitmap == NULL) |
| 6014 | return -ENOENT; /* cannot remove what isn't there */ |
| 6015 | err = 0; |
| 6016 | if (mddev->pers) { |
| 6017 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6018 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6019 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6020 | if (!err) |
| 6021 | err = bitmap_load(mddev); |
| 6022 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6023 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6024 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6025 | fd = -1; /* make sure to put the file */ |
| 6026 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6027 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6028 | } |
| 6029 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6030 | if (mddev->bitmap_info.file) { |
| 6031 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 6032 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 6033 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6034 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6035 | } |
| 6036 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6037 | return err; |
| 6038 | } |
| 6039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | /* |
| 6041 | * set_array_info is used two different ways |
| 6042 | * The original usage is when creating a new array. |
| 6043 | * In this usage, raid_disks is > 0 and it together with |
| 6044 | * level, size, not_persistent,layout,chunksize determine the |
| 6045 | * shape of the array. |
| 6046 | * This will always create an array with a type-0.90.0 superblock. |
| 6047 | * The newer usage is when assembling an array. |
| 6048 | * In this case raid_disks will be 0, and the major_version field is |
| 6049 | * use to determine which style super-blocks are to be found on the devices. |
| 6050 | * The minor and patch _version numbers are also kept incase the |
| 6051 | * super_block handler wishes to interpret them. |
| 6052 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6053 | 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] | 6054 | { |
| 6055 | |
| 6056 | if (info->raid_disks == 0) { |
| 6057 | /* just setting version number for superblock loading */ |
| 6058 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6059 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | super_types[info->major_version].name == NULL) { |
| 6061 | /* maybe try to auto-load a module? */ |
| 6062 | printk(KERN_INFO |
| 6063 | "md: superblock version %d not known\n", |
| 6064 | info->major_version); |
| 6065 | return -EINVAL; |
| 6066 | } |
| 6067 | mddev->major_version = info->major_version; |
| 6068 | mddev->minor_version = info->minor_version; |
| 6069 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6070 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6071 | /* ensure mddev_put doesn't delete this now that there |
| 6072 | * is some minimal configuration. |
| 6073 | */ |
| 6074 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | return 0; |
| 6076 | } |
| 6077 | mddev->major_version = MD_MAJOR_VERSION; |
| 6078 | mddev->minor_version = MD_MINOR_VERSION; |
| 6079 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6080 | mddev->ctime = get_seconds(); |
| 6081 | |
| 6082 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6083 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6084 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | mddev->raid_disks = info->raid_disks; |
| 6086 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6087 | * openned |
| 6088 | */ |
| 6089 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6090 | mddev->recovery_cp = MaxSector; |
| 6091 | else |
| 6092 | mddev->recovery_cp = 0; |
| 6093 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6094 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | |
| 6096 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6097 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | |
| 6099 | mddev->max_disks = MD_SB_DISKS; |
| 6100 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6101 | if (mddev->persistent) |
| 6102 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6103 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6105 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6106 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6107 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6108 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6109 | mddev->reshape_position = MaxSector; |
| 6110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | /* |
| 6112 | * Generate a 128 bit UUID |
| 6113 | */ |
| 6114 | get_random_bytes(mddev->uuid, 16); |
| 6115 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6116 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6117 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6118 | mddev->new_layout = mddev->layout; |
| 6119 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6120 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | return 0; |
| 6123 | } |
| 6124 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6125 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6126 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6127 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6128 | |
| 6129 | if (mddev->external_size) |
| 6130 | return; |
| 6131 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6132 | mddev->array_sectors = array_sectors; |
| 6133 | } |
| 6134 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6135 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6136 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6137 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6138 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6139 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6140 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6141 | |
| 6142 | if (mddev->pers->resize == NULL) |
| 6143 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6144 | /* The "num_sectors" is the number of sectors of each device that |
| 6145 | * is used. This can only make sense for arrays with redundancy. |
| 6146 | * linear and raid0 always use whatever space is available. We can only |
| 6147 | * consider changing this number if no resync or reconstruction is |
| 6148 | * 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] | 6149 | * 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] | 6150 | * of each device. If num_sectors is zero, we find the largest size |
| 6151 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6152 | */ |
| 6153 | if (mddev->sync_thread) |
| 6154 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6155 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6156 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6157 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6158 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6159 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6160 | num_sectors = avail; |
| 6161 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6162 | return -ENOSPC; |
| 6163 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6164 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6165 | if (!rv) |
| 6166 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6167 | return rv; |
| 6168 | } |
| 6169 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6170 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6171 | { |
| 6172 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6173 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6174 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6175 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6176 | return -EINVAL; |
| 6177 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6178 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6179 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6180 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6181 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6182 | |
| 6183 | rdev_for_each(rdev, mddev) { |
| 6184 | if (mddev->raid_disks < raid_disks && |
| 6185 | rdev->data_offset < rdev->new_data_offset) |
| 6186 | return -EINVAL; |
| 6187 | if (mddev->raid_disks > raid_disks && |
| 6188 | rdev->data_offset > rdev->new_data_offset) |
| 6189 | return -EINVAL; |
| 6190 | } |
| 6191 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6192 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6193 | if (mddev->delta_disks < 0) |
| 6194 | mddev->reshape_backwards = 1; |
| 6195 | else if (mddev->delta_disks > 0) |
| 6196 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6197 | |
| 6198 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6199 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6200 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6201 | mddev->reshape_backwards = 0; |
| 6202 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6203 | return rv; |
| 6204 | } |
| 6205 | |
| 6206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | /* |
| 6208 | * update_array_info is used to change the configuration of an |
| 6209 | * on-line array. |
| 6210 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6211 | * fields in the info are checked against the array. |
| 6212 | * Any differences that cannot be handled will cause an error. |
| 6213 | * Normally, only one change can be managed at a time. |
| 6214 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6215 | 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] | 6216 | { |
| 6217 | int rv = 0; |
| 6218 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6219 | int state = 0; |
| 6220 | |
| 6221 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6222 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6223 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
| 6225 | if (mddev->major_version != info->major_version || |
| 6226 | mddev->minor_version != info->minor_version || |
| 6227 | /* mddev->patch_version != info->patch_version || */ |
| 6228 | mddev->ctime != info->ctime || |
| 6229 | mddev->level != info->level || |
| 6230 | /* mddev->layout != info->layout || */ |
| 6231 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6232 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6233 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6234 | ((state^info->state) & 0xfffffe00) |
| 6235 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6236 | return -EINVAL; |
| 6237 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6238 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6239 | cnt++; |
| 6240 | if (mddev->raid_disks != info->raid_disks) |
| 6241 | cnt++; |
| 6242 | if (mddev->layout != info->layout) |
| 6243 | cnt++; |
| 6244 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6245 | cnt++; |
| 6246 | if (cnt == 0) |
| 6247 | return 0; |
| 6248 | if (cnt > 1) |
| 6249 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | |
| 6251 | if (mddev->layout != info->layout) { |
| 6252 | /* Change layout |
| 6253 | * we don't need to do anything at the md level, the |
| 6254 | * personality will take care of it all. |
| 6255 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6256 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6258 | else { |
| 6259 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6260 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6261 | if (rv) |
| 6262 | mddev->new_layout = mddev->layout; |
| 6263 | return rv; |
| 6264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6266 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6267 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6269 | if (mddev->raid_disks != info->raid_disks) |
| 6270 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6272 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6273 | if (mddev->pers->quiesce == NULL) |
| 6274 | return -EINVAL; |
| 6275 | if (mddev->recovery || mddev->sync_thread) |
| 6276 | return -EBUSY; |
| 6277 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6278 | /* add the bitmap */ |
| 6279 | if (mddev->bitmap) |
| 6280 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6281 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6282 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6283 | mddev->bitmap_info.offset = |
| 6284 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6285 | mddev->bitmap_info.space = |
| 6286 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6287 | mddev->pers->quiesce(mddev, 1); |
| 6288 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6289 | if (!rv) |
| 6290 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6291 | if (rv) |
| 6292 | bitmap_destroy(mddev); |
| 6293 | mddev->pers->quiesce(mddev, 0); |
| 6294 | } else { |
| 6295 | /* remove the bitmap */ |
| 6296 | if (!mddev->bitmap) |
| 6297 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6298 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6299 | return -EINVAL; |
| 6300 | mddev->pers->quiesce(mddev, 1); |
| 6301 | bitmap_destroy(mddev); |
| 6302 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6303 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6304 | } |
| 6305 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6306 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | return rv; |
| 6308 | } |
| 6309 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6310 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6312 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | |
| 6314 | if (mddev->pers == NULL) |
| 6315 | return -ENODEV; |
| 6316 | |
| 6317 | rdev = find_rdev(mddev, dev); |
| 6318 | if (!rdev) |
| 6319 | return -ENODEV; |
| 6320 | |
| 6321 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6322 | if (!test_bit(Faulty, &rdev->flags)) |
| 6323 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | return 0; |
| 6325 | } |
| 6326 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6327 | /* |
| 6328 | * We have a problem here : there is no easy way to give a CHS |
| 6329 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6330 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6331 | * dosfs just mad... ;-) |
| 6332 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6333 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6334 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6335 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6336 | |
| 6337 | geo->heads = 2; |
| 6338 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6339 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6340 | return 0; |
| 6341 | } |
| 6342 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6343 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | unsigned int cmd, unsigned long arg) |
| 6345 | { |
| 6346 | int err = 0; |
| 6347 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6348 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6349 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6351 | switch (cmd) { |
| 6352 | case RAID_VERSION: |
| 6353 | case GET_ARRAY_INFO: |
| 6354 | case GET_DISK_INFO: |
| 6355 | break; |
| 6356 | default: |
| 6357 | if (!capable(CAP_SYS_ADMIN)) |
| 6358 | return -EACCES; |
| 6359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | |
| 6361 | /* |
| 6362 | * Commands dealing with the RAID driver but not any |
| 6363 | * particular array: |
| 6364 | */ |
| 6365 | switch (cmd) |
| 6366 | { |
| 6367 | case RAID_VERSION: |
| 6368 | err = get_version(argp); |
| 6369 | goto done; |
| 6370 | |
| 6371 | case PRINT_RAID_DEBUG: |
| 6372 | err = 0; |
| 6373 | md_print_devices(); |
| 6374 | goto done; |
| 6375 | |
| 6376 | #ifndef MODULE |
| 6377 | case RAID_AUTORUN: |
| 6378 | err = 0; |
| 6379 | autostart_arrays(arg); |
| 6380 | goto done; |
| 6381 | #endif |
| 6382 | default:; |
| 6383 | } |
| 6384 | |
| 6385 | /* |
| 6386 | * Commands creating/starting a new array: |
| 6387 | */ |
| 6388 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6389 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | |
| 6391 | if (!mddev) { |
| 6392 | BUG(); |
| 6393 | goto abort; |
| 6394 | } |
| 6395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | err = mddev_lock(mddev); |
| 6397 | if (err) { |
| 6398 | printk(KERN_INFO |
| 6399 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6400 | err, cmd); |
| 6401 | goto abort; |
| 6402 | } |
| 6403 | |
| 6404 | switch (cmd) |
| 6405 | { |
| 6406 | case SET_ARRAY_INFO: |
| 6407 | { |
| 6408 | mdu_array_info_t info; |
| 6409 | if (!arg) |
| 6410 | memset(&info, 0, sizeof(info)); |
| 6411 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6412 | err = -EFAULT; |
| 6413 | goto abort_unlock; |
| 6414 | } |
| 6415 | if (mddev->pers) { |
| 6416 | err = update_array_info(mddev, &info); |
| 6417 | if (err) { |
| 6418 | printk(KERN_WARNING "md: couldn't update" |
| 6419 | " array info. %d\n", err); |
| 6420 | goto abort_unlock; |
| 6421 | } |
| 6422 | goto done_unlock; |
| 6423 | } |
| 6424 | if (!list_empty(&mddev->disks)) { |
| 6425 | printk(KERN_WARNING |
| 6426 | "md: array %s already has disks!\n", |
| 6427 | mdname(mddev)); |
| 6428 | err = -EBUSY; |
| 6429 | goto abort_unlock; |
| 6430 | } |
| 6431 | if (mddev->raid_disks) { |
| 6432 | printk(KERN_WARNING |
| 6433 | "md: array %s already initialised!\n", |
| 6434 | mdname(mddev)); |
| 6435 | err = -EBUSY; |
| 6436 | goto abort_unlock; |
| 6437 | } |
| 6438 | err = set_array_info(mddev, &info); |
| 6439 | if (err) { |
| 6440 | printk(KERN_WARNING "md: couldn't set" |
| 6441 | " array info. %d\n", err); |
| 6442 | goto abort_unlock; |
| 6443 | } |
| 6444 | } |
| 6445 | goto done_unlock; |
| 6446 | |
| 6447 | default:; |
| 6448 | } |
| 6449 | |
| 6450 | /* |
| 6451 | * Commands querying/configuring an existing array: |
| 6452 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6453 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6454 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6455 | if ((!mddev->raid_disks && !mddev->external) |
| 6456 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6457 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6458 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | err = -ENODEV; |
| 6460 | goto abort_unlock; |
| 6461 | } |
| 6462 | |
| 6463 | /* |
| 6464 | * Commands even a read-only array can execute: |
| 6465 | */ |
| 6466 | switch (cmd) |
| 6467 | { |
| 6468 | case GET_ARRAY_INFO: |
| 6469 | err = get_array_info(mddev, argp); |
| 6470 | goto done_unlock; |
| 6471 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6472 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6473 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6474 | goto done_unlock; |
| 6475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | case GET_DISK_INFO: |
| 6477 | err = get_disk_info(mddev, argp); |
| 6478 | goto done_unlock; |
| 6479 | |
| 6480 | case RESTART_ARRAY_RW: |
| 6481 | err = restart_array(mddev); |
| 6482 | goto done_unlock; |
| 6483 | |
| 6484 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6485 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | goto done_unlock; |
| 6487 | |
| 6488 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6489 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | goto done_unlock; |
| 6491 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6492 | case BLKROSET: |
| 6493 | if (get_user(ro, (int __user *)(arg))) { |
| 6494 | err = -EFAULT; |
| 6495 | goto done_unlock; |
| 6496 | } |
| 6497 | err = -EINVAL; |
| 6498 | |
| 6499 | /* if the bdev is going readonly the value of mddev->ro |
| 6500 | * does not matter, no writes are coming |
| 6501 | */ |
| 6502 | if (ro) |
| 6503 | goto done_unlock; |
| 6504 | |
| 6505 | /* are we are already prepared for writes? */ |
| 6506 | if (mddev->ro != 1) |
| 6507 | goto done_unlock; |
| 6508 | |
| 6509 | /* transitioning to readauto need only happen for |
| 6510 | * arrays that call md_write_start |
| 6511 | */ |
| 6512 | if (mddev->pers) { |
| 6513 | err = restart_array(mddev); |
| 6514 | if (err == 0) { |
| 6515 | mddev->ro = 2; |
| 6516 | set_disk_ro(mddev->gendisk, 0); |
| 6517 | } |
| 6518 | } |
| 6519 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | /* |
| 6523 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6524 | * superblock, so we do not allow them on read-only arrays. |
| 6525 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6526 | * here and hit the 'default' below, so only disallow |
| 6527 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6529 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6530 | if (mddev->ro == 2) { |
| 6531 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6532 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6533 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6534 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6535 | } else { |
| 6536 | err = -EROFS; |
| 6537 | goto abort_unlock; |
| 6538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | } |
| 6540 | |
| 6541 | switch (cmd) |
| 6542 | { |
| 6543 | case ADD_NEW_DISK: |
| 6544 | { |
| 6545 | mdu_disk_info_t info; |
| 6546 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6547 | err = -EFAULT; |
| 6548 | else |
| 6549 | err = add_new_disk(mddev, &info); |
| 6550 | goto done_unlock; |
| 6551 | } |
| 6552 | |
| 6553 | case HOT_REMOVE_DISK: |
| 6554 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6555 | goto done_unlock; |
| 6556 | |
| 6557 | case HOT_ADD_DISK: |
| 6558 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6559 | goto done_unlock; |
| 6560 | |
| 6561 | case SET_DISK_FAULTY: |
| 6562 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6563 | goto done_unlock; |
| 6564 | |
| 6565 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6566 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | goto done_unlock; |
| 6568 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6569 | case SET_BITMAP_FILE: |
| 6570 | err = set_bitmap_file(mddev, (int)arg); |
| 6571 | goto done_unlock; |
| 6572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | err = -EINVAL; |
| 6575 | goto abort_unlock; |
| 6576 | } |
| 6577 | |
| 6578 | done_unlock: |
| 6579 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6580 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6581 | err != -EINVAL) |
| 6582 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | mddev_unlock(mddev); |
| 6584 | |
| 6585 | return err; |
| 6586 | done: |
| 6587 | if (err) |
| 6588 | MD_BUG(); |
| 6589 | abort: |
| 6590 | return err; |
| 6591 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6592 | #ifdef CONFIG_COMPAT |
| 6593 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6594 | unsigned int cmd, unsigned long arg) |
| 6595 | { |
| 6596 | switch (cmd) { |
| 6597 | case HOT_REMOVE_DISK: |
| 6598 | case HOT_ADD_DISK: |
| 6599 | case SET_DISK_FAULTY: |
| 6600 | case SET_BITMAP_FILE: |
| 6601 | /* These take in integer arg, do not convert */ |
| 6602 | break; |
| 6603 | default: |
| 6604 | arg = (unsigned long)compat_ptr(arg); |
| 6605 | break; |
| 6606 | } |
| 6607 | |
| 6608 | return md_ioctl(bdev, mode, cmd, arg); |
| 6609 | } |
| 6610 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6612 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | { |
| 6614 | /* |
| 6615 | * Succeed if we can lock the mddev, which confirms that |
| 6616 | * it isn't being stopped right now. |
| 6617 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6618 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | int err; |
| 6620 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame^] | 6621 | if (!mddev) |
| 6622 | return -ENODEV; |
| 6623 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6624 | if (mddev->gendisk != bdev->bd_disk) { |
| 6625 | /* we are racing with mddev_put which is discarding this |
| 6626 | * bd_disk. |
| 6627 | */ |
| 6628 | mddev_put(mddev); |
| 6629 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6630 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6631 | /* Then retry the open from the top */ |
| 6632 | return -ERESTARTSYS; |
| 6633 | } |
| 6634 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6635 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6636 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | goto out; |
| 6638 | |
| 6639 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6640 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6641 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6643 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | out: |
| 6645 | return err; |
| 6646 | } |
| 6647 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6648 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6650 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6652 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6653 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | mddev_put(mddev); |
| 6655 | |
| 6656 | return 0; |
| 6657 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6658 | |
| 6659 | static int md_media_changed(struct gendisk *disk) |
| 6660 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6661 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6662 | |
| 6663 | return mddev->changed; |
| 6664 | } |
| 6665 | |
| 6666 | static int md_revalidate(struct gendisk *disk) |
| 6667 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6668 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6669 | |
| 6670 | mddev->changed = 0; |
| 6671 | return 0; |
| 6672 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6673 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | { |
| 6675 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6676 | .open = md_open, |
| 6677 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6678 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6679 | #ifdef CONFIG_COMPAT |
| 6680 | .compat_ioctl = md_compat_ioctl, |
| 6681 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6682 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6683 | .media_changed = md_media_changed, |
| 6684 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | }; |
| 6686 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6687 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6689 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | /* |
| 6692 | * md_thread is a 'system-thread', it's priority should be very |
| 6693 | * high. We avoid resource deadlocks individually in each |
| 6694 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6695 | * the very same RT priority as kswapd, thus we will never get |
| 6696 | * into a priority inversion deadlock. |
| 6697 | * |
| 6698 | * we definitely have to have equal or higher priority than |
| 6699 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6700 | * many dirty RAID5 blocks. |
| 6701 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6703 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6704 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6706 | /* We need to wait INTERRUPTIBLE so that |
| 6707 | * we don't add to the load-average. |
| 6708 | * That means we need to be sure no signals are |
| 6709 | * pending |
| 6710 | */ |
| 6711 | if (signal_pending(current)) |
| 6712 | flush_signals(current); |
| 6713 | |
| 6714 | wait_event_interruptible_timeout |
| 6715 | (thread->wqueue, |
| 6716 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6717 | || kthread_should_stop(), |
| 6718 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6720 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6721 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6722 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | return 0; |
| 6726 | } |
| 6727 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6728 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | { |
| 6730 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6731 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6733 | wake_up(&thread->wqueue); |
| 6734 | } |
| 6735 | } |
| 6736 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6737 | 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] | 6738 | const char *name) |
| 6739 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6740 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6742 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | if (!thread) |
| 6744 | return NULL; |
| 6745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | init_waitqueue_head(&thread->wqueue); |
| 6747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | thread->run = run; |
| 6749 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6750 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6751 | thread->tsk = kthread_run(md_thread, thread, |
| 6752 | "%s_%s", |
| 6753 | mdname(thread->mddev), |
| 6754 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6755 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | kfree(thread); |
| 6757 | return NULL; |
| 6758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6759 | return thread; |
| 6760 | } |
| 6761 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6762 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6764 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6765 | if (!thread) |
| 6766 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6767 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6768 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6769 | * non-existent thread |
| 6770 | */ |
| 6771 | spin_lock(&pers_lock); |
| 6772 | *threadp = NULL; |
| 6773 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6774 | |
| 6775 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | kfree(thread); |
| 6777 | } |
| 6778 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6779 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6780 | { |
| 6781 | if (!mddev) { |
| 6782 | MD_BUG(); |
| 6783 | return; |
| 6784 | } |
| 6785 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6786 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6788 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6789 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | return; |
| 6791 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6792 | if (mddev->degraded) |
| 6793 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6794 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6796 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6797 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6798 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6799 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6800 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | /* seq_file implementation /proc/mdstat */ |
| 6804 | |
| 6805 | static void status_unused(struct seq_file *seq) |
| 6806 | { |
| 6807 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6808 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | |
| 6810 | seq_printf(seq, "unused devices: "); |
| 6811 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6812 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | char b[BDEVNAME_SIZE]; |
| 6814 | i++; |
| 6815 | seq_printf(seq, "%s ", |
| 6816 | bdevname(rdev->bdev,b)); |
| 6817 | } |
| 6818 | if (!i) |
| 6819 | seq_printf(seq, "<none>"); |
| 6820 | |
| 6821 | seq_printf(seq, "\n"); |
| 6822 | } |
| 6823 | |
| 6824 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6825 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6827 | sector_t max_sectors, resync, res; |
| 6828 | unsigned long dt, db; |
| 6829 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6830 | int scale; |
| 6831 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6833 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6835 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6836 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6837 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6839 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | |
| 6841 | /* |
| 6842 | * Should not happen. |
| 6843 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6844 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | MD_BUG(); |
| 6846 | return; |
| 6847 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6848 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6849 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6850 | * u32, as those are the requirements for sector_div. |
| 6851 | * Thus 'scale' must be at least 10 |
| 6852 | */ |
| 6853 | scale = 10; |
| 6854 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6855 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6856 | scale++; |
| 6857 | } |
| 6858 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6859 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6860 | |
| 6861 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6863 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | seq_printf(seq, "["); |
| 6865 | for (i = 0; i < x; i++) |
| 6866 | seq_printf(seq, "="); |
| 6867 | seq_printf(seq, ">"); |
| 6868 | for (i = 0; i < y; i++) |
| 6869 | seq_printf(seq, "."); |
| 6870 | seq_printf(seq, "] "); |
| 6871 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6872 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6873 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6874 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6875 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6876 | "check" : |
| 6877 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6878 | "resync" : "recovery"))), |
| 6879 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6880 | (unsigned long long) resync/2, |
| 6881 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | |
| 6883 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | * dt: time from mark until now |
| 6885 | * db: blocks written from mark until now |
| 6886 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6887 | * |
| 6888 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6889 | * So we divide before multiply in case it is 32bit and close |
| 6890 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6891 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6892 | * near the end of resync when the number of remaining sectors |
| 6893 | * is close to 'db'. |
| 6894 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6895 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | */ |
| 6897 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6898 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6899 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6900 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6902 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6903 | sector_div(rt, db/32+1); |
| 6904 | rt *= dt; |
| 6905 | rt >>= 5; |
| 6906 | |
| 6907 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6908 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6910 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6914 | { |
| 6915 | struct list_head *tmp; |
| 6916 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6917 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | |
| 6919 | if (l >= 0x10000) |
| 6920 | return NULL; |
| 6921 | if (!l--) |
| 6922 | /* header */ |
| 6923 | return (void*)1; |
| 6924 | |
| 6925 | spin_lock(&all_mddevs_lock); |
| 6926 | list_for_each(tmp,&all_mddevs) |
| 6927 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6928 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | mddev_get(mddev); |
| 6930 | spin_unlock(&all_mddevs_lock); |
| 6931 | return mddev; |
| 6932 | } |
| 6933 | spin_unlock(&all_mddevs_lock); |
| 6934 | if (!l--) |
| 6935 | return (void*)2;/* tail */ |
| 6936 | return NULL; |
| 6937 | } |
| 6938 | |
| 6939 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6940 | { |
| 6941 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6942 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | |
| 6944 | ++*pos; |
| 6945 | if (v == (void*)2) |
| 6946 | return NULL; |
| 6947 | |
| 6948 | spin_lock(&all_mddevs_lock); |
| 6949 | if (v == (void*)1) |
| 6950 | tmp = all_mddevs.next; |
| 6951 | else |
| 6952 | tmp = mddev->all_mddevs.next; |
| 6953 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6954 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | else { |
| 6956 | next_mddev = (void*)2; |
| 6957 | *pos = 0x10000; |
| 6958 | } |
| 6959 | spin_unlock(&all_mddevs_lock); |
| 6960 | |
| 6961 | if (v != (void*)1) |
| 6962 | mddev_put(mddev); |
| 6963 | return next_mddev; |
| 6964 | |
| 6965 | } |
| 6966 | |
| 6967 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6968 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6969 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | |
| 6971 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6972 | mddev_put(mddev); |
| 6973 | } |
| 6974 | |
| 6975 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6976 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6977 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6978 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6979 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | |
| 6981 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6982 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | seq_printf(seq, "Personalities : "); |
| 6984 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6985 | list_for_each_entry(pers, &pers_list, list) |
| 6986 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | |
| 6988 | spin_unlock(&pers_lock); |
| 6989 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6990 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | return 0; |
| 6992 | } |
| 6993 | if (v == (void*)2) { |
| 6994 | status_unused(seq); |
| 6995 | return 0; |
| 6996 | } |
| 6997 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6998 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6999 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7002 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7003 | mddev->pers ? "" : "in"); |
| 7004 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7005 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7006 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7007 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7008 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | seq_printf(seq, " %s", mddev->pers->name); |
| 7010 | } |
| 7011 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7012 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7013 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | char b[BDEVNAME_SIZE]; |
| 7015 | seq_printf(seq, " %s[%d]", |
| 7016 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7017 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7018 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7019 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7020 | seq_printf(seq, "(F)"); |
| 7021 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7022 | } |
| 7023 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7024 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7025 | if (test_bit(Replacement, &rdev->flags)) |
| 7026 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7027 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | } |
| 7029 | |
| 7030 | if (!list_empty(&mddev->disks)) { |
| 7031 | if (mddev->pers) |
| 7032 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7033 | (unsigned long long) |
| 7034 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | else |
| 7036 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7037 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7039 | if (mddev->persistent) { |
| 7040 | if (mddev->major_version != 0 || |
| 7041 | mddev->minor_version != 90) { |
| 7042 | seq_printf(seq," super %d.%d", |
| 7043 | mddev->major_version, |
| 7044 | mddev->minor_version); |
| 7045 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7046 | } else if (mddev->external) |
| 7047 | seq_printf(seq, " super external:%s", |
| 7048 | mddev->metadata_type); |
| 7049 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7050 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | |
| 7052 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7053 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7055 | if (mddev->pers->sync_request) { |
| 7056 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7057 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7058 | seq_printf(seq, "\n "); |
| 7059 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 7060 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7061 | else if (mddev->recovery_cp < MaxSector) |
| 7062 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7063 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7064 | } else |
| 7065 | seq_printf(seq, "\n "); |
| 7066 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7067 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | |
| 7069 | seq_printf(seq, "\n"); |
| 7070 | } |
| 7071 | mddev_unlock(mddev); |
| 7072 | |
| 7073 | return 0; |
| 7074 | } |
| 7075 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7076 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | .start = md_seq_start, |
| 7078 | .next = md_seq_next, |
| 7079 | .stop = md_seq_stop, |
| 7080 | .show = md_seq_show, |
| 7081 | }; |
| 7082 | |
| 7083 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7084 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7085 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7086 | int error; |
| 7087 | |
| 7088 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7089 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7090 | return error; |
| 7091 | |
| 7092 | seq = file->private_data; |
| 7093 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7094 | return error; |
| 7095 | } |
| 7096 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7097 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7098 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7099 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7100 | int mask; |
| 7101 | |
| 7102 | poll_wait(filp, &md_event_waiters, wait); |
| 7103 | |
| 7104 | /* always allow read */ |
| 7105 | mask = POLLIN | POLLRDNORM; |
| 7106 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7107 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7108 | mask |= POLLERR | POLLPRI; |
| 7109 | return mask; |
| 7110 | } |
| 7111 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7112 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7113 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | .open = md_seq_open, |
| 7115 | .read = seq_read, |
| 7116 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7117 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7118 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | }; |
| 7120 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7121 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7124 | list_add_tail(&p->list, &pers_list); |
| 7125 | 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] | 7126 | spin_unlock(&pers_lock); |
| 7127 | return 0; |
| 7128 | } |
| 7129 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7130 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7132 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7134 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | spin_unlock(&pers_lock); |
| 7136 | return 0; |
| 7137 | } |
| 7138 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7139 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7141 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7143 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7144 | |
| 7145 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7146 | rcu_read_lock(); |
| 7147 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7149 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7150 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7151 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7152 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7153 | * as sync_io is counted when a request starts, and |
| 7154 | * disk_stats is counted when it completes. |
| 7155 | * So resync activity will cause curr_events to be smaller than |
| 7156 | * when there was no such activity. |
| 7157 | * non-sync IO will cause disk_stat to increase without |
| 7158 | * increasing sync_io so curr_events will (eventually) |
| 7159 | * be larger than it was before. Once it becomes |
| 7160 | * substantially larger, the test below will cause |
| 7161 | * the array to appear non-idle, and resync will slow |
| 7162 | * down. |
| 7163 | * If there is a lot of outstanding resync activity when |
| 7164 | * we set last_event to curr_events, then all that activity |
| 7165 | * completing might cause the array to appear non-idle |
| 7166 | * and resync will be slowed down even though there might |
| 7167 | * not have been non-resync activity. This will only |
| 7168 | * happen once though. 'last_events' will soon reflect |
| 7169 | * the state where there is little or no outstanding |
| 7170 | * resync requests, and further resync activity will |
| 7171 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7172 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7173 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7174 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | rdev->last_events = curr_events; |
| 7176 | idle = 0; |
| 7177 | } |
| 7178 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7179 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | return idle; |
| 7181 | } |
| 7182 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7183 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7184 | { |
| 7185 | /* another "blocks" (512byte) blocks have been synced */ |
| 7186 | atomic_sub(blocks, &mddev->recovery_active); |
| 7187 | wake_up(&mddev->recovery_wait); |
| 7188 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7189 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | md_wakeup_thread(mddev->thread); |
| 7191 | // stop recovery, signal do_sync .... |
| 7192 | } |
| 7193 | } |
| 7194 | |
| 7195 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7196 | /* md_write_start(mddev, bi) |
| 7197 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7198 | * in superblock) before writing, schedule a superblock update |
| 7199 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7200 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7201 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7203 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7204 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7205 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7206 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7207 | BUG_ON(mddev->ro == 1); |
| 7208 | if (mddev->ro == 2) { |
| 7209 | /* need to switch to read/write */ |
| 7210 | mddev->ro = 0; |
| 7211 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7212 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7213 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7214 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7215 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7216 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7217 | if (mddev->safemode == 1) |
| 7218 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7219 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7220 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7221 | if (mddev->in_sync) { |
| 7222 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7223 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7224 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7225 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7226 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7227 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7228 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7229 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7230 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7231 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7232 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7233 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | } |
| 7235 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7236 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | { |
| 7238 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7239 | if (mddev->safemode == 2) |
| 7240 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7241 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7243 | } |
| 7244 | } |
| 7245 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7246 | /* md_allow_write(mddev) |
| 7247 | * Calling this ensures that the array is marked 'active' so that writes |
| 7248 | * may proceed without blocking. It is important to call this before |
| 7249 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7250 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7251 | * |
| 7252 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7253 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7254 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7255 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7256 | { |
| 7257 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7258 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7259 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7260 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7261 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7262 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7263 | |
| 7264 | spin_lock_irq(&mddev->write_lock); |
| 7265 | if (mddev->in_sync) { |
| 7266 | mddev->in_sync = 0; |
| 7267 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7268 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7269 | if (mddev->safemode_delay && |
| 7270 | mddev->safemode == 0) |
| 7271 | mddev->safemode = 1; |
| 7272 | spin_unlock_irq(&mddev->write_lock); |
| 7273 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7274 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7275 | } else |
| 7276 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7277 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7278 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7279 | return -EAGAIN; |
| 7280 | else |
| 7281 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7282 | } |
| 7283 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | #define SYNC_MARKS 10 |
| 7286 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7287 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7289 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | unsigned int currspeed = 0, |
| 7291 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7292 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | unsigned long mark[SYNC_MARKS]; |
| 7294 | sector_t mark_cnt[SYNC_MARKS]; |
| 7295 | int last_mark,m; |
| 7296 | struct list_head *tmp; |
| 7297 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7298 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7299 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7300 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | |
| 7302 | /* just incase thread restarts... */ |
| 7303 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7304 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7305 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7306 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7308 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7309 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7310 | desc = "data-check"; |
| 7311 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7312 | desc = "requested-resync"; |
| 7313 | else |
| 7314 | desc = "resync"; |
| 7315 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7316 | desc = "reshape"; |
| 7317 | else |
| 7318 | desc = "recovery"; |
| 7319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | /* we overload curr_resync somewhat here. |
| 7321 | * 0 == not engaged in resync at all |
| 7322 | * 2 == checking that there is no conflict with another sync |
| 7323 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7324 | * commense |
| 7325 | * other == active in resync - this many blocks |
| 7326 | * |
| 7327 | * Before starting a resync we must have set curr_resync to |
| 7328 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7329 | * less than ours. When we find one that is the same or higher |
| 7330 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7331 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7332 | * This will mean we have to start checking from the beginning again. |
| 7333 | * |
| 7334 | */ |
| 7335 | |
| 7336 | do { |
| 7337 | mddev->curr_resync = 2; |
| 7338 | |
| 7339 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7340 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7341 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7342 | |
| 7343 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7345 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | if (mddev2 == mddev) |
| 7347 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7348 | if (!mddev->parallel_resync |
| 7349 | && mddev2->curr_resync |
| 7350 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | DEFINE_WAIT(wq); |
| 7352 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7353 | /* arbitrarily yield */ |
| 7354 | mddev->curr_resync = 1; |
| 7355 | wake_up(&resync_wait); |
| 7356 | } |
| 7357 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7358 | /* no need to wait here, we can wait the next |
| 7359 | * time 'round when curr_resync == 2 |
| 7360 | */ |
| 7361 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7362 | /* We need to wait 'interruptible' so as not to |
| 7363 | * contribute to the load average, and not to |
| 7364 | * be caught by 'softlockup' |
| 7365 | */ |
| 7366 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7367 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7368 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7369 | printk(KERN_INFO "md: delaying %s of %s" |
| 7370 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7371 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7372 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7373 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7374 | if (signal_pending(current)) |
| 7375 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | schedule(); |
| 7377 | finish_wait(&resync_wait, &wq); |
| 7378 | goto try_again; |
| 7379 | } |
| 7380 | finish_wait(&resync_wait, &wq); |
| 7381 | } |
| 7382 | } |
| 7383 | } while (mddev->curr_resync < 2); |
| 7384 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7385 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7386 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7388 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | */ |
| 7390 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7391 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7392 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7393 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7394 | j = mddev->resync_min; |
| 7395 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7396 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7397 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7398 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7399 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7400 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7401 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7402 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7403 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7404 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7405 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7406 | if (rdev->raid_disk >= 0 && |
| 7407 | !test_bit(Faulty, &rdev->flags) && |
| 7408 | !test_bit(In_sync, &rdev->flags) && |
| 7409 | rdev->recovery_offset < j) |
| 7410 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7411 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7414 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7415 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7416 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7417 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7418 | "(but not more than %d KB/sec) for %s.\n", |
| 7419 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7421 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7422 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7423 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7425 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7426 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | } |
| 7428 | last_mark = 0; |
| 7429 | mddev->resync_mark = mark[last_mark]; |
| 7430 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7431 | |
| 7432 | /* |
| 7433 | * Tune reconstruction: |
| 7434 | */ |
| 7435 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7436 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7437 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7438 | |
| 7439 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | last_check = 0; |
| 7441 | |
| 7442 | if (j>2) { |
| 7443 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7444 | "md: resuming %s of %s from checkpoint.\n", |
| 7445 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | mddev->curr_resync = j; |
| 7447 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7448 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | |
| 7450 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7451 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7452 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7453 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7454 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7455 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7456 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7457 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7458 | > (max_sectors >> 4)) || |
| 7459 | (j - mddev->curr_resync_completed)*2 |
| 7460 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7461 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7462 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7463 | wait_event(mddev->recovery_wait, |
| 7464 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7465 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7466 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7467 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7468 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7469 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7470 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7471 | /* As this condition is controlled by user-space, |
| 7472 | * we can block indefinitely, so use '_interruptible' |
| 7473 | * to avoid triggering warnings. |
| 7474 | */ |
| 7475 | flush_signals(current); /* just in case */ |
| 7476 | wait_event_interruptible(mddev->recovery_wait, |
| 7477 | mddev->resync_max > j |
| 7478 | || kthread_should_stop()); |
| 7479 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7480 | |
| 7481 | if (kthread_should_stop()) |
| 7482 | goto interrupted; |
| 7483 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7484 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7485 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7486 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7487 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | goto out; |
| 7489 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7490 | |
| 7491 | if (!skipped) { /* actual IO requested */ |
| 7492 | io_sectors += sectors; |
| 7493 | atomic_add(sectors, &mddev->recovery_active); |
| 7494 | } |
| 7495 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7496 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7497 | break; |
| 7498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | j += sectors; |
| 7500 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7501 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7502 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7503 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7504 | * visible in /proc/mdstat |
| 7505 | */ |
| 7506 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7507 | |
| 7508 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | continue; |
| 7510 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7511 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | repeat: |
| 7513 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7514 | /* step marks */ |
| 7515 | int next = (last_mark+1) % SYNC_MARKS; |
| 7516 | |
| 7517 | mddev->resync_mark = mark[next]; |
| 7518 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7519 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7520 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7521 | last_mark = next; |
| 7522 | } |
| 7523 | |
| 7524 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7525 | if (kthread_should_stop()) |
| 7526 | goto interrupted; |
| 7527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | |
| 7529 | /* |
| 7530 | * this loop exits only if either when we are slower than |
| 7531 | * the 'hard' speed limit, or the system was IO-idle for |
| 7532 | * a jiffy. |
| 7533 | * the system might be non-idle CPU-wise, but we only care |
| 7534 | * about not overloading the IO subsystem. (things like an |
| 7535 | * e2fsck being done on the RAID array should execute fast) |
| 7536 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7537 | cond_resched(); |
| 7538 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7539 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7540 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7542 | if (currspeed > speed_min(mddev)) { |
| 7543 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7544 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7545 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7546 | goto repeat; |
| 7547 | } |
| 7548 | } |
| 7549 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7550 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | /* |
| 7552 | * this also signals 'finished resyncing' to md_stop |
| 7553 | */ |
| 7554 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7555 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7556 | |
| 7557 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7558 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7559 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7560 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7561 | mddev->curr_resync > 2) { |
| 7562 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7563 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7564 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7565 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7566 | "md: checkpointing %s of %s.\n", |
| 7567 | desc, mdname(mddev)); |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7568 | mddev->recovery_cp = |
| 7569 | mddev->curr_resync_completed; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7570 | } |
| 7571 | } else |
| 7572 | mddev->recovery_cp = MaxSector; |
| 7573 | } else { |
| 7574 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7575 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7576 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7577 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7578 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7579 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7580 | !test_bit(Faulty, &rdev->flags) && |
| 7581 | !test_bit(In_sync, &rdev->flags) && |
| 7582 | rdev->recovery_offset < mddev->curr_resync) |
| 7583 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7584 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7587 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7588 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7589 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7590 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7591 | /* We completed so min/max setting can be forgotten if used. */ |
| 7592 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7593 | mddev->resync_min = 0; |
| 7594 | mddev->resync_max = MaxSector; |
| 7595 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7596 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7597 | mddev->curr_resync = 0; |
| 7598 | wake_up(&resync_wait); |
| 7599 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7600 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7601 | return; |
| 7602 | |
| 7603 | interrupted: |
| 7604 | /* |
| 7605 | * got a signal, exit. |
| 7606 | */ |
| 7607 | printk(KERN_INFO |
| 7608 | "md: md_do_sync() got signal ... exiting\n"); |
| 7609 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7610 | goto out; |
| 7611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7612 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7613 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7614 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7615 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7616 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7617 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7618 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7619 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7620 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7621 | mddev->curr_resync_completed = 0; |
| 7622 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7623 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7624 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7625 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7626 | (test_bit(Faulty, &rdev->flags) || |
| 7627 | ! test_bit(In_sync, &rdev->flags)) && |
| 7628 | atomic_read(&rdev->nr_pending)==0) { |
| 7629 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7630 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7631 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7632 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7633 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7634 | } |
| 7635 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7636 | if (removed) |
| 7637 | sysfs_notify(&mddev->kobj, NULL, |
| 7638 | "degraded"); |
| 7639 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7640 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7641 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7642 | if (rdev->raid_disk >= 0 && |
| 7643 | !test_bit(In_sync, &rdev->flags) && |
| 7644 | !test_bit(Faulty, &rdev->flags)) |
| 7645 | spares++; |
| 7646 | if (rdev->raid_disk < 0 |
| 7647 | && !test_bit(Faulty, &rdev->flags)) { |
| 7648 | rdev->recovery_offset = 0; |
| 7649 | if (mddev->pers-> |
| 7650 | hot_add_disk(mddev, rdev) == 0) { |
| 7651 | if (sysfs_link_rdev(mddev, rdev)) |
| 7652 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7653 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7654 | md_new_event(mddev); |
| 7655 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7656 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7657 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7658 | } |
| 7659 | return spares; |
| 7660 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7661 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7662 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7663 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7664 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7665 | |
| 7666 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7667 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7668 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7669 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7670 | /* success...*/ |
| 7671 | /* activate any spares */ |
| 7672 | if (mddev->pers->spare_active(mddev)) |
| 7673 | sysfs_notify(&mddev->kobj, NULL, |
| 7674 | "degraded"); |
| 7675 | } |
| 7676 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7677 | mddev->pers->finish_reshape) |
| 7678 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7679 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7680 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7681 | * information must be scrapped. Also if any device is now |
| 7682 | * In_sync we must scrape the saved_raid_disk for that device |
| 7683 | * do the superblock for an incrementally recovered device |
| 7684 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7685 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7686 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7687 | if (!mddev->degraded || |
| 7688 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7689 | rdev->saved_raid_disk = -1; |
| 7690 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7691 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7692 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7693 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7694 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7695 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7696 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7697 | /* flag recovery needed just to double check */ |
| 7698 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7699 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7700 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7701 | if (mddev->event_work.func) |
| 7702 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7703 | } |
| 7704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7705 | /* |
| 7706 | * This routine is regularly called by all per-raid-array threads to |
| 7707 | * deal with generic issues like resync and super-block update. |
| 7708 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7709 | * need this as they never do any recovery or update the superblock. |
| 7710 | * |
| 7711 | * It does not do any resync itself, but rather "forks" off other threads |
| 7712 | * to do that as needed. |
| 7713 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7714 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7715 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7716 | * and wakeups up this thread which will reap the thread and finish up. |
| 7717 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7718 | * |
| 7719 | * The overall approach is: |
| 7720 | * 1/ if the superblock needs updating, update it. |
| 7721 | * 2/ If a recovery thread is running, don't do anything else. |
| 7722 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7723 | * 4/ If there are any faulty devices, remove them. |
| 7724 | * 5/ If array is degraded, try to add spares devices |
| 7725 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7726 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7727 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7728 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7729 | if (mddev->suspended) |
| 7730 | return; |
| 7731 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7732 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7733 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7734 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7735 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7736 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7737 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7738 | mdname(mddev)); |
| 7739 | mddev->safemode = 2; |
| 7740 | } |
| 7741 | flush_signals(current); |
| 7742 | } |
| 7743 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7744 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7745 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7746 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7747 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7749 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7750 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7751 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7752 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7753 | )) |
| 7754 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7755 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7756 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7757 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7758 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7759 | if (mddev->ro) { |
| 7760 | /* Only thing we do on a ro array is remove |
| 7761 | * failed devices. |
| 7762 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7763 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7764 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7765 | if (rdev->raid_disk >= 0 && |
| 7766 | !test_bit(Blocked, &rdev->flags) && |
| 7767 | test_bit(Faulty, &rdev->flags) && |
| 7768 | atomic_read(&rdev->nr_pending)==0) { |
| 7769 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7770 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7771 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7772 | rdev->raid_disk = -1; |
| 7773 | } |
| 7774 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7775 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7776 | goto unlock; |
| 7777 | } |
| 7778 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7779 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7780 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7781 | spin_lock_irq(&mddev->write_lock); |
| 7782 | if (mddev->safemode && |
| 7783 | !atomic_read(&mddev->writes_pending) && |
| 7784 | !mddev->in_sync && |
| 7785 | mddev->recovery_cp == MaxSector) { |
| 7786 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7787 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7788 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7789 | } |
| 7790 | if (mddev->safemode == 1) |
| 7791 | mddev->safemode = 0; |
| 7792 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7793 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7794 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7795 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7796 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7797 | if (mddev->flags) |
| 7798 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7801 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7802 | /* resync/recovery still happening */ |
| 7803 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7804 | goto unlock; |
| 7805 | } |
| 7806 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7807 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | goto unlock; |
| 7809 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7810 | /* Set RUNNING before clearing NEEDED to avoid |
| 7811 | * any transients in the value of "sync_action". |
| 7812 | */ |
| 7813 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7814 | /* Clear some bits that don't mean anything, but |
| 7815 | * might be left set |
| 7816 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7817 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7818 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7819 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7820 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7821 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7822 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7823 | /* no recovery is running. |
| 7824 | * remove any failed drives, then |
| 7825 | * add spares if possible. |
| 7826 | * Spare are also removed and re-added, to allow |
| 7827 | * the personality to fail the re-add. |
| 7828 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7830 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7831 | if (mddev->pers->check_reshape == NULL || |
| 7832 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7833 | /* Cannot proceed */ |
| 7834 | goto unlock; |
| 7835 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7836 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7837 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7838 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7839 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7840 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7841 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7842 | } else if (mddev->recovery_cp < MaxSector) { |
| 7843 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7844 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7845 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7846 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7850 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7851 | /* We are adding a device or devices to an array |
| 7852 | * which has the bitmap stored on all devices. |
| 7853 | * So make sure all bitmap pages get written |
| 7854 | */ |
| 7855 | bitmap_write_all(mddev->bitmap); |
| 7856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7858 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7859 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7860 | if (!mddev->sync_thread) { |
| 7861 | printk(KERN_ERR "%s: could not start resync" |
| 7862 | " thread...\n", |
| 7863 | mdname(mddev)); |
| 7864 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7865 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7866 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7867 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7868 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7869 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7870 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7871 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7872 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7873 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7874 | } |
| 7875 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7876 | if (!mddev->sync_thread) { |
| 7877 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7878 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7879 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7880 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7881 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7883 | mddev_unlock(mddev); |
| 7884 | } |
| 7885 | } |
| 7886 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7887 | 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] | 7888 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7889 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7890 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7891 | !test_bit(Blocked, &rdev->flags) && |
| 7892 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7893 | msecs_to_jiffies(5000)); |
| 7894 | rdev_dec_pending(rdev, mddev); |
| 7895 | } |
| 7896 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7897 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7898 | void md_finish_reshape(struct mddev *mddev) |
| 7899 | { |
| 7900 | /* called be personality module when reshape completes. */ |
| 7901 | struct md_rdev *rdev; |
| 7902 | |
| 7903 | rdev_for_each(rdev, mddev) { |
| 7904 | if (rdev->data_offset > rdev->new_data_offset) |
| 7905 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7906 | else |
| 7907 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7908 | rdev->data_offset = rdev->new_data_offset; |
| 7909 | } |
| 7910 | } |
| 7911 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7912 | |
| 7913 | /* Bad block management. |
| 7914 | * We can record which blocks on each device are 'bad' and so just |
| 7915 | * fail those blocks, or that stripe, rather than the whole device. |
| 7916 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7917 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7918 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7919 | * A 'shift' can be set so that larger blocks are tracked and |
| 7920 | * consequently larger devices can be covered. |
| 7921 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7922 | * |
| 7923 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7924 | * might need to retry if it is very unlucky. |
| 7925 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7926 | * so we use the write_seqlock_irq variant. |
| 7927 | * |
| 7928 | * When looking for a bad block we specify a range and want to |
| 7929 | * know if any block in the range is bad. So we binary-search |
| 7930 | * to the last range that starts at-or-before the given endpoint, |
| 7931 | * (or "before the sector after the target range") |
| 7932 | * then see if it ends after the given start. |
| 7933 | * We return |
| 7934 | * 0 if there are no known bad blocks in the range |
| 7935 | * 1 if there are known bad block which are all acknowledged |
| 7936 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7937 | * plus the start/length of the first bad section we overlap. |
| 7938 | */ |
| 7939 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7940 | sector_t *first_bad, int *bad_sectors) |
| 7941 | { |
| 7942 | int hi; |
| 7943 | int lo = 0; |
| 7944 | u64 *p = bb->page; |
| 7945 | int rv = 0; |
| 7946 | sector_t target = s + sectors; |
| 7947 | unsigned seq; |
| 7948 | |
| 7949 | if (bb->shift > 0) { |
| 7950 | /* round the start down, and the end up */ |
| 7951 | s >>= bb->shift; |
| 7952 | target += (1<<bb->shift) - 1; |
| 7953 | target >>= bb->shift; |
| 7954 | sectors = target - s; |
| 7955 | } |
| 7956 | /* 'target' is now the first block after the bad range */ |
| 7957 | |
| 7958 | retry: |
| 7959 | seq = read_seqbegin(&bb->lock); |
| 7960 | |
| 7961 | hi = bb->count; |
| 7962 | |
| 7963 | /* Binary search between lo and hi for 'target' |
| 7964 | * i.e. for the last range that starts before 'target' |
| 7965 | */ |
| 7966 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7967 | * are known not to be the last range before target. |
| 7968 | * VARIANT: hi-lo is the number of possible |
| 7969 | * ranges, and decreases until it reaches 1 |
| 7970 | */ |
| 7971 | while (hi - lo > 1) { |
| 7972 | int mid = (lo + hi) / 2; |
| 7973 | sector_t a = BB_OFFSET(p[mid]); |
| 7974 | if (a < target) |
| 7975 | /* This could still be the one, earlier ranges |
| 7976 | * could not. */ |
| 7977 | lo = mid; |
| 7978 | else |
| 7979 | /* This and later ranges are definitely out. */ |
| 7980 | hi = mid; |
| 7981 | } |
| 7982 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7983 | if (hi > lo) { |
| 7984 | /* need to check all range that end after 's' to see if |
| 7985 | * any are unacknowledged. |
| 7986 | */ |
| 7987 | while (lo >= 0 && |
| 7988 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7989 | if (BB_OFFSET(p[lo]) < target) { |
| 7990 | /* starts before the end, and finishes after |
| 7991 | * the start, so they must overlap |
| 7992 | */ |
| 7993 | if (rv != -1 && BB_ACK(p[lo])) |
| 7994 | rv = 1; |
| 7995 | else |
| 7996 | rv = -1; |
| 7997 | *first_bad = BB_OFFSET(p[lo]); |
| 7998 | *bad_sectors = BB_LEN(p[lo]); |
| 7999 | } |
| 8000 | lo--; |
| 8001 | } |
| 8002 | } |
| 8003 | |
| 8004 | if (read_seqretry(&bb->lock, seq)) |
| 8005 | goto retry; |
| 8006 | |
| 8007 | return rv; |
| 8008 | } |
| 8009 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8010 | |
| 8011 | /* |
| 8012 | * Add a range of bad blocks to the table. |
| 8013 | * This might extend the table, or might contract it |
| 8014 | * if two adjacent ranges can be merged. |
| 8015 | * We binary-search to find the 'insertion' point, then |
| 8016 | * decide how best to handle it. |
| 8017 | */ |
| 8018 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8019 | int acknowledged) |
| 8020 | { |
| 8021 | u64 *p; |
| 8022 | int lo, hi; |
| 8023 | int rv = 1; |
| 8024 | |
| 8025 | if (bb->shift < 0) |
| 8026 | /* badblocks are disabled */ |
| 8027 | return 0; |
| 8028 | |
| 8029 | if (bb->shift) { |
| 8030 | /* round the start down, and the end up */ |
| 8031 | sector_t next = s + sectors; |
| 8032 | s >>= bb->shift; |
| 8033 | next += (1<<bb->shift) - 1; |
| 8034 | next >>= bb->shift; |
| 8035 | sectors = next - s; |
| 8036 | } |
| 8037 | |
| 8038 | write_seqlock_irq(&bb->lock); |
| 8039 | |
| 8040 | p = bb->page; |
| 8041 | lo = 0; |
| 8042 | hi = bb->count; |
| 8043 | /* Find the last range that starts at-or-before 's' */ |
| 8044 | while (hi - lo > 1) { |
| 8045 | int mid = (lo + hi) / 2; |
| 8046 | sector_t a = BB_OFFSET(p[mid]); |
| 8047 | if (a <= s) |
| 8048 | lo = mid; |
| 8049 | else |
| 8050 | hi = mid; |
| 8051 | } |
| 8052 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8053 | hi = lo; |
| 8054 | |
| 8055 | if (hi > lo) { |
| 8056 | /* we found a range that might merge with the start |
| 8057 | * of our new range |
| 8058 | */ |
| 8059 | sector_t a = BB_OFFSET(p[lo]); |
| 8060 | sector_t e = a + BB_LEN(p[lo]); |
| 8061 | int ack = BB_ACK(p[lo]); |
| 8062 | if (e >= s) { |
| 8063 | /* Yes, we can merge with a previous range */ |
| 8064 | if (s == a && s + sectors >= e) |
| 8065 | /* new range covers old */ |
| 8066 | ack = acknowledged; |
| 8067 | else |
| 8068 | ack = ack && acknowledged; |
| 8069 | |
| 8070 | if (e < s + sectors) |
| 8071 | e = s + sectors; |
| 8072 | if (e - a <= BB_MAX_LEN) { |
| 8073 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8074 | s = e; |
| 8075 | } else { |
| 8076 | /* does not all fit in one range, |
| 8077 | * make p[lo] maximal |
| 8078 | */ |
| 8079 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8080 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8081 | s = a + BB_MAX_LEN; |
| 8082 | } |
| 8083 | sectors = e - s; |
| 8084 | } |
| 8085 | } |
| 8086 | if (sectors && hi < bb->count) { |
| 8087 | /* 'hi' points to the first range that starts after 's'. |
| 8088 | * Maybe we can merge with the start of that range */ |
| 8089 | sector_t a = BB_OFFSET(p[hi]); |
| 8090 | sector_t e = a + BB_LEN(p[hi]); |
| 8091 | int ack = BB_ACK(p[hi]); |
| 8092 | if (a <= s + sectors) { |
| 8093 | /* merging is possible */ |
| 8094 | if (e <= s + sectors) { |
| 8095 | /* full overlap */ |
| 8096 | e = s + sectors; |
| 8097 | ack = acknowledged; |
| 8098 | } else |
| 8099 | ack = ack && acknowledged; |
| 8100 | |
| 8101 | a = s; |
| 8102 | if (e - a <= BB_MAX_LEN) { |
| 8103 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8104 | s = e; |
| 8105 | } else { |
| 8106 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8107 | s = a + BB_MAX_LEN; |
| 8108 | } |
| 8109 | sectors = e - s; |
| 8110 | lo = hi; |
| 8111 | hi++; |
| 8112 | } |
| 8113 | } |
| 8114 | if (sectors == 0 && hi < bb->count) { |
| 8115 | /* we might be able to combine lo and hi */ |
| 8116 | /* Note: 's' is at the end of 'lo' */ |
| 8117 | sector_t a = BB_OFFSET(p[hi]); |
| 8118 | int lolen = BB_LEN(p[lo]); |
| 8119 | int hilen = BB_LEN(p[hi]); |
| 8120 | int newlen = lolen + hilen - (s - a); |
| 8121 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8122 | /* yes, we can combine them */ |
| 8123 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8124 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8125 | memmove(p + hi, p + hi + 1, |
| 8126 | (bb->count - hi - 1) * 8); |
| 8127 | bb->count--; |
| 8128 | } |
| 8129 | } |
| 8130 | while (sectors) { |
| 8131 | /* didn't merge (it all). |
| 8132 | * Need to add a range just before 'hi' */ |
| 8133 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8134 | /* No room for more */ |
| 8135 | rv = 0; |
| 8136 | break; |
| 8137 | } else { |
| 8138 | int this_sectors = sectors; |
| 8139 | memmove(p + hi + 1, p + hi, |
| 8140 | (bb->count - hi) * 8); |
| 8141 | bb->count++; |
| 8142 | |
| 8143 | if (this_sectors > BB_MAX_LEN) |
| 8144 | this_sectors = BB_MAX_LEN; |
| 8145 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8146 | sectors -= this_sectors; |
| 8147 | s += this_sectors; |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8152 | if (!acknowledged) |
| 8153 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8154 | write_sequnlock_irq(&bb->lock); |
| 8155 | |
| 8156 | return rv; |
| 8157 | } |
| 8158 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8159 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8160 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8161 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8162 | int rv; |
| 8163 | if (is_new) |
| 8164 | s += rdev->new_data_offset; |
| 8165 | else |
| 8166 | s += rdev->data_offset; |
| 8167 | rv = md_set_badblocks(&rdev->badblocks, |
| 8168 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8169 | if (rv) { |
| 8170 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8171 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8172 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8173 | md_wakeup_thread(rdev->mddev->thread); |
| 8174 | } |
| 8175 | return rv; |
| 8176 | } |
| 8177 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8178 | |
| 8179 | /* |
| 8180 | * Remove a range of bad blocks from the table. |
| 8181 | * This may involve extending the table if we spilt a region, |
| 8182 | * but it must not fail. So if the table becomes full, we just |
| 8183 | * drop the remove request. |
| 8184 | */ |
| 8185 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8186 | { |
| 8187 | u64 *p; |
| 8188 | int lo, hi; |
| 8189 | sector_t target = s + sectors; |
| 8190 | int rv = 0; |
| 8191 | |
| 8192 | if (bb->shift > 0) { |
| 8193 | /* When clearing we round the start up and the end down. |
| 8194 | * This should not matter as the shift should align with |
| 8195 | * the block size and no rounding should ever be needed. |
| 8196 | * However it is better the think a block is bad when it |
| 8197 | * isn't than to think a block is not bad when it is. |
| 8198 | */ |
| 8199 | s += (1<<bb->shift) - 1; |
| 8200 | s >>= bb->shift; |
| 8201 | target >>= bb->shift; |
| 8202 | sectors = target - s; |
| 8203 | } |
| 8204 | |
| 8205 | write_seqlock_irq(&bb->lock); |
| 8206 | |
| 8207 | p = bb->page; |
| 8208 | lo = 0; |
| 8209 | hi = bb->count; |
| 8210 | /* Find the last range that starts before 'target' */ |
| 8211 | while (hi - lo > 1) { |
| 8212 | int mid = (lo + hi) / 2; |
| 8213 | sector_t a = BB_OFFSET(p[mid]); |
| 8214 | if (a < target) |
| 8215 | lo = mid; |
| 8216 | else |
| 8217 | hi = mid; |
| 8218 | } |
| 8219 | if (hi > lo) { |
| 8220 | /* p[lo] is the last range that could overlap the |
| 8221 | * current range. Earlier ranges could also overlap, |
| 8222 | * but only this one can overlap the end of the range. |
| 8223 | */ |
| 8224 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8225 | /* Partial overlap, leave the tail of this range */ |
| 8226 | int ack = BB_ACK(p[lo]); |
| 8227 | sector_t a = BB_OFFSET(p[lo]); |
| 8228 | sector_t end = a + BB_LEN(p[lo]); |
| 8229 | |
| 8230 | if (a < s) { |
| 8231 | /* we need to split this range */ |
| 8232 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8233 | rv = 0; |
| 8234 | goto out; |
| 8235 | } |
| 8236 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8237 | bb->count++; |
| 8238 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8239 | lo++; |
| 8240 | } |
| 8241 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8242 | /* there is no longer an overlap */ |
| 8243 | hi = lo; |
| 8244 | lo--; |
| 8245 | } |
| 8246 | while (lo >= 0 && |
| 8247 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8248 | /* This range does overlap */ |
| 8249 | if (BB_OFFSET(p[lo]) < s) { |
| 8250 | /* Keep the early parts of this range. */ |
| 8251 | int ack = BB_ACK(p[lo]); |
| 8252 | sector_t start = BB_OFFSET(p[lo]); |
| 8253 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8254 | /* now low doesn't overlap, so.. */ |
| 8255 | break; |
| 8256 | } |
| 8257 | lo--; |
| 8258 | } |
| 8259 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8260 | * anything between needs to be discarded |
| 8261 | */ |
| 8262 | if (hi - lo > 1) { |
| 8263 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8264 | bb->count -= (hi - lo - 1); |
| 8265 | } |
| 8266 | } |
| 8267 | |
| 8268 | bb->changed = 1; |
| 8269 | out: |
| 8270 | write_sequnlock_irq(&bb->lock); |
| 8271 | return rv; |
| 8272 | } |
| 8273 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8274 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8275 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8276 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8277 | if (is_new) |
| 8278 | s += rdev->new_data_offset; |
| 8279 | else |
| 8280 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8281 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8282 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8283 | } |
| 8284 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8285 | |
| 8286 | /* |
| 8287 | * Acknowledge all bad blocks in a list. |
| 8288 | * This only succeeds if ->changed is clear. It is used by |
| 8289 | * in-kernel metadata updates |
| 8290 | */ |
| 8291 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8292 | { |
| 8293 | if (bb->page == NULL || bb->changed) |
| 8294 | /* no point even trying */ |
| 8295 | return; |
| 8296 | write_seqlock_irq(&bb->lock); |
| 8297 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8298 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8299 | u64 *p = bb->page; |
| 8300 | int i; |
| 8301 | for (i = 0; i < bb->count ; i++) { |
| 8302 | if (!BB_ACK(p[i])) { |
| 8303 | sector_t start = BB_OFFSET(p[i]); |
| 8304 | int len = BB_LEN(p[i]); |
| 8305 | p[i] = BB_MAKE(start, len, 1); |
| 8306 | } |
| 8307 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8308 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8309 | } |
| 8310 | write_sequnlock_irq(&bb->lock); |
| 8311 | } |
| 8312 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8313 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8314 | /* sysfs access to bad-blocks list. |
| 8315 | * We present two files. |
| 8316 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8317 | * are recorded as bad. The list is truncated to fit within |
| 8318 | * the one-page limit of sysfs. |
| 8319 | * Writing "sector length" to this file adds an acknowledged |
| 8320 | * bad block list. |
| 8321 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8322 | * been acknowledged. Writing to this file adds bad blocks |
| 8323 | * without acknowledging them. This is largely for testing. |
| 8324 | */ |
| 8325 | |
| 8326 | static ssize_t |
| 8327 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8328 | { |
| 8329 | size_t len; |
| 8330 | int i; |
| 8331 | u64 *p = bb->page; |
| 8332 | unsigned seq; |
| 8333 | |
| 8334 | if (bb->shift < 0) |
| 8335 | return 0; |
| 8336 | |
| 8337 | retry: |
| 8338 | seq = read_seqbegin(&bb->lock); |
| 8339 | |
| 8340 | len = 0; |
| 8341 | i = 0; |
| 8342 | |
| 8343 | while (len < PAGE_SIZE && i < bb->count) { |
| 8344 | sector_t s = BB_OFFSET(p[i]); |
| 8345 | unsigned int length = BB_LEN(p[i]); |
| 8346 | int ack = BB_ACK(p[i]); |
| 8347 | i++; |
| 8348 | |
| 8349 | if (unack && ack) |
| 8350 | continue; |
| 8351 | |
| 8352 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8353 | (unsigned long long)s << bb->shift, |
| 8354 | length << bb->shift); |
| 8355 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8356 | if (unack && len == 0) |
| 8357 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8358 | |
| 8359 | if (read_seqretry(&bb->lock, seq)) |
| 8360 | goto retry; |
| 8361 | |
| 8362 | return len; |
| 8363 | } |
| 8364 | |
| 8365 | #define DO_DEBUG 1 |
| 8366 | |
| 8367 | static ssize_t |
| 8368 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8369 | { |
| 8370 | unsigned long long sector; |
| 8371 | int length; |
| 8372 | char newline; |
| 8373 | #ifdef DO_DEBUG |
| 8374 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8375 | * Normally only a successful write may clear a badblock |
| 8376 | */ |
| 8377 | int clear = 0; |
| 8378 | if (page[0] == '-') { |
| 8379 | clear = 1; |
| 8380 | page++; |
| 8381 | } |
| 8382 | #endif /* DO_DEBUG */ |
| 8383 | |
| 8384 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8385 | case 3: |
| 8386 | if (newline != '\n') |
| 8387 | return -EINVAL; |
| 8388 | case 2: |
| 8389 | if (length <= 0) |
| 8390 | return -EINVAL; |
| 8391 | break; |
| 8392 | default: |
| 8393 | return -EINVAL; |
| 8394 | } |
| 8395 | |
| 8396 | #ifdef DO_DEBUG |
| 8397 | if (clear) { |
| 8398 | md_clear_badblocks(bb, sector, length); |
| 8399 | return len; |
| 8400 | } |
| 8401 | #endif /* DO_DEBUG */ |
| 8402 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8403 | return len; |
| 8404 | else |
| 8405 | return -ENOSPC; |
| 8406 | } |
| 8407 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8408 | static int md_notify_reboot(struct notifier_block *this, |
| 8409 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8410 | { |
| 8411 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8412 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8413 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8414 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8415 | for_each_mddev(mddev, tmp) { |
| 8416 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8417 | if (mddev->pers) |
| 8418 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8419 | mddev->safemode = 2; |
| 8420 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8421 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8422 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8423 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8424 | /* |
| 8425 | * certain more exotic SCSI devices are known to be |
| 8426 | * volatile wrt too early system reboots. While the |
| 8427 | * right place to handle this issue is the given |
| 8428 | * driver, we do want to have a safe RAID driver ... |
| 8429 | */ |
| 8430 | if (need_delay) |
| 8431 | mdelay(1000*1); |
| 8432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8433 | return NOTIFY_DONE; |
| 8434 | } |
| 8435 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8436 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8437 | .notifier_call = md_notify_reboot, |
| 8438 | .next = NULL, |
| 8439 | .priority = INT_MAX, /* before any real devices */ |
| 8440 | }; |
| 8441 | |
| 8442 | static void md_geninit(void) |
| 8443 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8444 | 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] | 8445 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8446 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8447 | } |
| 8448 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8449 | static int __init md_init(void) |
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 | int ret = -ENOMEM; |
| 8452 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8453 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8454 | if (!md_wq) |
| 8455 | goto err_wq; |
| 8456 | |
| 8457 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8458 | if (!md_misc_wq) |
| 8459 | goto err_misc_wq; |
| 8460 | |
| 8461 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8462 | goto err_md; |
| 8463 | |
| 8464 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8465 | goto err_mdp; |
| 8466 | mdp_major = ret; |
| 8467 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8468 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8469 | md_probe, NULL, NULL); |
| 8470 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8471 | md_probe, NULL, NULL); |
| 8472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8473 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8474 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8475 | |
| 8476 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8477 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8478 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8479 | err_mdp: |
| 8480 | unregister_blkdev(MD_MAJOR, "md"); |
| 8481 | err_md: |
| 8482 | destroy_workqueue(md_misc_wq); |
| 8483 | err_misc_wq: |
| 8484 | destroy_workqueue(md_wq); |
| 8485 | err_wq: |
| 8486 | return ret; |
| 8487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8488 | |
| 8489 | #ifndef MODULE |
| 8490 | |
| 8491 | /* |
| 8492 | * Searches all registered partitions for autorun RAID arrays |
| 8493 | * at boot time. |
| 8494 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8495 | |
| 8496 | static LIST_HEAD(all_detected_devices); |
| 8497 | struct detected_devices_node { |
| 8498 | struct list_head list; |
| 8499 | dev_t dev; |
| 8500 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8501 | |
| 8502 | void md_autodetect_dev(dev_t dev) |
| 8503 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8504 | struct detected_devices_node *node_detected_dev; |
| 8505 | |
| 8506 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8507 | if (node_detected_dev) { |
| 8508 | node_detected_dev->dev = dev; |
| 8509 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8510 | } else { |
| 8511 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8512 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8514 | } |
| 8515 | |
| 8516 | |
| 8517 | static void autostart_arrays(int part) |
| 8518 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8519 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8520 | struct detected_devices_node *node_detected_dev; |
| 8521 | dev_t dev; |
| 8522 | int i_scanned, i_passed; |
| 8523 | |
| 8524 | i_scanned = 0; |
| 8525 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8526 | |
| 8527 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8528 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8529 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8530 | i_scanned++; |
| 8531 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8532 | struct detected_devices_node, list); |
| 8533 | list_del(&node_detected_dev->list); |
| 8534 | dev = node_detected_dev->dev; |
| 8535 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8536 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8537 | if (IS_ERR(rdev)) |
| 8538 | continue; |
| 8539 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8540 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8541 | MD_BUG(); |
| 8542 | continue; |
| 8543 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8544 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8545 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8546 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8547 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8548 | |
| 8549 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8550 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8551 | |
| 8552 | autorun_devices(part); |
| 8553 | } |
| 8554 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8555 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8556 | |
| 8557 | static __exit void md_exit(void) |
| 8558 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8559 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8560 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8561 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8562 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8563 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8564 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8565 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8566 | unregister_blkdev(mdp_major, "mdp"); |
| 8567 | unregister_reboot_notifier(&md_notifier); |
| 8568 | unregister_sysctl_table(raid_table_header); |
| 8569 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8570 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8571 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8572 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8573 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8574 | destroy_workqueue(md_misc_wq); |
| 8575 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8576 | } |
| 8577 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8578 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8579 | module_exit(md_exit) |
| 8580 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8581 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8582 | { |
| 8583 | return sprintf(buffer, "%d", start_readonly); |
| 8584 | } |
| 8585 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8586 | { |
| 8587 | char *e; |
| 8588 | int num = simple_strtoul(val, &e, 10); |
| 8589 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8590 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8591 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8592 | } |
| 8593 | return -EINVAL; |
| 8594 | } |
| 8595 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8596 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8597 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8598 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8599 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8601 | EXPORT_SYMBOL(register_md_personality); |
| 8602 | EXPORT_SYMBOL(unregister_md_personality); |
| 8603 | EXPORT_SYMBOL(md_error); |
| 8604 | EXPORT_SYMBOL(md_done_sync); |
| 8605 | EXPORT_SYMBOL(md_write_start); |
| 8606 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8607 | EXPORT_SYMBOL(md_register_thread); |
| 8608 | EXPORT_SYMBOL(md_unregister_thread); |
| 8609 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8610 | EXPORT_SYMBOL(md_check_recovery); |
| 8611 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8612 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8613 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8614 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |