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 | ff01bb48 | 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); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 394 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 395 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 396 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 397 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 398 | { |
| 399 | mddev->suspended = 0; |
| 400 | wake_up(&mddev->sb_wait); |
| 401 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 402 | |
| 403 | md_wakeup_thread(mddev->thread); |
| 404 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 405 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 406 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 407 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 408 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 409 | { |
| 410 | return mddev->suspended; |
| 411 | } |
| 412 | EXPORT_SYMBOL(mddev_congested); |
| 413 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 415 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | */ |
| 417 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 420 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 421 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | |
| 423 | rdev_dec_pending(rdev, mddev); |
| 424 | |
| 425 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 426 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 427 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | } |
| 429 | bio_put(bio); |
| 430 | } |
| 431 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 432 | static void md_submit_flush_data(struct work_struct *ws); |
| 433 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 434 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 436 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 437 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 439 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 440 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | rcu_read_lock(); |
| 442 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 443 | if (rdev->raid_disk >= 0 && |
| 444 | !test_bit(Faulty, &rdev->flags)) { |
| 445 | /* Take two references, one is dropped |
| 446 | * when request finishes, one after |
| 447 | * we reclaim rcu_read_lock |
| 448 | */ |
| 449 | struct bio *bi; |
| 450 | atomic_inc(&rdev->nr_pending); |
| 451 | atomic_inc(&rdev->nr_pending); |
| 452 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 453 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 454 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 455 | bi->bi_private = rdev; |
| 456 | bi->bi_bdev = rdev->bdev; |
| 457 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 458 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 459 | rcu_read_lock(); |
| 460 | rdev_dec_pending(rdev, mddev); |
| 461 | } |
| 462 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 463 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 464 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 465 | } |
| 466 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 467 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 468 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 469 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 470 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 471 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 472 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 473 | /* an empty barrier - all done */ |
| 474 | bio_endio(bio, 0); |
| 475 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 476 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 477 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 478 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 479 | |
| 480 | mddev->flush_bio = NULL; |
| 481 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 482 | } |
| 483 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 484 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 485 | { |
| 486 | spin_lock_irq(&mddev->write_lock); |
| 487 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 488 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 489 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 490 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 491 | spin_unlock_irq(&mddev->write_lock); |
| 492 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 493 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 494 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 495 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 496 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 497 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 498 | /* Support for plugging. |
| 499 | * This mirrors the plugging support in request_queue, but does not |
| 500 | * require having a whole queue or request structures. |
| 501 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 502 | * plugged on. This links tot the private plug_handle structure in the |
| 503 | * personality data where we keep a count of the number of outstanding |
| 504 | * plugs so other code can see if a plug is active. |
| 505 | */ |
| 506 | struct md_plug_cb { |
| 507 | struct blk_plug_cb cb; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 508 | struct mddev *mddev; |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 512 | { |
| 513 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 514 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 515 | md_wakeup_thread(mdcb->mddev->thread); |
| 516 | kfree(mdcb); |
| 517 | } |
| 518 | |
| 519 | /* Check that an unplug wakeup will come shortly. |
| 520 | * If not, wakeup the md thread immediately |
| 521 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 522 | int mddev_check_plugged(struct mddev *mddev) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 523 | { |
| 524 | struct blk_plug *plug = current->plug; |
| 525 | struct md_plug_cb *mdcb; |
| 526 | |
| 527 | if (!plug) |
| 528 | return 0; |
| 529 | |
| 530 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 531 | if (mdcb->cb.callback == plugger_unplug && |
| 532 | mdcb->mddev == mddev) { |
| 533 | /* Already on the list, move to top */ |
| 534 | if (mdcb != list_first_entry(&plug->cb_list, |
| 535 | struct md_plug_cb, |
| 536 | cb.list)) |
| 537 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 538 | return 1; |
| 539 | } |
| 540 | } |
| 541 | /* Not currently on the callback list */ |
| 542 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 543 | if (!mdcb) |
| 544 | return 0; |
| 545 | |
| 546 | mdcb->mddev = mddev; |
| 547 | mdcb->cb.callback = plugger_unplug; |
| 548 | atomic_inc(&mddev->plug_cnt); |
| 549 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 550 | return 1; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 553 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 554 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
| 556 | atomic_inc(&mddev->active); |
| 557 | return mddev; |
| 558 | } |
| 559 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 560 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 561 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 562 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 564 | struct bio_set *bs = NULL; |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 567 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 568 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 569 | mddev->ctime == 0 && !mddev->hold_active) { |
| 570 | /* Array is not configured at all, and not held active, |
| 571 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 572 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 573 | bs = mddev->bio_set; |
| 574 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 575 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 576 | /* We did a probe so need to clean up. Call |
| 577 | * queue_work inside the spinlock so that |
| 578 | * flush_workqueue() after mddev_find will |
| 579 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 580 | */ |
| 581 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 582 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 583 | } else |
| 584 | kfree(mddev); |
| 585 | } |
| 586 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 587 | if (bs) |
| 588 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 591 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 592 | { |
| 593 | mutex_init(&mddev->open_mutex); |
| 594 | mutex_init(&mddev->reconfig_mutex); |
| 595 | mutex_init(&mddev->bitmap_info.mutex); |
| 596 | INIT_LIST_HEAD(&mddev->disks); |
| 597 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 598 | init_timer(&mddev->safemode_timer); |
| 599 | atomic_set(&mddev->active, 1); |
| 600 | atomic_set(&mddev->openers, 0); |
| 601 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 602 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 603 | spin_lock_init(&mddev->write_lock); |
| 604 | atomic_set(&mddev->flush_pending, 0); |
| 605 | init_waitqueue_head(&mddev->sb_wait); |
| 606 | init_waitqueue_head(&mddev->recovery_wait); |
| 607 | mddev->reshape_position = MaxSector; |
| 608 | mddev->resync_min = 0; |
| 609 | mddev->resync_max = MaxSector; |
| 610 | mddev->level = LEVEL_NONE; |
| 611 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 612 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 613 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 614 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 616 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 618 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 619 | unit &= ~((1<<MdpMinorShift)-1); |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | retry: |
| 622 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 624 | if (unit) { |
| 625 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 626 | if (mddev->unit == unit) { |
| 627 | mddev_get(mddev); |
| 628 | spin_unlock(&all_mddevs_lock); |
| 629 | kfree(new); |
| 630 | return mddev; |
| 631 | } |
| 632 | |
| 633 | if (new) { |
| 634 | list_add(&new->all_mddevs, &all_mddevs); |
| 635 | spin_unlock(&all_mddevs_lock); |
| 636 | new->hold_active = UNTIL_IOCTL; |
| 637 | return new; |
| 638 | } |
| 639 | } else if (new) { |
| 640 | /* find an unused unit number */ |
| 641 | static int next_minor = 512; |
| 642 | int start = next_minor; |
| 643 | int is_free = 0; |
| 644 | int dev = 0; |
| 645 | while (!is_free) { |
| 646 | dev = MKDEV(MD_MAJOR, next_minor); |
| 647 | next_minor++; |
| 648 | if (next_minor > MINORMASK) |
| 649 | next_minor = 0; |
| 650 | if (next_minor == start) { |
| 651 | /* Oh dear, all in use. */ |
| 652 | spin_unlock(&all_mddevs_lock); |
| 653 | kfree(new); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | is_free = 1; |
| 658 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 659 | if (mddev->unit == dev) { |
| 660 | is_free = 0; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | new->unit = dev; |
| 665 | new->md_minor = MINOR(dev); |
| 666 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | list_add(&new->all_mddevs, &all_mddevs); |
| 668 | spin_unlock(&all_mddevs_lock); |
| 669 | return new; |
| 670 | } |
| 671 | spin_unlock(&all_mddevs_lock); |
| 672 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 673 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (!new) |
| 675 | return NULL; |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | new->unit = unit; |
| 678 | if (MAJOR(unit) == MD_MAJOR) |
| 679 | new->md_minor = MINOR(unit); |
| 680 | else |
| 681 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 682 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 683 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | goto retry; |
| 686 | } |
| 687 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 688 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 690 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 693 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 694 | { |
| 695 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 696 | } |
| 697 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 698 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 700 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 703 | static struct attribute_group md_redundancy_group; |
| 704 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 705 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 707 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 708 | /* These cannot be removed under reconfig_mutex as |
| 709 | * an access to the files will try to take reconfig_mutex |
| 710 | * while holding the file unremovable, which leads to |
| 711 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 712 | * So hold set sysfs_active while the remove in happeing, |
| 713 | * and anything else which might set ->to_remove or my |
| 714 | * otherwise change the sysfs namespace will fail with |
| 715 | * -EBUSY if sysfs_active is still set. |
| 716 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 717 | * test it under the same mutex to ensure its correct value |
| 718 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 719 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 720 | struct attribute_group *to_remove = mddev->to_remove; |
| 721 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 722 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 723 | mutex_unlock(&mddev->reconfig_mutex); |
| 724 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 725 | if (mddev->kobj.sd) { |
| 726 | if (to_remove != &md_redundancy_group) |
| 727 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 728 | if (mddev->pers == NULL || |
| 729 | mddev->pers->sync_request == NULL) { |
| 730 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 731 | if (mddev->sysfs_action) |
| 732 | sysfs_put(mddev->sysfs_action); |
| 733 | mddev->sysfs_action = NULL; |
| 734 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 735 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 736 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 737 | } else |
| 738 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 740 | /* As we've dropped the mutex we need a spinlock to |
| 741 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 742 | */ |
| 743 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 744 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 745 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 748 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 750 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 752 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (rdev->desc_nr == nr) |
| 754 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return NULL; |
| 757 | } |
| 758 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 759 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 761 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 763 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (rdev->bdev->bd_dev == dev) |
| 765 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return NULL; |
| 768 | } |
| 769 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 770 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 771 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 772 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 773 | list_for_each_entry(pers, &pers_list, list) { |
| 774 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 775 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 776 | if (strcmp(pers->name, clevel)==0) |
| 777 | return pers; |
| 778 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 779 | return NULL; |
| 780 | } |
| 781 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 782 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 783 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 785 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 786 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 789 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | if (rdev->sb_page) |
| 792 | MD_BUG(); |
| 793 | |
| 794 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 795 | if (!rdev->sb_page) { |
| 796 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 797 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 803 | static void free_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | { |
| 805 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 806 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | rdev->sb_loaded = 0; |
| 808 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 809 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 810 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 812 | if (rdev->bb_page) { |
| 813 | put_page(rdev->bb_page); |
| 814 | rdev->bb_page = NULL; |
| 815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 819 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 820 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 821 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 822 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 823 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 824 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 825 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 826 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 827 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 828 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 829 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 830 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 831 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 832 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 833 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 834 | } |
| 835 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 836 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 837 | sector_t sector, int size, struct page *page) |
| 838 | { |
| 839 | /* write first size bytes of page to sector of rdev |
| 840 | * Increment mddev->pending_writes before returning |
| 841 | * and decrement it on completion, waking up sb_wait |
| 842 | * if zero is reached. |
| 843 | * If an error occurred, call md_error |
| 844 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 845 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 846 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 847 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 848 | bio->bi_sector = sector; |
| 849 | bio_add_page(bio, page, size, 0); |
| 850 | bio->bi_private = rdev; |
| 851 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 852 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 853 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 854 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 855 | } |
| 856 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 857 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 858 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 859 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 860 | DEFINE_WAIT(wq); |
| 861 | for(;;) { |
| 862 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 863 | if (atomic_read(&mddev->pending_writes)==0) |
| 864 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 865 | schedule(); |
| 866 | } |
| 867 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 868 | } |
| 869 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 870 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 875 | 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] | 876 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 878 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | struct completion event; |
| 880 | int ret; |
| 881 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 882 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 884 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 885 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 886 | if (metadata_op) |
| 887 | bio->bi_sector = sector + rdev->sb_start; |
| 888 | else |
| 889 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | bio_add_page(bio, page, size, 0); |
| 891 | init_completion(&event); |
| 892 | bio->bi_private = &event; |
| 893 | bio->bi_end_io = bi_complete; |
| 894 | submit_bio(rw, bio); |
| 895 | wait_for_completion(&event); |
| 896 | |
| 897 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 898 | bio_put(bio); |
| 899 | return ret; |
| 900 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 901 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 903 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | { |
| 905 | char b[BDEVNAME_SIZE]; |
| 906 | if (!rdev->sb_page) { |
| 907 | MD_BUG(); |
| 908 | return -EINVAL; |
| 909 | } |
| 910 | if (rdev->sb_loaded) |
| 911 | return 0; |
| 912 | |
| 913 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 914 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | goto fail; |
| 916 | rdev->sb_loaded = 1; |
| 917 | return 0; |
| 918 | |
| 919 | fail: |
| 920 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 921 | bdevname(rdev->bdev,b)); |
| 922 | return -EINVAL; |
| 923 | } |
| 924 | |
| 925 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 926 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 927 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 928 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 929 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 930 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 934 | { |
| 935 | int ret; |
| 936 | mdp_super_t *tmp1, *tmp2; |
| 937 | |
| 938 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 939 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 940 | |
| 941 | if (!tmp1 || !tmp2) { |
| 942 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 943 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | goto abort; |
| 945 | } |
| 946 | |
| 947 | *tmp1 = *sb1; |
| 948 | *tmp2 = *sb2; |
| 949 | |
| 950 | /* |
| 951 | * nr_disks is not constant |
| 952 | */ |
| 953 | tmp1->nr_disks = 0; |
| 954 | tmp2->nr_disks = 0; |
| 955 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 956 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 958 | kfree(tmp1); |
| 959 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return ret; |
| 961 | } |
| 962 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 963 | |
| 964 | static u32 md_csum_fold(u32 csum) |
| 965 | { |
| 966 | csum = (csum & 0xffff) + (csum >> 16); |
| 967 | return (csum & 0xffff) + (csum >> 16); |
| 968 | } |
| 969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 971 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 972 | u64 newcsum = 0; |
| 973 | u32 *sb32 = (u32*)sb; |
| 974 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | unsigned int disk_csum, csum; |
| 976 | |
| 977 | disk_csum = sb->sb_csum; |
| 978 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 979 | |
| 980 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 981 | newcsum += sb32[i]; |
| 982 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 983 | |
| 984 | |
| 985 | #ifdef CONFIG_ALPHA |
| 986 | /* This used to use csum_partial, which was wrong for several |
| 987 | * reasons including that different results are returned on |
| 988 | * different architectures. It isn't critical that we get exactly |
| 989 | * the same return value as before (we always csum_fold before |
| 990 | * testing, and that removes any differences). However as we |
| 991 | * know that csum_partial always returned a 16bit value on |
| 992 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 993 | */ |
| 994 | sb->sb_csum = md_csum_fold(disk_csum); |
| 995 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 997 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return csum; |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | /* |
| 1003 | * Handle superblock details. |
| 1004 | * We want to be able to handle multiple superblock formats |
| 1005 | * so we have a common interface to them all, and an array of |
| 1006 | * different handlers. |
| 1007 | * We rely on user-space to write the initial superblock, and support |
| 1008 | * reading and updating of superblocks. |
| 1009 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1010 | * 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] | 1011 | * loads and validates a superblock on dev. |
| 1012 | * if refdev != NULL, compare superblocks on both devices |
| 1013 | * Return: |
| 1014 | * 0 - dev has a superblock that is compatible with refdev |
| 1015 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1016 | * so dev should be used as the refdev in future |
| 1017 | * -EINVAL superblock incompatible or invalid |
| 1018 | * -othererror e.g. -EIO |
| 1019 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1020 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | * Verify that dev is acceptable into mddev. |
| 1022 | * The first time, mddev->raid_disks will be 0, and data from |
| 1023 | * dev should be merged in. Subsequent calls check that dev |
| 1024 | * is new enough. Return 0 or -EINVAL |
| 1025 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1026 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | * Update the superblock for rdev with data in mddev |
| 1028 | * This does not write to disc. |
| 1029 | * |
| 1030 | */ |
| 1031 | |
| 1032 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1033 | char *name; |
| 1034 | struct module *owner; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1035 | int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1036 | int minor_version); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1037 | int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev); |
| 1038 | void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1039 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1040 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | }; |
| 1042 | |
| 1043 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1044 | * Check that the given mddev has no bitmap. |
| 1045 | * |
| 1046 | * This function is called from the run method of all personalities that do not |
| 1047 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1048 | * has a bitmap. Otherwise, it returns 0. |
| 1049 | * |
| 1050 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1051 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1052 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1053 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1054 | return 0; |
| 1055 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1056 | mdname(mddev), mddev->pers->name); |
| 1057 | return 1; |
| 1058 | } |
| 1059 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1060 | |
| 1061 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | * load_super for 0.90.0 |
| 1063 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1064 | 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] | 1065 | { |
| 1066 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1067 | mdp_super_t *sb; |
| 1068 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1071 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | * it's at the end of the disk. |
| 1073 | * |
| 1074 | * It also happens to be a multiple of 4Kb. |
| 1075 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1076 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1078 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | if (ret) return ret; |
| 1080 | |
| 1081 | ret = -EINVAL; |
| 1082 | |
| 1083 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1084 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
| 1086 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1087 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1088 | b); |
| 1089 | goto abort; |
| 1090 | } |
| 1091 | |
| 1092 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1093 | sb->minor_version < 90 || |
| 1094 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1096 | sb->major_version, sb->minor_version, |
| 1097 | b); |
| 1098 | goto abort; |
| 1099 | } |
| 1100 | |
| 1101 | if (sb->raid_disks <= 0) |
| 1102 | goto abort; |
| 1103 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1104 | 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] | 1105 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1106 | b); |
| 1107 | goto abort; |
| 1108 | } |
| 1109 | |
| 1110 | rdev->preferred_minor = sb->md_minor; |
| 1111 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1112 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1113 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | if (sb->level == LEVEL_MULTIPATH) |
| 1116 | rdev->desc_nr = -1; |
| 1117 | else |
| 1118 | rdev->desc_nr = sb->this_disk.number; |
| 1119 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1120 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1122 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1124 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | if (!uuid_equal(refsb, sb)) { |
| 1126 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1127 | b, bdevname(refdev->bdev,b2)); |
| 1128 | goto abort; |
| 1129 | } |
| 1130 | if (!sb_equal(refsb, sb)) { |
| 1131 | printk(KERN_WARNING "md: %s has same UUID" |
| 1132 | " but different superblock to %s\n", |
| 1133 | b, bdevname(refdev->bdev, b2)); |
| 1134 | goto abort; |
| 1135 | } |
| 1136 | ev1 = md_event(sb); |
| 1137 | ev2 = md_event(refsb); |
| 1138 | if (ev1 > ev2) |
| 1139 | ret = 1; |
| 1140 | else |
| 1141 | ret = 0; |
| 1142 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1143 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1144 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1145 | if (rdev->sectors >= (2ULL << 32)) |
| 1146 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1148 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1149 | /* "this cannot possibly happen" ... */ |
| 1150 | ret = -EINVAL; |
| 1151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | abort: |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * validate_super for 0.90.0 |
| 1158 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1159 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
| 1161 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1162 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1163 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1165 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1166 | clear_bit(Faulty, &rdev->flags); |
| 1167 | clear_bit(In_sync, &rdev->flags); |
| 1168 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (mddev->raid_disks == 0) { |
| 1171 | mddev->major_version = 0; |
| 1172 | mddev->minor_version = sb->minor_version; |
| 1173 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1174 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1175 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | mddev->ctime = sb->ctime; |
| 1177 | mddev->utime = sb->utime; |
| 1178 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1179 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | mddev->layout = sb->layout; |
| 1181 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1182 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1183 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1184 | mddev->bitmap_info.offset = 0; |
| 1185 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1187 | if (mddev->minor_version >= 91) { |
| 1188 | mddev->reshape_position = sb->reshape_position; |
| 1189 | mddev->delta_disks = sb->delta_disks; |
| 1190 | mddev->new_level = sb->new_level; |
| 1191 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1192 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1193 | } else { |
| 1194 | mddev->reshape_position = MaxSector; |
| 1195 | mddev->delta_disks = 0; |
| 1196 | mddev->new_level = mddev->level; |
| 1197 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1198 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1202 | mddev->recovery_cp = MaxSector; |
| 1203 | else { |
| 1204 | if (sb->events_hi == sb->cp_events_hi && |
| 1205 | sb->events_lo == sb->cp_events_lo) { |
| 1206 | mddev->recovery_cp = sb->recovery_cp; |
| 1207 | } else |
| 1208 | mddev->recovery_cp = 0; |
| 1209 | } |
| 1210 | |
| 1211 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1212 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1213 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1214 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1215 | |
| 1216 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1217 | |
| 1218 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1219 | mddev->bitmap_info.file == NULL) |
| 1220 | mddev->bitmap_info.offset = |
| 1221 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1222 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1223 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1224 | /* Insist on good event counter while assembling, except |
| 1225 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1227 | if (sb->disks[rdev->desc_nr].state & ( |
| 1228 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1229 | if (ev1 < mddev->events) |
| 1230 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1231 | } else if (mddev->bitmap) { |
| 1232 | /* if adding to array with a bitmap, then we can accept an |
| 1233 | * older device ... but not too old. |
| 1234 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1235 | if (ev1 < mddev->bitmap->events_cleared) |
| 1236 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1237 | } else { |
| 1238 | if (ev1 < mddev->events) |
| 1239 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1240 | return 0; |
| 1241 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | desc = sb->disks + rdev->desc_nr; |
| 1245 | |
| 1246 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1247 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1248 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1249 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1250 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1252 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1253 | /* active but not in sync implies recovery up to |
| 1254 | * reshape position. We don't know exactly where |
| 1255 | * that is, so set to zero for now */ |
| 1256 | if (mddev->minor_version >= 91) { |
| 1257 | rdev->recovery_offset = 0; |
| 1258 | rdev->raid_disk = desc->raid_disk; |
| 1259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1261 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1262 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1263 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1264 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * sync_super for 0.90.0 |
| 1270 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1271 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | { |
| 1273 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1274 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* make rdev->sb match mddev data.. |
| 1279 | * |
| 1280 | * 1/ zero out disks |
| 1281 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1282 | * 3/ any empty disks < next_spare become removed |
| 1283 | * |
| 1284 | * disks[0] gets initialised to REMOVED because |
| 1285 | * we cannot be sure from other fields if it has |
| 1286 | * been initialised or not. |
| 1287 | */ |
| 1288 | int i; |
| 1289 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1290 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1291 | rdev->sb_size = MD_SB_BYTES; |
| 1292 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1293 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | memset(sb, 0, sizeof(*sb)); |
| 1296 | |
| 1297 | sb->md_magic = MD_SB_MAGIC; |
| 1298 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | sb->patch_version = mddev->patch_version; |
| 1300 | sb->gvalid_words = 0; /* ignored */ |
| 1301 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1302 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1303 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1304 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1305 | |
| 1306 | sb->ctime = mddev->ctime; |
| 1307 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1308 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | sb->raid_disks = mddev->raid_disks; |
| 1310 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1311 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | sb->utime = mddev->utime; |
| 1313 | sb->state = 0; |
| 1314 | sb->events_hi = (mddev->events>>32); |
| 1315 | sb->events_lo = (u32)mddev->events; |
| 1316 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1317 | if (mddev->reshape_position == MaxSector) |
| 1318 | sb->minor_version = 90; |
| 1319 | else { |
| 1320 | sb->minor_version = 91; |
| 1321 | sb->reshape_position = mddev->reshape_position; |
| 1322 | sb->new_level = mddev->new_level; |
| 1323 | sb->delta_disks = mddev->delta_disks; |
| 1324 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1325 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1326 | } |
| 1327 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | if (mddev->in_sync) |
| 1329 | { |
| 1330 | sb->recovery_cp = mddev->recovery_cp; |
| 1331 | sb->cp_events_hi = (mddev->events>>32); |
| 1332 | sb->cp_events_lo = (u32)mddev->events; |
| 1333 | if (mddev->recovery_cp == MaxSector) |
| 1334 | sb->state = (1<< MD_SB_CLEAN); |
| 1335 | } else |
| 1336 | sb->recovery_cp = 0; |
| 1337 | |
| 1338 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1339 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1341 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1342 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1345 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1347 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1348 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1349 | |
| 1350 | if (rdev2->raid_disk >= 0 && |
| 1351 | sb->minor_version >= 91) |
| 1352 | /* we have nowhere to store the recovery_offset, |
| 1353 | * but if it is not below the reshape_position, |
| 1354 | * we can piggy-back on that. |
| 1355 | */ |
| 1356 | is_active = 1; |
| 1357 | if (rdev2->raid_disk < 0 || |
| 1358 | test_bit(Faulty, &rdev2->flags)) |
| 1359 | is_active = 0; |
| 1360 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1361 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1363 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1364 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | d = &sb->disks[rdev2->desc_nr]; |
| 1366 | nr_disks++; |
| 1367 | d->number = rdev2->desc_nr; |
| 1368 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1369 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1370 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | d->raid_disk = rdev2->raid_disk; |
| 1372 | else |
| 1373 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1374 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1376 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1378 | if (test_bit(In_sync, &rdev2->flags)) |
| 1379 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | active++; |
| 1381 | working++; |
| 1382 | } else { |
| 1383 | d->state = 0; |
| 1384 | spare++; |
| 1385 | working++; |
| 1386 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1387 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1388 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1391 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1392 | mdp_disk_t *d = &sb->disks[i]; |
| 1393 | if (d->state == 0 && d->number == 0) { |
| 1394 | d->number = i; |
| 1395 | d->raid_disk = i; |
| 1396 | d->state = (1<<MD_DISK_REMOVED); |
| 1397 | d->state |= (1<<MD_DISK_FAULTY); |
| 1398 | failed++; |
| 1399 | } |
| 1400 | } |
| 1401 | sb->nr_disks = nr_disks; |
| 1402 | sb->active_disks = active; |
| 1403 | sb->working_disks = working; |
| 1404 | sb->failed_disks = failed; |
| 1405 | sb->spare_disks = spare; |
| 1406 | |
| 1407 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1408 | sb->sb_csum = calc_sb_csum(sb); |
| 1409 | } |
| 1410 | |
| 1411 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1412 | * rdev_size_change for 0.90.0 |
| 1413 | */ |
| 1414 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1415 | 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] | 1416 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1417 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1418 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1419 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1420 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1421 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1422 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1423 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1424 | /* Limit to 4TB as metadata cannot record more than that. |
| 1425 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1426 | */ |
| 1427 | if (num_sectors >= (2ULL << 32)) |
| 1428 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1429 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1430 | rdev->sb_page); |
| 1431 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1432 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | * version 1 superblock |
| 1438 | */ |
| 1439 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1440 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1442 | __le32 disk_csum; |
| 1443 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | unsigned long long newcsum; |
| 1445 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1446 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | int i; |
| 1448 | |
| 1449 | disk_csum = sb->sb_csum; |
| 1450 | sb->sb_csum = 0; |
| 1451 | newcsum = 0; |
| 1452 | for (i=0; size>=4; size -= 4 ) |
| 1453 | newcsum += le32_to_cpu(*isuper++); |
| 1454 | |
| 1455 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1456 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
| 1458 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1459 | sb->sb_csum = disk_csum; |
| 1460 | return cpu_to_le32(csum); |
| 1461 | } |
| 1462 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1463 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1464 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1465 | 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] | 1466 | { |
| 1467 | struct mdp_superblock_1 *sb; |
| 1468 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1469 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1471 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
| 1473 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1474 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | * It is always aligned to a 4K boundary and |
| 1476 | * depeding on minor_version, it can be: |
| 1477 | * 0: At least 8K, but less than 12K, from end of device |
| 1478 | * 1: At start of device |
| 1479 | * 2: 4K from start of device. |
| 1480 | */ |
| 1481 | switch(minor_version) { |
| 1482 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1483 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1484 | sb_start -= 8*2; |
| 1485 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | break; |
| 1487 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1488 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | break; |
| 1490 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1491 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | break; |
| 1493 | default: |
| 1494 | return -EINVAL; |
| 1495 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1496 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1498 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1499 | * and it is safe to read 4k, so we do that |
| 1500 | */ |
| 1501 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | if (ret) return ret; |
| 1503 | |
| 1504 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1505 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1508 | sb->major_version != cpu_to_le32(1) || |
| 1509 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1510 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1511 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | return -EINVAL; |
| 1513 | |
| 1514 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1515 | printk("md: invalid superblock checksum on %s\n", |
| 1516 | bdevname(rdev->bdev,b)); |
| 1517 | return -EINVAL; |
| 1518 | } |
| 1519 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1520 | printk("md: data_size too small on %s\n", |
| 1521 | bdevname(rdev->bdev,b)); |
| 1522 | return -EINVAL; |
| 1523 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | rdev->preferred_minor = 0xffff; |
| 1526 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1527 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1529 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1530 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1531 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1532 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1533 | |
| 1534 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1535 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1536 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1537 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1538 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1539 | rdev->desc_nr = -1; |
| 1540 | else |
| 1541 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1542 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1543 | if (!rdev->bb_page) { |
| 1544 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1545 | if (!rdev->bb_page) |
| 1546 | return -ENOMEM; |
| 1547 | } |
| 1548 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1549 | rdev->badblocks.count == 0) { |
| 1550 | /* need to load the bad block list. |
| 1551 | * Currently we limit it to one page. |
| 1552 | */ |
| 1553 | s32 offset; |
| 1554 | sector_t bb_sector; |
| 1555 | u64 *bbp; |
| 1556 | int i; |
| 1557 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1558 | if (sectors > (PAGE_SIZE / 512)) |
| 1559 | return -EINVAL; |
| 1560 | offset = le32_to_cpu(sb->bblog_offset); |
| 1561 | if (offset == 0) |
| 1562 | return -EINVAL; |
| 1563 | bb_sector = (long long)offset; |
| 1564 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1565 | rdev->bb_page, READ, true)) |
| 1566 | return -EIO; |
| 1567 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1568 | rdev->badblocks.shift = sb->bblog_shift; |
| 1569 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1570 | u64 bb = le64_to_cpu(*bbp); |
| 1571 | int count = bb & (0x3ff); |
| 1572 | u64 sector = bb >> 10; |
| 1573 | sector <<= sb->bblog_shift; |
| 1574 | count <<= sb->bblog_shift; |
| 1575 | if (bb + 1 == 0) |
| 1576 | break; |
| 1577 | if (md_set_badblocks(&rdev->badblocks, |
| 1578 | sector, count, 1) == 0) |
| 1579 | return -EINVAL; |
| 1580 | } |
| 1581 | } else if (sb->bblog_offset == 0) |
| 1582 | rdev->badblocks.shift = -1; |
| 1583 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1584 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1585 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1586 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1588 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | |
| 1590 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1591 | sb->level != refsb->level || |
| 1592 | sb->layout != refsb->layout || |
| 1593 | sb->chunksize != refsb->chunksize) { |
| 1594 | printk(KERN_WARNING "md: %s has strangely different" |
| 1595 | " superblock to %s\n", |
| 1596 | bdevname(rdev->bdev,b), |
| 1597 | bdevname(refdev->bdev,b2)); |
| 1598 | return -EINVAL; |
| 1599 | } |
| 1600 | ev1 = le64_to_cpu(sb->events); |
| 1601 | ev2 = le64_to_cpu(refsb->events); |
| 1602 | |
| 1603 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1604 | ret = 1; |
| 1605 | else |
| 1606 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1608 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1609 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1610 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1612 | rdev->sectors = rdev->sb_start; |
| 1613 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1615 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1616 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1617 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1618 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1621 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1623 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1624 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1626 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1627 | clear_bit(Faulty, &rdev->flags); |
| 1628 | clear_bit(In_sync, &rdev->flags); |
| 1629 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (mddev->raid_disks == 0) { |
| 1632 | mddev->major_version = 1; |
| 1633 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1634 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1635 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1637 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1638 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1639 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | mddev->layout = le32_to_cpu(sb->layout); |
| 1641 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1642 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1643 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1644 | mddev->bitmap_info.offset = 0; |
| 1645 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
| 1647 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1648 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1649 | |
| 1650 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1651 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1652 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1653 | mddev->bitmap_info.file == NULL ) |
| 1654 | mddev->bitmap_info.offset = |
| 1655 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1656 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1657 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1658 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1659 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1660 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1661 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1662 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1663 | } else { |
| 1664 | mddev->reshape_position = MaxSector; |
| 1665 | mddev->delta_disks = 0; |
| 1666 | mddev->new_level = mddev->level; |
| 1667 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1668 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1671 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1672 | /* Insist of good event counter while assembling, except for |
| 1673 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1675 | if (rdev->desc_nr >= 0 && |
| 1676 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1677 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1678 | if (ev1 < mddev->events) |
| 1679 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1680 | } else if (mddev->bitmap) { |
| 1681 | /* If adding to array with a bitmap, then we can accept an |
| 1682 | * older device, but not too old. |
| 1683 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1684 | if (ev1 < mddev->bitmap->events_cleared) |
| 1685 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1686 | } else { |
| 1687 | if (ev1 < mddev->events) |
| 1688 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1689 | return 0; |
| 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1692 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1693 | if (rdev->desc_nr < 0 || |
| 1694 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1695 | role = 0xffff; |
| 1696 | rdev->desc_nr = -1; |
| 1697 | } else |
| 1698 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | switch(role) { |
| 1700 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | break; |
| 1702 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1703 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | break; |
| 1705 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1706 | if ((le32_to_cpu(sb->feature_map) & |
| 1707 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1708 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1709 | else |
| 1710 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | rdev->raid_disk = role; |
| 1712 | break; |
| 1713 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1714 | if (sb->devflags & WriteMostly1) |
| 1715 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1716 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1717 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1718 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1719 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
| 1723 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1724 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | { |
| 1726 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1727 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | int max_dev, i; |
| 1729 | /* make rdev->sb match mddev and rdev data. */ |
| 1730 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1731 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | |
| 1733 | sb->feature_map = 0; |
| 1734 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1735 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1738 | |
| 1739 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1740 | sb->events = cpu_to_le64(mddev->events); |
| 1741 | if (mddev->in_sync) |
| 1742 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1743 | else |
| 1744 | sb->resync_offset = cpu_to_le64(0); |
| 1745 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1746 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1747 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1748 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1749 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1750 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1751 | sb->level = cpu_to_le32(mddev->level); |
| 1752 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1753 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1754 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1755 | sb->devflags |= WriteMostly1; |
| 1756 | else |
| 1757 | sb->devflags &= ~WriteMostly1; |
| 1758 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1759 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1760 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1761 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1762 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1763 | |
| 1764 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1765 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1766 | sb->feature_map |= |
| 1767 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1768 | sb->recovery_offset = |
| 1769 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1770 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1771 | if (test_bit(Replacement, &rdev->flags)) |
| 1772 | sb->feature_map |= |
| 1773 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1774 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1775 | if (mddev->reshape_position != MaxSector) { |
| 1776 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1777 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1778 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1779 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1780 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1781 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1782 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1783 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1784 | if (rdev->badblocks.count == 0) |
| 1785 | /* Nothing to do for bad blocks*/ ; |
| 1786 | else if (sb->bblog_offset == 0) |
| 1787 | /* Cannot record bad blocks on this device */ |
| 1788 | md_error(mddev, rdev); |
| 1789 | else { |
| 1790 | struct badblocks *bb = &rdev->badblocks; |
| 1791 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1792 | u64 *p = bb->page; |
| 1793 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1794 | if (bb->changed) { |
| 1795 | unsigned seq; |
| 1796 | |
| 1797 | retry: |
| 1798 | seq = read_seqbegin(&bb->lock); |
| 1799 | |
| 1800 | memset(bbp, 0xff, PAGE_SIZE); |
| 1801 | |
| 1802 | for (i = 0 ; i < bb->count ; i++) { |
| 1803 | u64 internal_bb = *p++; |
| 1804 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1805 | | BB_LEN(internal_bb)); |
| 1806 | *bbp++ = cpu_to_le64(store_bb); |
| 1807 | } |
| 1808 | if (read_seqretry(&bb->lock, seq)) |
| 1809 | goto retry; |
| 1810 | |
| 1811 | bb->sector = (rdev->sb_start + |
| 1812 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1813 | bb->size = le16_to_cpu(sb->bblog_size); |
| 1814 | bb->changed = 0; |
| 1815 | } |
| 1816 | } |
| 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1819 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | if (rdev2->desc_nr+1 > max_dev) |
| 1821 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1822 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1823 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1824 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1825 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1826 | rdev->sb_size = max_dev * 2 + 256; |
| 1827 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1828 | if (rdev->sb_size & bmask) |
| 1829 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1830 | } else |
| 1831 | max_dev = le32_to_cpu(sb->max_dev); |
| 1832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | for (i=0; i<max_dev;i++) |
| 1834 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1835 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1836 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1838 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1840 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1842 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1843 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | else |
| 1845 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1846 | } |
| 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1849 | } |
| 1850 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1851 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1852 | 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] | 1853 | { |
| 1854 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1855 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1856 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1857 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1858 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1859 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1860 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1861 | max_sectors -= rdev->data_offset; |
| 1862 | if (!num_sectors || num_sectors > max_sectors) |
| 1863 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1864 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1865 | /* minor version 0 with bitmap we can't move */ |
| 1866 | return 0; |
| 1867 | } else { |
| 1868 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1869 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1870 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1871 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1872 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1873 | if (!num_sectors || num_sectors > max_sectors) |
| 1874 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1875 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1876 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1877 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1878 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1879 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1880 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1881 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1882 | rdev->sb_page); |
| 1883 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1884 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1887 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | [0] = { |
| 1889 | .name = "0.90.0", |
| 1890 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1891 | .load_super = super_90_load, |
| 1892 | .validate_super = super_90_validate, |
| 1893 | .sync_super = super_90_sync, |
| 1894 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | }, |
| 1896 | [1] = { |
| 1897 | .name = "md-1", |
| 1898 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1899 | .load_super = super_1_load, |
| 1900 | .validate_super = super_1_validate, |
| 1901 | .sync_super = super_1_sync, |
| 1902 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | }, |
| 1904 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1906 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1907 | { |
| 1908 | if (mddev->sync_super) { |
| 1909 | mddev->sync_super(mddev, rdev); |
| 1910 | return; |
| 1911 | } |
| 1912 | |
| 1913 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1914 | |
| 1915 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1916 | } |
| 1917 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1918 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1920 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1922 | rcu_read_lock(); |
| 1923 | rdev_for_each_rcu(rdev, mddev1) |
| 1924 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1925 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1926 | rdev2->bdev->bd_contains) { |
| 1927 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1928 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1929 | } |
| 1930 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | static LIST_HEAD(pending_raid_disks); |
| 1935 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1936 | /* |
| 1937 | * Try to register data integrity profile for an mddev |
| 1938 | * |
| 1939 | * This is called when an array is started and after a disk has been kicked |
| 1940 | * from the array. It only succeeds if all working and active component devices |
| 1941 | * are integrity capable with matching profiles. |
| 1942 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1943 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1944 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1945 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1946 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1947 | if (list_empty(&mddev->disks)) |
| 1948 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1949 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1950 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1951 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1952 | /* skip spares and non-functional disks */ |
| 1953 | if (test_bit(Faulty, &rdev->flags)) |
| 1954 | continue; |
| 1955 | if (rdev->raid_disk < 0) |
| 1956 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1957 | if (!reference) { |
| 1958 | /* Use the first rdev as the reference */ |
| 1959 | reference = rdev; |
| 1960 | continue; |
| 1961 | } |
| 1962 | /* does this rdev's profile match the reference profile? */ |
| 1963 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1964 | rdev->bdev->bd_disk) < 0) |
| 1965 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1966 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1967 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1968 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1969 | /* |
| 1970 | * All component devices are integrity capable and have matching |
| 1971 | * profiles, register the common profile for the md device. |
| 1972 | */ |
| 1973 | if (blk_integrity_register(mddev->gendisk, |
| 1974 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1975 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1976 | mdname(mddev)); |
| 1977 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1978 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1979 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1980 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1981 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1982 | mdname(mddev)); |
| 1983 | return -EINVAL; |
| 1984 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1985 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1986 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1987 | EXPORT_SYMBOL(md_integrity_register); |
| 1988 | |
| 1989 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1990 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1991 | { |
| 1992 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1993 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1994 | |
| 1995 | if (!bi_mddev) /* nothing to do */ |
| 1996 | return; |
| 1997 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1998 | return; |
| 1999 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2000 | rdev->bdev->bd_disk) >= 0) |
| 2001 | return; |
| 2002 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2003 | blk_integrity_unregister(mddev->gendisk); |
| 2004 | } |
| 2005 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2006 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2007 | 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] | 2008 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2009 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2010 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2011 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2012 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | |
| 2014 | if (rdev->mddev) { |
| 2015 | MD_BUG(); |
| 2016 | return -EINVAL; |
| 2017 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2018 | |
| 2019 | /* prevent duplicates */ |
| 2020 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2021 | return -EEXIST; |
| 2022 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2023 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2024 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2025 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2026 | if (mddev->pers) { |
| 2027 | /* Cannot change size, so fail |
| 2028 | * If mddev->level <= 0, then we don't care |
| 2029 | * about aligning sizes (e.g. linear) |
| 2030 | */ |
| 2031 | if (mddev->level > 0) |
| 2032 | return -ENOSPC; |
| 2033 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2034 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
| 2037 | /* Verify rdev->desc_nr is unique. |
| 2038 | * If it is -1, assign a free number, else |
| 2039 | * check number is not in use |
| 2040 | */ |
| 2041 | if (rdev->desc_nr < 0) { |
| 2042 | int choice = 0; |
| 2043 | if (mddev->pers) choice = mddev->raid_disks; |
| 2044 | while (find_rdev_nr(mddev, choice)) |
| 2045 | choice++; |
| 2046 | rdev->desc_nr = choice; |
| 2047 | } else { |
| 2048 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2049 | return -EBUSY; |
| 2050 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2051 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2052 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2053 | mdname(mddev), mddev->max_disks); |
| 2054 | return -EBUSY; |
| 2055 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2056 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2057 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2058 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2061 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2062 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2063 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2064 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2065 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2066 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2067 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2068 | /* failure here is OK */; |
| 2069 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2070 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2071 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2072 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2073 | |
| 2074 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2075 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2078 | |
| 2079 | fail: |
| 2080 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2081 | b, mdname(mddev)); |
| 2082 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2085 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2086 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2087 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2088 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2089 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2092 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
| 2094 | char b[BDEVNAME_SIZE]; |
| 2095 | if (!rdev->mddev) { |
| 2096 | MD_BUG(); |
| 2097 | return; |
| 2098 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2099 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2100 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2102 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2103 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2104 | sysfs_put(rdev->sysfs_state); |
| 2105 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2106 | kfree(rdev->badblocks.page); |
| 2107 | rdev->badblocks.count = 0; |
| 2108 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2109 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2110 | * writing to 'remove' to "dev/state". We also need |
| 2111 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2112 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2113 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2114 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2115 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2116 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * prevent the device from being mounted, repartitioned or |
| 2121 | * otherwise reused by a RAID array (or any other kernel |
| 2122 | * subsystem), by bd_claiming the device. |
| 2123 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2124 | 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] | 2125 | { |
| 2126 | int err = 0; |
| 2127 | struct block_device *bdev; |
| 2128 | char b[BDEVNAME_SIZE]; |
| 2129 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2130 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2131 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | if (IS_ERR(bdev)) { |
| 2133 | printk(KERN_ERR "md: could not open %s.\n", |
| 2134 | __bdevname(dev, b)); |
| 2135 | return PTR_ERR(bdev); |
| 2136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | rdev->bdev = bdev; |
| 2138 | return err; |
| 2139 | } |
| 2140 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2141 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
| 2143 | struct block_device *bdev = rdev->bdev; |
| 2144 | rdev->bdev = NULL; |
| 2145 | if (!bdev) |
| 2146 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2147 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | void md_autodetect_dev(dev_t dev); |
| 2151 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2152 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | { |
| 2154 | char b[BDEVNAME_SIZE]; |
| 2155 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2156 | bdevname(rdev->bdev,b)); |
| 2157 | if (rdev->mddev) |
| 2158 | MD_BUG(); |
| 2159 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2161 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2162 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | #endif |
| 2164 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2165 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2168 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
| 2170 | unbind_rdev_from_array(rdev); |
| 2171 | export_rdev(rdev); |
| 2172 | } |
| 2173 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2174 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2176 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2178 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | if (!rdev->mddev) { |
| 2180 | MD_BUG(); |
| 2181 | continue; |
| 2182 | } |
| 2183 | kick_rdev_from_array(rdev); |
| 2184 | } |
| 2185 | if (!list_empty(&mddev->disks)) |
| 2186 | MD_BUG(); |
| 2187 | mddev->raid_disks = 0; |
| 2188 | mddev->major_version = 0; |
| 2189 | } |
| 2190 | |
| 2191 | static void print_desc(mdp_disk_t *desc) |
| 2192 | { |
| 2193 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2194 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2195 | } |
| 2196 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2197 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | { |
| 2199 | int i; |
| 2200 | |
| 2201 | printk(KERN_INFO |
| 2202 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2203 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2204 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2205 | sb->ctime); |
| 2206 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2207 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2208 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2209 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2210 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2211 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2212 | sb->failed_disks, sb->spare_disks, |
| 2213 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2214 | |
| 2215 | printk(KERN_INFO); |
| 2216 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2217 | mdp_disk_t *desc; |
| 2218 | |
| 2219 | desc = sb->disks + i; |
| 2220 | if (desc->number || desc->major || desc->minor || |
| 2221 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2222 | printk(" D %2d: ", i); |
| 2223 | print_desc(desc); |
| 2224 | } |
| 2225 | } |
| 2226 | printk(KERN_INFO "md: THIS: "); |
| 2227 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2230 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2231 | { |
| 2232 | __u8 *uuid; |
| 2233 | |
| 2234 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2235 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2236 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2237 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2238 | le32_to_cpu(sb->major_version), |
| 2239 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2240 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2241 | sb->set_name, |
| 2242 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2243 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2244 | |
| 2245 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2246 | printk(KERN_INFO |
| 2247 | "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] | 2248 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2249 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2250 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2251 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2252 | le32_to_cpu(sb->level), |
| 2253 | (unsigned long long)le64_to_cpu(sb->size), |
| 2254 | le32_to_cpu(sb->raid_disks), |
| 2255 | le32_to_cpu(sb->layout), |
| 2256 | le32_to_cpu(sb->chunksize), |
| 2257 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2258 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2259 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2260 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2261 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2262 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2263 | sb->devflags, |
| 2264 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2265 | (unsigned long long)le64_to_cpu(sb->events), |
| 2266 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2267 | le32_to_cpu(sb->sb_csum), |
| 2268 | le32_to_cpu(sb->max_dev) |
| 2269 | ); |
| 2270 | } |
| 2271 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2272 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | { |
| 2274 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2275 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2276 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2277 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2278 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2280 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2281 | switch (major_version) { |
| 2282 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2283 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2284 | break; |
| 2285 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2286 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2287 | break; |
| 2288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } else |
| 2290 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2291 | } |
| 2292 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2293 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2295 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2296 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2297 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | char b[BDEVNAME_SIZE]; |
| 2299 | |
| 2300 | printk("\n"); |
| 2301 | printk("md: **********************************\n"); |
| 2302 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2303 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2304 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2306 | if (mddev->bitmap) |
| 2307 | bitmap_print_sb(mddev->bitmap); |
| 2308 | else |
| 2309 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2310 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2312 | printk("\n"); |
| 2313 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2314 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2315 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | } |
| 2317 | printk("md: **********************************\n"); |
| 2318 | printk("\n"); |
| 2319 | } |
| 2320 | |
| 2321 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2322 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2324 | /* Update each superblock (in-memory image), but |
| 2325 | * if we are allowed to, skip spares which already |
| 2326 | * have the right event counter, or have one earlier |
| 2327 | * (which would mean they aren't being marked as dirty |
| 2328 | * with the rest of the array) |
| 2329 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2330 | struct md_rdev *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2331 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2332 | if (rdev->sb_events == mddev->events || |
| 2333 | (nospares && |
| 2334 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2335 | rdev->sb_events+1 == mddev->events)) { |
| 2336 | /* Don't update this superblock */ |
| 2337 | rdev->sb_loaded = 2; |
| 2338 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2339 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2340 | rdev->sb_loaded = 1; |
| 2341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2345 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2347 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2348 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2349 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2350 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2353 | /* First make sure individual recovery_offsets are correct */ |
| 2354 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2355 | if (rdev->raid_disk >= 0 && |
| 2356 | mddev->delta_disks >= 0 && |
| 2357 | !test_bit(In_sync, &rdev->flags) && |
| 2358 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2359 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2360 | |
| 2361 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2362 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2363 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2364 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2365 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2366 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2367 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2368 | if (rdev->badblocks.changed) { |
| 2369 | md_ack_all_badblocks(&rdev->badblocks); |
| 2370 | md_error(mddev, rdev); |
| 2371 | } |
| 2372 | clear_bit(Blocked, &rdev->flags); |
| 2373 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2374 | wake_up(&rdev->blocked_wait); |
| 2375 | } |
| 2376 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2377 | wake_up(&mddev->sb_wait); |
| 2378 | return; |
| 2379 | } |
| 2380 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2381 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2382 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2383 | mddev->utime = get_seconds(); |
| 2384 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2385 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2386 | force_change = 1; |
| 2387 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2388 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2389 | * though if events isn't the right even/odd, we will have to do |
| 2390 | * spares after all |
| 2391 | */ |
| 2392 | nospares = 1; |
| 2393 | if (force_change) |
| 2394 | nospares = 0; |
| 2395 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2396 | /* If the array is degraded, then skipping spares is both |
| 2397 | * dangerous and fairly pointless. |
| 2398 | * Dangerous because a device that was removed from the array |
| 2399 | * might have a event_count that still looks up-to-date, |
| 2400 | * so it can be re-added without a resync. |
| 2401 | * Pointless because if there are any spares to skip, |
| 2402 | * then a recovery will happen and soon that array won't |
| 2403 | * be degraded any more and the spare can go back to sleep then. |
| 2404 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2405 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2406 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2407 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2408 | |
| 2409 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2410 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2411 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2412 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2413 | && mddev->can_decrease_events |
| 2414 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2415 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2416 | mddev->can_decrease_events = 0; |
| 2417 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2418 | /* otherwise we have to go forward and ... */ |
| 2419 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2420 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | |
| 2423 | if (!mddev->events) { |
| 2424 | /* |
| 2425 | * oops, this 64-bit counter should never wrap. |
| 2426 | * Either we are in around ~1 trillion A.C., assuming |
| 2427 | * 1 reboot per second, or we have a bug: |
| 2428 | */ |
| 2429 | MD_BUG(); |
| 2430 | mddev->events --; |
| 2431 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2432 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2433 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2434 | if (rdev->badblocks.changed) |
| 2435 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2436 | if (test_bit(Faulty, &rdev->flags)) |
| 2437 | set_bit(FaultRecorded, &rdev->flags); |
| 2438 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2439 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2440 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2441 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2443 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2444 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2446 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2447 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2449 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2450 | if (rdev->sb_loaded != 1) |
| 2451 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2453 | if (!test_bit(Faulty, &rdev->flags) && |
| 2454 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2455 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2456 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2457 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2458 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2459 | bdevname(rdev->bdev, b), |
| 2460 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2461 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2462 | if (rdev->badblocks.size) { |
| 2463 | md_super_write(mddev, rdev, |
| 2464 | rdev->badblocks.sector, |
| 2465 | rdev->badblocks.size << 9, |
| 2466 | rdev->bb_page); |
| 2467 | rdev->badblocks.size = 0; |
| 2468 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2469 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2470 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2471 | pr_debug("md: %s (skipping faulty)\n", |
| 2472 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2473 | else |
| 2474 | pr_debug("(skipping incremental s/r "); |
| 2475 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2476 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | /* only need to write one superblock... */ |
| 2478 | break; |
| 2479 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2480 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2481 | /* 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] | 2482 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2483 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2484 | if (mddev->in_sync != sync_req || |
| 2485 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2486 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2487 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2488 | goto repeat; |
| 2489 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2490 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2491 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2492 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2493 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2494 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2495 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2496 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2497 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2498 | clear_bit(Blocked, &rdev->flags); |
| 2499 | |
| 2500 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2501 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2502 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2503 | wake_up(&rdev->blocked_wait); |
| 2504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2507 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2508 | * We want to accept with case. For this we use cmd_match. |
| 2509 | */ |
| 2510 | static int cmd_match(const char *cmd, const char *str) |
| 2511 | { |
| 2512 | /* See if cmd, written into a sysfs file, matches |
| 2513 | * str. They must either be the same, or cmd can |
| 2514 | * have a trailing newline |
| 2515 | */ |
| 2516 | while (*cmd && *str && *cmd == *str) { |
| 2517 | cmd++; |
| 2518 | str++; |
| 2519 | } |
| 2520 | if (*cmd == '\n') |
| 2521 | cmd++; |
| 2522 | if (*str || *cmd) |
| 2523 | return 0; |
| 2524 | return 1; |
| 2525 | } |
| 2526 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2527 | struct rdev_sysfs_entry { |
| 2528 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2529 | ssize_t (*show)(struct md_rdev *, char *); |
| 2530 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2531 | }; |
| 2532 | |
| 2533 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2534 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2535 | { |
| 2536 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2537 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2538 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2539 | if (test_bit(Faulty, &rdev->flags) || |
| 2540 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2541 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2542 | sep = ","; |
| 2543 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2544 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2545 | len += sprintf(page+len, "%sin_sync",sep); |
| 2546 | sep = ","; |
| 2547 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2548 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2549 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2550 | sep = ","; |
| 2551 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2552 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2553 | (rdev->badblocks.unacked_exist |
| 2554 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2555 | len += sprintf(page+len, "%sblocked", sep); |
| 2556 | sep = ","; |
| 2557 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2558 | if (!test_bit(Faulty, &rdev->flags) && |
| 2559 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2560 | len += sprintf(page+len, "%sspare", sep); |
| 2561 | sep = ","; |
| 2562 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2563 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2564 | len += sprintf(page+len, "%swrite_error", sep); |
| 2565 | sep = ","; |
| 2566 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2567 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2568 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2569 | sep = ","; |
| 2570 | } |
| 2571 | if (test_bit(Replacement, &rdev->flags)) { |
| 2572 | len += sprintf(page+len, "%sreplacement", sep); |
| 2573 | sep = ","; |
| 2574 | } |
| 2575 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2576 | return len+sprintf(page+len, "\n"); |
| 2577 | } |
| 2578 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2579 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2580 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2581 | { |
| 2582 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2583 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2584 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2585 | * writemostly - sets write_mostly |
| 2586 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2587 | * blocked - sets the Blocked flags |
| 2588 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2589 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2590 | * write_error - sets WriteErrorSeen |
| 2591 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2592 | */ |
| 2593 | int err = -EINVAL; |
| 2594 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2595 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2596 | if (test_bit(Faulty, &rdev->flags)) |
| 2597 | err = 0; |
| 2598 | else |
| 2599 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2600 | } else if (cmd_match(buf, "remove")) { |
| 2601 | if (rdev->raid_disk >= 0) |
| 2602 | err = -EBUSY; |
| 2603 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2604 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2605 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2606 | if (mddev->pers) |
| 2607 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2608 | md_new_event(mddev); |
| 2609 | err = 0; |
| 2610 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2611 | } else if (cmd_match(buf, "writemostly")) { |
| 2612 | set_bit(WriteMostly, &rdev->flags); |
| 2613 | err = 0; |
| 2614 | } else if (cmd_match(buf, "-writemostly")) { |
| 2615 | clear_bit(WriteMostly, &rdev->flags); |
| 2616 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2617 | } else if (cmd_match(buf, "blocked")) { |
| 2618 | set_bit(Blocked, &rdev->flags); |
| 2619 | err = 0; |
| 2620 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2621 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2622 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2623 | /* metadata handler doesn't understand badblocks, |
| 2624 | * so we need to fail the device |
| 2625 | */ |
| 2626 | md_error(rdev->mddev, rdev); |
| 2627 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2628 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2629 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2630 | wake_up(&rdev->blocked_wait); |
| 2631 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2632 | md_wakeup_thread(rdev->mddev->thread); |
| 2633 | |
| 2634 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2635 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2636 | set_bit(In_sync, &rdev->flags); |
| 2637 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2638 | } else if (cmd_match(buf, "write_error")) { |
| 2639 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2640 | err = 0; |
| 2641 | } else if (cmd_match(buf, "-write_error")) { |
| 2642 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2643 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2644 | } else if (cmd_match(buf, "want_replacement")) { |
| 2645 | /* Any non-spare device that is not a replacement can |
| 2646 | * become want_replacement at any time, but we then need to |
| 2647 | * check if recovery is needed. |
| 2648 | */ |
| 2649 | if (rdev->raid_disk >= 0 && |
| 2650 | !test_bit(Replacement, &rdev->flags)) |
| 2651 | set_bit(WantReplacement, &rdev->flags); |
| 2652 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2653 | md_wakeup_thread(rdev->mddev->thread); |
| 2654 | err = 0; |
| 2655 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2656 | /* Clearing 'want_replacement' is always allowed. |
| 2657 | * Once replacements starts it is too late though. |
| 2658 | */ |
| 2659 | err = 0; |
| 2660 | clear_bit(WantReplacement, &rdev->flags); |
| 2661 | } else if (cmd_match(buf, "replacement")) { |
| 2662 | /* Can only set a device as a replacement when array has not |
| 2663 | * yet been started. Once running, replacement is automatic |
| 2664 | * from spares, or by assigning 'slot'. |
| 2665 | */ |
| 2666 | if (rdev->mddev->pers) |
| 2667 | err = -EBUSY; |
| 2668 | else { |
| 2669 | set_bit(Replacement, &rdev->flags); |
| 2670 | err = 0; |
| 2671 | } |
| 2672 | } else if (cmd_match(buf, "-replacement")) { |
| 2673 | /* Similarly, can only clear Replacement before start */ |
| 2674 | if (rdev->mddev->pers) |
| 2675 | err = -EBUSY; |
| 2676 | else { |
| 2677 | clear_bit(Replacement, &rdev->flags); |
| 2678 | err = 0; |
| 2679 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2680 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2681 | if (!err) |
| 2682 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2683 | return err ? err : len; |
| 2684 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2685 | static struct rdev_sysfs_entry rdev_state = |
| 2686 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2687 | |
| 2688 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2689 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2690 | { |
| 2691 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2692 | } |
| 2693 | |
| 2694 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2695 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2696 | { |
| 2697 | char *e; |
| 2698 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2699 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2700 | atomic_set(&rdev->corrected_errors, n); |
| 2701 | return len; |
| 2702 | } |
| 2703 | return -EINVAL; |
| 2704 | } |
| 2705 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2706 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2707 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2708 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2709 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2710 | { |
| 2711 | if (rdev->raid_disk < 0) |
| 2712 | return sprintf(page, "none\n"); |
| 2713 | else |
| 2714 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2715 | } |
| 2716 | |
| 2717 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2718 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2719 | { |
| 2720 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2721 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2722 | int slot = simple_strtoul(buf, &e, 10); |
| 2723 | if (strncmp(buf, "none", 4)==0) |
| 2724 | slot = -1; |
| 2725 | else if (e==buf || (*e && *e!= '\n')) |
| 2726 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2727 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2728 | /* Setting 'slot' on an active array requires also |
| 2729 | * updating the 'rd%d' link, and communicating |
| 2730 | * with the personality with ->hot_*_disk. |
| 2731 | * For now we only support removing |
| 2732 | * failed/spare devices. This normally happens automatically, |
| 2733 | * but not when the metadata is externally managed. |
| 2734 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2735 | if (rdev->raid_disk == -1) |
| 2736 | return -EEXIST; |
| 2737 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2738 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2739 | return -EINVAL; |
| 2740 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2741 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2742 | if (err) |
| 2743 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2744 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2745 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2746 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2747 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2748 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2749 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2750 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2751 | */ |
| 2752 | |
| 2753 | if (rdev->raid_disk != -1) |
| 2754 | return -EBUSY; |
| 2755 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2756 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2757 | return -EBUSY; |
| 2758 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2759 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2760 | return -EINVAL; |
| 2761 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2762 | if (slot >= rdev->mddev->raid_disks && |
| 2763 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2764 | return -ENOSPC; |
| 2765 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2766 | rdev->raid_disk = slot; |
| 2767 | if (test_bit(In_sync, &rdev->flags)) |
| 2768 | rdev->saved_raid_disk = slot; |
| 2769 | else |
| 2770 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2771 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2772 | err = rdev->mddev->pers-> |
| 2773 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2774 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2775 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2776 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2777 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2778 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2779 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2780 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2781 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2782 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2783 | if (slot >= rdev->mddev->raid_disks && |
| 2784 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2785 | return -ENOSPC; |
| 2786 | rdev->raid_disk = slot; |
| 2787 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2788 | clear_bit(Faulty, &rdev->flags); |
| 2789 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2790 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2791 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2792 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2793 | return len; |
| 2794 | } |
| 2795 | |
| 2796 | |
| 2797 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2798 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2799 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2800 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2801 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2802 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2803 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2807 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2808 | { |
| 2809 | char *e; |
| 2810 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2811 | if (e==buf || (*e && *e != '\n')) |
| 2812 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2813 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2814 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2815 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2816 | /* Must set offset before size, so overlap checks |
| 2817 | * can be sane */ |
| 2818 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2819 | rdev->data_offset = offset; |
| 2820 | return len; |
| 2821 | } |
| 2822 | |
| 2823 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2824 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2825 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2826 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2827 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2828 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2829 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2832 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2833 | { |
| 2834 | /* check if two start/length pairs overlap */ |
| 2835 | if (s1+l1 <= s2) |
| 2836 | return 0; |
| 2837 | if (s2+l2 <= s1) |
| 2838 | return 0; |
| 2839 | return 1; |
| 2840 | } |
| 2841 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2842 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2843 | { |
| 2844 | unsigned long long blocks; |
| 2845 | sector_t new; |
| 2846 | |
| 2847 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2848 | return -EINVAL; |
| 2849 | |
| 2850 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2851 | return -EINVAL; /* sector conversion overflow */ |
| 2852 | |
| 2853 | new = blocks * 2; |
| 2854 | if (new != blocks * 2) |
| 2855 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2856 | |
| 2857 | *sectors = new; |
| 2858 | return 0; |
| 2859 | } |
| 2860 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2861 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2862 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2863 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2864 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2865 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2866 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2867 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2868 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2869 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2870 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2871 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2872 | sectors = super_types[my_mddev->major_version]. |
| 2873 | rdev_size_change(rdev, sectors); |
| 2874 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2875 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2876 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2877 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2878 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2879 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2880 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2881 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2882 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2883 | rdev->sectors = sectors; |
| 2884 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2885 | /* need to check that all other rdevs with the same ->bdev |
| 2886 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2887 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2888 | * we have to change it back, we will have the lock again. |
| 2889 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2890 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2891 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2892 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2893 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2894 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2895 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2896 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2897 | |
| 2898 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2899 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2900 | if (rdev->bdev == rdev2->bdev && |
| 2901 | rdev != rdev2 && |
| 2902 | overlaps(rdev->data_offset, rdev->sectors, |
| 2903 | rdev2->data_offset, |
| 2904 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2905 | overlap = 1; |
| 2906 | break; |
| 2907 | } |
| 2908 | mddev_unlock(mddev); |
| 2909 | if (overlap) { |
| 2910 | mddev_put(mddev); |
| 2911 | break; |
| 2912 | } |
| 2913 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2914 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2915 | if (overlap) { |
| 2916 | /* Someone else could have slipped in a size |
| 2917 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2918 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2919 | * safe, and trust userspace not to race with |
| 2920 | * itself |
| 2921 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2922 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2923 | return -EBUSY; |
| 2924 | } |
| 2925 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2926 | return len; |
| 2927 | } |
| 2928 | |
| 2929 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2930 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2931 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2932 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2933 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2934 | { |
| 2935 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2936 | |
| 2937 | if (test_bit(In_sync, &rdev->flags) || |
| 2938 | recovery_start == MaxSector) |
| 2939 | return sprintf(page, "none\n"); |
| 2940 | |
| 2941 | return sprintf(page, "%llu\n", recovery_start); |
| 2942 | } |
| 2943 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2944 | 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] | 2945 | { |
| 2946 | unsigned long long recovery_start; |
| 2947 | |
| 2948 | if (cmd_match(buf, "none")) |
| 2949 | recovery_start = MaxSector; |
| 2950 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2951 | return -EINVAL; |
| 2952 | |
| 2953 | if (rdev->mddev->pers && |
| 2954 | rdev->raid_disk >= 0) |
| 2955 | return -EBUSY; |
| 2956 | |
| 2957 | rdev->recovery_offset = recovery_start; |
| 2958 | if (recovery_start == MaxSector) |
| 2959 | set_bit(In_sync, &rdev->flags); |
| 2960 | else |
| 2961 | clear_bit(In_sync, &rdev->flags); |
| 2962 | return len; |
| 2963 | } |
| 2964 | |
| 2965 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2966 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2967 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2968 | |
| 2969 | static ssize_t |
| 2970 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2971 | static ssize_t |
| 2972 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2973 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2974 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2975 | { |
| 2976 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2977 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2978 | 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] | 2979 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2980 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2981 | /* Maybe that ack was all we needed */ |
| 2982 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2983 | wake_up(&rdev->blocked_wait); |
| 2984 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2985 | } |
| 2986 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2987 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2988 | |
| 2989 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2990 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2991 | { |
| 2992 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2993 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2994 | 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] | 2995 | { |
| 2996 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2997 | } |
| 2998 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2999 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3000 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3001 | static struct attribute *rdev_default_attrs[] = { |
| 3002 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3003 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3004 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3005 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3006 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3007 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3008 | &rdev_bad_blocks.attr, |
| 3009 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3010 | NULL, |
| 3011 | }; |
| 3012 | static ssize_t |
| 3013 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3014 | { |
| 3015 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3016 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3017 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3018 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3019 | |
| 3020 | if (!entry->show) |
| 3021 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3022 | |
| 3023 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3024 | if (!rv) { |
| 3025 | if (rdev->mddev == NULL) |
| 3026 | rv = -EBUSY; |
| 3027 | else |
| 3028 | rv = entry->show(rdev, page); |
| 3029 | mddev_unlock(mddev); |
| 3030 | } |
| 3031 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | static ssize_t |
| 3035 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3036 | const char *page, size_t length) |
| 3037 | { |
| 3038 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3039 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3040 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3041 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3042 | |
| 3043 | if (!entry->store) |
| 3044 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3045 | if (!capable(CAP_SYS_ADMIN)) |
| 3046 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3047 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3048 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3049 | if (rdev->mddev == NULL) |
| 3050 | rv = -EBUSY; |
| 3051 | else |
| 3052 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3053 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3054 | } |
| 3055 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | static void rdev_free(struct kobject *ko) |
| 3059 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3060 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3061 | kfree(rdev); |
| 3062 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3063 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3064 | .show = rdev_attr_show, |
| 3065 | .store = rdev_attr_store, |
| 3066 | }; |
| 3067 | static struct kobj_type rdev_ktype = { |
| 3068 | .release = rdev_free, |
| 3069 | .sysfs_ops = &rdev_sysfs_ops, |
| 3070 | .default_attrs = rdev_default_attrs, |
| 3071 | }; |
| 3072 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3073 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3074 | { |
| 3075 | rdev->desc_nr = -1; |
| 3076 | rdev->saved_raid_disk = -1; |
| 3077 | rdev->raid_disk = -1; |
| 3078 | rdev->flags = 0; |
| 3079 | rdev->data_offset = 0; |
| 3080 | rdev->sb_events = 0; |
| 3081 | rdev->last_read_error.tv_sec = 0; |
| 3082 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3083 | rdev->sb_loaded = 0; |
| 3084 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3085 | atomic_set(&rdev->nr_pending, 0); |
| 3086 | atomic_set(&rdev->read_errors, 0); |
| 3087 | atomic_set(&rdev->corrected_errors, 0); |
| 3088 | |
| 3089 | INIT_LIST_HEAD(&rdev->same_set); |
| 3090 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3091 | |
| 3092 | /* Add space to store bad block list. |
| 3093 | * This reserves the space even on arrays where it cannot |
| 3094 | * be used - I wonder if that matters |
| 3095 | */ |
| 3096 | rdev->badblocks.count = 0; |
| 3097 | rdev->badblocks.shift = 0; |
| 3098 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3099 | seqlock_init(&rdev->badblocks.lock); |
| 3100 | if (rdev->badblocks.page == NULL) |
| 3101 | return -ENOMEM; |
| 3102 | |
| 3103 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3104 | } |
| 3105 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | /* |
| 3107 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3108 | * |
| 3109 | * mark the device faulty if: |
| 3110 | * |
| 3111 | * - the device is nonexistent (zero size) |
| 3112 | * - the device has no valid superblock |
| 3113 | * |
| 3114 | * a faulty rdev _never_ has rdev->sb set. |
| 3115 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3116 | 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] | 3117 | { |
| 3118 | char b[BDEVNAME_SIZE]; |
| 3119 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3120 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | sector_t size; |
| 3122 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3123 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | if (!rdev) { |
| 3125 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3126 | return ERR_PTR(-ENOMEM); |
| 3127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3129 | err = md_rdev_init(rdev); |
| 3130 | if (err) |
| 3131 | goto abort_free; |
| 3132 | err = alloc_disk_sb(rdev); |
| 3133 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | goto abort_free; |
| 3135 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3136 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | if (err) |
| 3138 | goto abort_free; |
| 3139 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3140 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3141 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3142 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | if (!size) { |
| 3144 | printk(KERN_WARNING |
| 3145 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3146 | bdevname(rdev->bdev,b)); |
| 3147 | err = -EINVAL; |
| 3148 | goto abort_free; |
| 3149 | } |
| 3150 | |
| 3151 | if (super_format >= 0) { |
| 3152 | err = super_types[super_format]. |
| 3153 | load_super(rdev, NULL, super_minor); |
| 3154 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3155 | printk(KERN_WARNING |
| 3156 | "md: %s does not have a valid v%d.%d " |
| 3157 | "superblock, not importing!\n", |
| 3158 | bdevname(rdev->bdev,b), |
| 3159 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | goto abort_free; |
| 3161 | } |
| 3162 | if (err < 0) { |
| 3163 | printk(KERN_WARNING |
| 3164 | "md: could not read %s's sb, not importing!\n", |
| 3165 | bdevname(rdev->bdev,b)); |
| 3166 | goto abort_free; |
| 3167 | } |
| 3168 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3169 | if (super_format == -1) |
| 3170 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3171 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | return rdev; |
| 3174 | |
| 3175 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3176 | if (rdev->bdev) |
| 3177 | unlock_rdev(rdev); |
| 3178 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3179 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | kfree(rdev); |
| 3181 | return ERR_PTR(err); |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * Check a full RAID array for plausibility |
| 3186 | */ |
| 3187 | |
| 3188 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3189 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | { |
| 3191 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3192 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | char b[BDEVNAME_SIZE]; |
| 3194 | |
| 3195 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3196 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | switch (super_types[mddev->major_version]. |
| 3198 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3199 | case 1: |
| 3200 | freshest = rdev; |
| 3201 | break; |
| 3202 | case 0: |
| 3203 | break; |
| 3204 | default: |
| 3205 | printk( KERN_ERR \ |
| 3206 | "md: fatal superblock inconsistency in %s" |
| 3207 | " -- removing from array\n", |
| 3208 | bdevname(rdev->bdev,b)); |
| 3209 | kick_rdev_from_array(rdev); |
| 3210 | } |
| 3211 | |
| 3212 | |
| 3213 | super_types[mddev->major_version]. |
| 3214 | validate_super(mddev, freshest); |
| 3215 | |
| 3216 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3217 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3218 | if (mddev->max_disks && |
| 3219 | (rdev->desc_nr >= mddev->max_disks || |
| 3220 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3221 | printk(KERN_WARNING |
| 3222 | "md: %s: %s: only %d devices permitted\n", |
| 3223 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3224 | mddev->max_disks); |
| 3225 | kick_rdev_from_array(rdev); |
| 3226 | continue; |
| 3227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | if (rdev != freshest) |
| 3229 | if (super_types[mddev->major_version]. |
| 3230 | validate_super(mddev, rdev)) { |
| 3231 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3232 | " from array!\n", |
| 3233 | bdevname(rdev->bdev,b)); |
| 3234 | kick_rdev_from_array(rdev); |
| 3235 | continue; |
| 3236 | } |
| 3237 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3238 | rdev->desc_nr = i++; |
| 3239 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3240 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3241 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3242 | rdev->raid_disk = -1; |
| 3243 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | } |
| 3245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | } |
| 3247 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3248 | /* Read a fixed-point number. |
| 3249 | * Numbers in sysfs attributes should be in "standard" units where |
| 3250 | * possible, so time should be in seconds. |
| 3251 | * However we internally use a a much smaller unit such as |
| 3252 | * milliseconds or jiffies. |
| 3253 | * This function takes a decimal number with a possible fractional |
| 3254 | * component, and produces an integer which is the result of |
| 3255 | * multiplying that number by 10^'scale'. |
| 3256 | * all without any floating-point arithmetic. |
| 3257 | */ |
| 3258 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3259 | { |
| 3260 | unsigned long result = 0; |
| 3261 | long decimals = -1; |
| 3262 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3263 | if (*cp == '.') |
| 3264 | decimals = 0; |
| 3265 | else if (decimals < scale) { |
| 3266 | unsigned int value; |
| 3267 | value = *cp - '0'; |
| 3268 | result = result * 10 + value; |
| 3269 | if (decimals >= 0) |
| 3270 | decimals++; |
| 3271 | } |
| 3272 | cp++; |
| 3273 | } |
| 3274 | if (*cp == '\n') |
| 3275 | cp++; |
| 3276 | if (*cp) |
| 3277 | return -EINVAL; |
| 3278 | if (decimals < 0) |
| 3279 | decimals = 0; |
| 3280 | while (decimals < scale) { |
| 3281 | result *= 10; |
| 3282 | decimals ++; |
| 3283 | } |
| 3284 | *res = result; |
| 3285 | return 0; |
| 3286 | } |
| 3287 | |
| 3288 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3289 | static void md_safemode_timeout(unsigned long data); |
| 3290 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3291 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3292 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3293 | { |
| 3294 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3295 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3296 | } |
| 3297 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3298 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3299 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3300 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3301 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3302 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3303 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3304 | if (msec == 0) |
| 3305 | mddev->safemode_delay = 0; |
| 3306 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3307 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3308 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3309 | if (mddev->safemode_delay == 0) |
| 3310 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3311 | if (mddev->safemode_delay < old_delay) |
| 3312 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3313 | } |
| 3314 | return len; |
| 3315 | } |
| 3316 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3317 | __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] | 3318 | |
| 3319 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3320 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3321 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3322 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3323 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3324 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3325 | else if (mddev->clevel[0]) |
| 3326 | return sprintf(page, "%s\n", mddev->clevel); |
| 3327 | else if (mddev->level != LEVEL_NONE) |
| 3328 | return sprintf(page, "%d\n", mddev->level); |
| 3329 | else |
| 3330 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3331 | } |
| 3332 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3333 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3334 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3335 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3336 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3337 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3338 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3339 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3340 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3341 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3342 | |
| 3343 | if (mddev->pers == NULL) { |
| 3344 | if (len == 0) |
| 3345 | return 0; |
| 3346 | if (len >= sizeof(mddev->clevel)) |
| 3347 | return -ENOSPC; |
| 3348 | strncpy(mddev->clevel, buf, len); |
| 3349 | if (mddev->clevel[len-1] == '\n') |
| 3350 | len--; |
| 3351 | mddev->clevel[len] = 0; |
| 3352 | mddev->level = LEVEL_NONE; |
| 3353 | return rv; |
| 3354 | } |
| 3355 | |
| 3356 | /* request to change the personality. Need to ensure: |
| 3357 | * - array is not engaged in resync/recovery/reshape |
| 3358 | * - old personality can be suspended |
| 3359 | * - new personality will access other array. |
| 3360 | */ |
| 3361 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3362 | if (mddev->sync_thread || |
| 3363 | mddev->reshape_position != MaxSector || |
| 3364 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3365 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3366 | |
| 3367 | if (!mddev->pers->quiesce) { |
| 3368 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3369 | mdname(mddev), mddev->pers->name); |
| 3370 | return -EINVAL; |
| 3371 | } |
| 3372 | |
| 3373 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3374 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3375 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3376 | strncpy(clevel, buf, len); |
| 3377 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3378 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3379 | clevel[len] = 0; |
| 3380 | if (strict_strtol(clevel, 10, &level)) |
| 3381 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3382 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3383 | if (request_module("md-%s", clevel) != 0) |
| 3384 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3385 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3386 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3387 | if (!pers || !try_module_get(pers->owner)) { |
| 3388 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3389 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3390 | return -EINVAL; |
| 3391 | } |
| 3392 | spin_unlock(&pers_lock); |
| 3393 | |
| 3394 | if (pers == mddev->pers) { |
| 3395 | /* Nothing to do! */ |
| 3396 | module_put(pers->owner); |
| 3397 | return rv; |
| 3398 | } |
| 3399 | if (!pers->takeover) { |
| 3400 | module_put(pers->owner); |
| 3401 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3402 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3403 | return -EINVAL; |
| 3404 | } |
| 3405 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3406 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3407 | rdev->new_raid_disk = rdev->raid_disk; |
| 3408 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3409 | /* ->takeover must set new_* and/or delta_disks |
| 3410 | * if it succeeds, and may set them when it fails. |
| 3411 | */ |
| 3412 | priv = pers->takeover(mddev); |
| 3413 | if (IS_ERR(priv)) { |
| 3414 | mddev->new_level = mddev->level; |
| 3415 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3416 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3417 | mddev->raid_disks -= mddev->delta_disks; |
| 3418 | mddev->delta_disks = 0; |
| 3419 | module_put(pers->owner); |
| 3420 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3421 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3422 | return PTR_ERR(priv); |
| 3423 | } |
| 3424 | |
| 3425 | /* Looks like we have a winner */ |
| 3426 | mddev_suspend(mddev); |
| 3427 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3428 | |
| 3429 | if (mddev->pers->sync_request == NULL && |
| 3430 | pers->sync_request != NULL) { |
| 3431 | /* need to add the md_redundancy_group */ |
| 3432 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3433 | printk(KERN_WARNING |
| 3434 | "md: cannot register extra attributes for %s\n", |
| 3435 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3436 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3437 | } |
| 3438 | if (mddev->pers->sync_request != NULL && |
| 3439 | pers->sync_request == NULL) { |
| 3440 | /* need to remove the md_redundancy_group */ |
| 3441 | if (mddev->to_remove == NULL) |
| 3442 | mddev->to_remove = &md_redundancy_group; |
| 3443 | } |
| 3444 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3445 | if (mddev->pers->sync_request == NULL && |
| 3446 | mddev->external) { |
| 3447 | /* We are converting from a no-redundancy array |
| 3448 | * to a redundancy array and metadata is managed |
| 3449 | * externally so we need to be sure that writes |
| 3450 | * won't block due to a need to transition |
| 3451 | * clean->dirty |
| 3452 | * until external management is started. |
| 3453 | */ |
| 3454 | mddev->in_sync = 0; |
| 3455 | mddev->safemode_delay = 0; |
| 3456 | mddev->safemode = 0; |
| 3457 | } |
| 3458 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3459 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3460 | if (rdev->raid_disk < 0) |
| 3461 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3462 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3463 | rdev->new_raid_disk = -1; |
| 3464 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3465 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3466 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3467 | } |
| 3468 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3469 | if (rdev->raid_disk < 0) |
| 3470 | continue; |
| 3471 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3472 | continue; |
| 3473 | rdev->raid_disk = rdev->new_raid_disk; |
| 3474 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3475 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3476 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3477 | if (sysfs_link_rdev(mddev, rdev)) |
| 3478 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3479 | " for %s after level change\n", |
| 3480 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3481 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3485 | mddev->pers = pers; |
| 3486 | mddev->private = priv; |
| 3487 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3488 | mddev->level = mddev->new_level; |
| 3489 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3490 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3491 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3492 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3493 | if (mddev->pers->sync_request == NULL) { |
| 3494 | /* this is now an array without redundancy, so |
| 3495 | * it must always be in_sync |
| 3496 | */ |
| 3497 | mddev->in_sync = 1; |
| 3498 | del_timer_sync(&mddev->safemode_timer); |
| 3499 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3500 | pers->run(mddev); |
| 3501 | mddev_resume(mddev); |
| 3502 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3503 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3504 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3505 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3506 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3507 | return rv; |
| 3508 | } |
| 3509 | |
| 3510 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3511 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3512 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3513 | |
| 3514 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3515 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3516 | { |
| 3517 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3518 | if (mddev->reshape_position != MaxSector && |
| 3519 | mddev->layout != mddev->new_layout) |
| 3520 | return sprintf(page, "%d (%d)\n", |
| 3521 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3522 | return sprintf(page, "%d\n", mddev->layout); |
| 3523 | } |
| 3524 | |
| 3525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3526 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3527 | { |
| 3528 | char *e; |
| 3529 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3530 | |
| 3531 | if (!*buf || (*e && *e != '\n')) |
| 3532 | return -EINVAL; |
| 3533 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3534 | if (mddev->pers) { |
| 3535 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3536 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3537 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3538 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3539 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3540 | if (err) { |
| 3541 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3542 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3543 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3544 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3545 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3546 | if (mddev->reshape_position == MaxSector) |
| 3547 | mddev->layout = n; |
| 3548 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3549 | return len; |
| 3550 | } |
| 3551 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3552 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3553 | |
| 3554 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3555 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3556 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3557 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3558 | if (mddev->raid_disks == 0) |
| 3559 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3560 | if (mddev->reshape_position != MaxSector && |
| 3561 | mddev->delta_disks != 0) |
| 3562 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3563 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3564 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3565 | } |
| 3566 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3567 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3568 | |
| 3569 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3570 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3571 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3572 | char *e; |
| 3573 | int rv = 0; |
| 3574 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3575 | |
| 3576 | if (!*buf || (*e && *e != '\n')) |
| 3577 | return -EINVAL; |
| 3578 | |
| 3579 | if (mddev->pers) |
| 3580 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3581 | else if (mddev->reshape_position != MaxSector) { |
| 3582 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3583 | mddev->delta_disks = n - olddisks; |
| 3584 | mddev->raid_disks = n; |
| 3585 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3586 | mddev->raid_disks = n; |
| 3587 | return rv ? rv : len; |
| 3588 | } |
| 3589 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3590 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3591 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3592 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3593 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3594 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3595 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3596 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3597 | return sprintf(page, "%d (%d)\n", |
| 3598 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3599 | mddev->chunk_sectors << 9); |
| 3600 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3604 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3605 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3606 | char *e; |
| 3607 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3608 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3609 | if (!*buf || (*e && *e != '\n')) |
| 3610 | return -EINVAL; |
| 3611 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3612 | if (mddev->pers) { |
| 3613 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3614 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3615 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3616 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3617 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3618 | if (err) { |
| 3619 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3620 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3621 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3622 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3623 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3624 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3625 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3626 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3627 | return len; |
| 3628 | } |
| 3629 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3630 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3631 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3632 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3633 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3634 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3635 | if (mddev->recovery_cp == MaxSector) |
| 3636 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3637 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3638 | } |
| 3639 | |
| 3640 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3641 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3642 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3643 | char *e; |
| 3644 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3645 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3646 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3647 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3648 | if (cmd_match(buf, "none")) |
| 3649 | n = MaxSector; |
| 3650 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3651 | return -EINVAL; |
| 3652 | |
| 3653 | mddev->recovery_cp = n; |
| 3654 | return len; |
| 3655 | } |
| 3656 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3657 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3658 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3659 | /* |
| 3660 | * The array state can be: |
| 3661 | * |
| 3662 | * clear |
| 3663 | * No devices, no size, no level |
| 3664 | * Equivalent to STOP_ARRAY ioctl |
| 3665 | * inactive |
| 3666 | * May have some settings, but array is not active |
| 3667 | * all IO results in error |
| 3668 | * When written, doesn't tear down array, but just stops it |
| 3669 | * suspended (not supported yet) |
| 3670 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3671 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3672 | * readonly |
| 3673 | * no resync can happen. no superblocks get written. |
| 3674 | * write requests fail |
| 3675 | * read-auto |
| 3676 | * like readonly, but behaves like 'clean' on a write request. |
| 3677 | * |
| 3678 | * clean - no pending writes, but otherwise active. |
| 3679 | * When written to inactive array, starts without resync |
| 3680 | * If a write request arrives then |
| 3681 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3682 | * if not known, block and switch to write-pending |
| 3683 | * If written to an active array that has pending writes, then fails. |
| 3684 | * active |
| 3685 | * fully active: IO and resync can be happening. |
| 3686 | * When written to inactive array, starts with resync |
| 3687 | * |
| 3688 | * write-pending |
| 3689 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3690 | * |
| 3691 | * active-idle |
| 3692 | * like active, but no writes have been seen for a while (100msec). |
| 3693 | * |
| 3694 | */ |
| 3695 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3696 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3697 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3698 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3699 | "write-pending", "active-idle", NULL }; |
| 3700 | |
| 3701 | static int match_word(const char *word, char **list) |
| 3702 | { |
| 3703 | int n; |
| 3704 | for (n=0; list[n]; n++) |
| 3705 | if (cmd_match(word, list[n])) |
| 3706 | break; |
| 3707 | return n; |
| 3708 | } |
| 3709 | |
| 3710 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3711 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3712 | { |
| 3713 | enum array_state st = inactive; |
| 3714 | |
| 3715 | if (mddev->pers) |
| 3716 | switch(mddev->ro) { |
| 3717 | case 1: |
| 3718 | st = readonly; |
| 3719 | break; |
| 3720 | case 2: |
| 3721 | st = read_auto; |
| 3722 | break; |
| 3723 | case 0: |
| 3724 | if (mddev->in_sync) |
| 3725 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3726 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3727 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3728 | else if (mddev->safemode) |
| 3729 | st = active_idle; |
| 3730 | else |
| 3731 | st = active; |
| 3732 | } |
| 3733 | else { |
| 3734 | if (list_empty(&mddev->disks) && |
| 3735 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3736 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3737 | st = clear; |
| 3738 | else |
| 3739 | st = inactive; |
| 3740 | } |
| 3741 | return sprintf(page, "%s\n", array_states[st]); |
| 3742 | } |
| 3743 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3745 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3746 | static int do_md_run(struct mddev * mddev); |
| 3747 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3748 | |
| 3749 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3750 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3751 | { |
| 3752 | int err = -EINVAL; |
| 3753 | enum array_state st = match_word(buf, array_states); |
| 3754 | switch(st) { |
| 3755 | case bad_word: |
| 3756 | break; |
| 3757 | case clear: |
| 3758 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3759 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3760 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3761 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3762 | break; |
| 3763 | case inactive: |
| 3764 | /* stopping an active array */ |
| 3765 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3766 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3767 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3768 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3769 | } else |
| 3770 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3771 | break; |
| 3772 | case suspended: |
| 3773 | break; /* not supported yet */ |
| 3774 | case readonly: |
| 3775 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3776 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3777 | else { |
| 3778 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3779 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3780 | err = do_md_run(mddev); |
| 3781 | } |
| 3782 | break; |
| 3783 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3784 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3785 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3786 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3787 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3788 | err = restart_array(mddev); |
| 3789 | if (err == 0) { |
| 3790 | mddev->ro = 2; |
| 3791 | set_disk_ro(mddev->gendisk, 0); |
| 3792 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3793 | } else { |
| 3794 | mddev->ro = 2; |
| 3795 | err = do_md_run(mddev); |
| 3796 | } |
| 3797 | break; |
| 3798 | case clean: |
| 3799 | if (mddev->pers) { |
| 3800 | restart_array(mddev); |
| 3801 | spin_lock_irq(&mddev->write_lock); |
| 3802 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3803 | if (mddev->in_sync == 0) { |
| 3804 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3805 | if (mddev->safemode == 1) |
| 3806 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3807 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3808 | } |
| 3809 | err = 0; |
| 3810 | } else |
| 3811 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3812 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3813 | } else |
| 3814 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3815 | break; |
| 3816 | case active: |
| 3817 | if (mddev->pers) { |
| 3818 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3819 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3820 | wake_up(&mddev->sb_wait); |
| 3821 | err = 0; |
| 3822 | } else { |
| 3823 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3824 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3825 | err = do_md_run(mddev); |
| 3826 | } |
| 3827 | break; |
| 3828 | case write_pending: |
| 3829 | case active_idle: |
| 3830 | /* these cannot be set */ |
| 3831 | break; |
| 3832 | } |
| 3833 | if (err) |
| 3834 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3835 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3836 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3837 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3838 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3839 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3840 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3841 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3842 | static struct md_sysfs_entry md_array_state = |
| 3843 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3844 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3845 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3846 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3847 | return sprintf(page, "%d\n", |
| 3848 | atomic_read(&mddev->max_corr_read_errors)); |
| 3849 | } |
| 3850 | |
| 3851 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3852 | 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] | 3853 | { |
| 3854 | char *e; |
| 3855 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3856 | |
| 3857 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3858 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3859 | return len; |
| 3860 | } |
| 3861 | return -EINVAL; |
| 3862 | } |
| 3863 | |
| 3864 | static struct md_sysfs_entry max_corr_read_errors = |
| 3865 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3866 | max_corrected_read_errors_store); |
| 3867 | |
| 3868 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3869 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3870 | { |
| 3871 | return -EINVAL; |
| 3872 | } |
| 3873 | |
| 3874 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3875 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3876 | { |
| 3877 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3878 | /* The new device is added to the array. |
| 3879 | * If the array has a persistent superblock, we read the |
| 3880 | * superblock to initialise info and check validity. |
| 3881 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3882 | * which mainly checks size. |
| 3883 | */ |
| 3884 | char *e; |
| 3885 | int major = simple_strtoul(buf, &e, 10); |
| 3886 | int minor; |
| 3887 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3888 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3889 | int err; |
| 3890 | |
| 3891 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3892 | return -EINVAL; |
| 3893 | minor = simple_strtoul(e+1, &e, 10); |
| 3894 | if (*e && *e != '\n') |
| 3895 | return -EINVAL; |
| 3896 | dev = MKDEV(major, minor); |
| 3897 | if (major != MAJOR(dev) || |
| 3898 | minor != MINOR(dev)) |
| 3899 | return -EOVERFLOW; |
| 3900 | |
| 3901 | |
| 3902 | if (mddev->persistent) { |
| 3903 | rdev = md_import_device(dev, mddev->major_version, |
| 3904 | mddev->minor_version); |
| 3905 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3906 | struct md_rdev *rdev0 |
| 3907 | = list_entry(mddev->disks.next, |
| 3908 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3909 | err = super_types[mddev->major_version] |
| 3910 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3911 | if (err < 0) |
| 3912 | goto out; |
| 3913 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3914 | } else if (mddev->external) |
| 3915 | rdev = md_import_device(dev, -2, -1); |
| 3916 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3917 | rdev = md_import_device(dev, -1, -1); |
| 3918 | |
| 3919 | if (IS_ERR(rdev)) |
| 3920 | return PTR_ERR(rdev); |
| 3921 | err = bind_rdev_to_array(rdev, mddev); |
| 3922 | out: |
| 3923 | if (err) |
| 3924 | export_rdev(rdev); |
| 3925 | return err ? err : len; |
| 3926 | } |
| 3927 | |
| 3928 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3929 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3930 | |
| 3931 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3932 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3933 | { |
| 3934 | char *end; |
| 3935 | unsigned long chunk, end_chunk; |
| 3936 | |
| 3937 | if (!mddev->bitmap) |
| 3938 | goto out; |
| 3939 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3940 | while (*buf) { |
| 3941 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3942 | if (buf == end) break; |
| 3943 | if (*end == '-') { /* range */ |
| 3944 | buf = end + 1; |
| 3945 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3946 | if (buf == end) break; |
| 3947 | } |
| 3948 | if (*end && !isspace(*end)) break; |
| 3949 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3950 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3951 | } |
| 3952 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3953 | out: |
| 3954 | return len; |
| 3955 | } |
| 3956 | |
| 3957 | static struct md_sysfs_entry md_bitmap = |
| 3958 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3959 | |
| 3960 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3961 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3962 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3963 | return sprintf(page, "%llu\n", |
| 3964 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3965 | } |
| 3966 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3967 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3968 | |
| 3969 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3970 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3971 | { |
| 3972 | /* If array is inactive, we can reduce the component size, but |
| 3973 | * not increase it (except from 0). |
| 3974 | * If array is active, we can try an on-line resize |
| 3975 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3976 | sector_t sectors; |
| 3977 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3978 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3979 | if (err < 0) |
| 3980 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3981 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3982 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3983 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3984 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3985 | if (mddev->dev_sectors == 0 || |
| 3986 | mddev->dev_sectors > sectors) |
| 3987 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3988 | else |
| 3989 | err = -ENOSPC; |
| 3990 | } |
| 3991 | return err ? err : len; |
| 3992 | } |
| 3993 | |
| 3994 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3995 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3996 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3997 | |
| 3998 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3999 | * This is one of |
| 4000 | * 'none' for arrays with no metadata (good luck...) |
| 4001 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4002 | * or N.M for internally known formats |
| 4003 | */ |
| 4004 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4005 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4006 | { |
| 4007 | if (mddev->persistent) |
| 4008 | return sprintf(page, "%d.%d\n", |
| 4009 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4010 | else if (mddev->external) |
| 4011 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4012 | else |
| 4013 | return sprintf(page, "none\n"); |
| 4014 | } |
| 4015 | |
| 4016 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4017 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4018 | { |
| 4019 | int major, minor; |
| 4020 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4021 | /* Changing the details of 'external' metadata is |
| 4022 | * always permitted. Otherwise there must be |
| 4023 | * no devices attached to the array. |
| 4024 | */ |
| 4025 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4026 | ; |
| 4027 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4028 | return -EBUSY; |
| 4029 | |
| 4030 | if (cmd_match(buf, "none")) { |
| 4031 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4032 | mddev->external = 0; |
| 4033 | mddev->major_version = 0; |
| 4034 | mddev->minor_version = 90; |
| 4035 | return len; |
| 4036 | } |
| 4037 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4038 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4039 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4040 | namelen = sizeof(mddev->metadata_type)-1; |
| 4041 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4042 | mddev->metadata_type[namelen] = 0; |
| 4043 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4044 | mddev->metadata_type[--namelen] = 0; |
| 4045 | mddev->persistent = 0; |
| 4046 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4047 | mddev->major_version = 0; |
| 4048 | mddev->minor_version = 90; |
| 4049 | return len; |
| 4050 | } |
| 4051 | major = simple_strtoul(buf, &e, 10); |
| 4052 | if (e==buf || *e != '.') |
| 4053 | return -EINVAL; |
| 4054 | buf = e+1; |
| 4055 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4056 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4057 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4058 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4059 | return -ENOENT; |
| 4060 | mddev->major_version = major; |
| 4061 | mddev->minor_version = minor; |
| 4062 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4063 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4064 | return len; |
| 4065 | } |
| 4066 | |
| 4067 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4068 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4069 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4070 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4071 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4072 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4073 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4074 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4075 | type = "frozen"; |
| 4076 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4077 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4078 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4079 | type = "reshape"; |
| 4080 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4081 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4082 | type = "resync"; |
| 4083 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4084 | type = "check"; |
| 4085 | else |
| 4086 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4087 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4088 | type = "recover"; |
| 4089 | } |
| 4090 | return sprintf(page, "%s\n", type); |
| 4091 | } |
| 4092 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4093 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4094 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4095 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4096 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4097 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4098 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4099 | return -EINVAL; |
| 4100 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4101 | if (cmd_match(page, "frozen")) |
| 4102 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4103 | else |
| 4104 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4105 | |
| 4106 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4107 | if (mddev->sync_thread) { |
| 4108 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4109 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4110 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4111 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4112 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4113 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4114 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4115 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4116 | else if (cmd_match(page, "recover")) { |
| 4117 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4118 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4119 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4120 | int err; |
| 4121 | if (mddev->pers->start_reshape == NULL) |
| 4122 | return -EINVAL; |
| 4123 | err = mddev->pers->start_reshape(mddev); |
| 4124 | if (err) |
| 4125 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4126 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4127 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4128 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4129 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4130 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4131 | return -EINVAL; |
| 4132 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4133 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4134 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4135 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4136 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4137 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4138 | return len; |
| 4139 | } |
| 4140 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4141 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4142 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4143 | { |
| 4144 | return sprintf(page, "%llu\n", |
| 4145 | (unsigned long long) mddev->resync_mismatches); |
| 4146 | } |
| 4147 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4148 | static struct md_sysfs_entry md_scan_mode = |
| 4149 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4150 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4151 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4152 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4153 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4154 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4155 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4156 | { |
| 4157 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4158 | mddev->sync_speed_min ? "local": "system"); |
| 4159 | } |
| 4160 | |
| 4161 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4162 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4163 | { |
| 4164 | int min; |
| 4165 | char *e; |
| 4166 | if (strncmp(buf, "system", 6)==0) { |
| 4167 | mddev->sync_speed_min = 0; |
| 4168 | return len; |
| 4169 | } |
| 4170 | min = simple_strtoul(buf, &e, 10); |
| 4171 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4172 | return -EINVAL; |
| 4173 | mddev->sync_speed_min = min; |
| 4174 | return len; |
| 4175 | } |
| 4176 | |
| 4177 | static struct md_sysfs_entry md_sync_min = |
| 4178 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4179 | |
| 4180 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4181 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4182 | { |
| 4183 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4184 | mddev->sync_speed_max ? "local": "system"); |
| 4185 | } |
| 4186 | |
| 4187 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4188 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4189 | { |
| 4190 | int max; |
| 4191 | char *e; |
| 4192 | if (strncmp(buf, "system", 6)==0) { |
| 4193 | mddev->sync_speed_max = 0; |
| 4194 | return len; |
| 4195 | } |
| 4196 | max = simple_strtoul(buf, &e, 10); |
| 4197 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4198 | return -EINVAL; |
| 4199 | mddev->sync_speed_max = max; |
| 4200 | return len; |
| 4201 | } |
| 4202 | |
| 4203 | static struct md_sysfs_entry md_sync_max = |
| 4204 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4205 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4206 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4207 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4208 | { |
| 4209 | return sprintf(page, "%d\n", mddev->degraded); |
| 4210 | } |
| 4211 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4212 | |
| 4213 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4214 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4215 | { |
| 4216 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4217 | } |
| 4218 | |
| 4219 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4220 | 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] | 4221 | { |
| 4222 | long n; |
| 4223 | |
| 4224 | if (strict_strtol(buf, 10, &n)) |
| 4225 | return -EINVAL; |
| 4226 | |
| 4227 | if (n != 0 && n != 1) |
| 4228 | return -EINVAL; |
| 4229 | |
| 4230 | mddev->parallel_resync = n; |
| 4231 | |
| 4232 | if (mddev->sync_thread) |
| 4233 | wake_up(&resync_wait); |
| 4234 | |
| 4235 | return len; |
| 4236 | } |
| 4237 | |
| 4238 | /* force parallel resync, even with shared block devices */ |
| 4239 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4240 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4241 | sync_force_parallel_show, sync_force_parallel_store); |
| 4242 | |
| 4243 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4244 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4245 | { |
| 4246 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4247 | if (mddev->curr_resync == 0) |
| 4248 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4249 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4250 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4251 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4252 | db = resync - mddev->resync_mark_cnt; |
| 4253 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4254 | } |
| 4255 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4256 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4257 | |
| 4258 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4259 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4260 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4261 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4262 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4263 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4264 | return sprintf(page, "none\n"); |
| 4265 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4266 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4267 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4268 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4269 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4270 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4271 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4272 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4273 | } |
| 4274 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4275 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4276 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4277 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4278 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4279 | { |
| 4280 | return sprintf(page, "%llu\n", |
| 4281 | (unsigned long long)mddev->resync_min); |
| 4282 | } |
| 4283 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4284 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4285 | { |
| 4286 | unsigned long long min; |
| 4287 | if (strict_strtoull(buf, 10, &min)) |
| 4288 | return -EINVAL; |
| 4289 | if (min > mddev->resync_max) |
| 4290 | return -EINVAL; |
| 4291 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4292 | return -EBUSY; |
| 4293 | |
| 4294 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4295 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4296 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4297 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4298 | return -EINVAL; |
| 4299 | } |
| 4300 | mddev->resync_min = min; |
| 4301 | |
| 4302 | return len; |
| 4303 | } |
| 4304 | |
| 4305 | static struct md_sysfs_entry md_min_sync = |
| 4306 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4307 | |
| 4308 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4309 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4310 | { |
| 4311 | if (mddev->resync_max == MaxSector) |
| 4312 | return sprintf(page, "max\n"); |
| 4313 | else |
| 4314 | return sprintf(page, "%llu\n", |
| 4315 | (unsigned long long)mddev->resync_max); |
| 4316 | } |
| 4317 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4318 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4319 | { |
| 4320 | if (strncmp(buf, "max", 3) == 0) |
| 4321 | mddev->resync_max = MaxSector; |
| 4322 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4323 | unsigned long long max; |
| 4324 | if (strict_strtoull(buf, 10, &max)) |
| 4325 | return -EINVAL; |
| 4326 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4327 | return -EINVAL; |
| 4328 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4329 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4330 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4331 | return -EBUSY; |
| 4332 | |
| 4333 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4334 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4335 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4336 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4337 | return -EINVAL; |
| 4338 | } |
| 4339 | mddev->resync_max = max; |
| 4340 | } |
| 4341 | wake_up(&mddev->recovery_wait); |
| 4342 | return len; |
| 4343 | } |
| 4344 | |
| 4345 | static struct md_sysfs_entry md_max_sync = |
| 4346 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4347 | |
| 4348 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4349 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4350 | { |
| 4351 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4352 | } |
| 4353 | |
| 4354 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4355 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4356 | { |
| 4357 | char *e; |
| 4358 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4359 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4360 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4361 | if (mddev->pers == NULL || |
| 4362 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4363 | return -EINVAL; |
| 4364 | if (buf == e || (*e && *e != '\n')) |
| 4365 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4366 | |
| 4367 | mddev->suspend_lo = new; |
| 4368 | if (new >= old) |
| 4369 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4370 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4371 | else { |
| 4372 | /* Expanding suspended region - need to wait */ |
| 4373 | mddev->pers->quiesce(mddev, 1); |
| 4374 | mddev->pers->quiesce(mddev, 0); |
| 4375 | } |
| 4376 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4377 | } |
| 4378 | static struct md_sysfs_entry md_suspend_lo = |
| 4379 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4380 | |
| 4381 | |
| 4382 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4383 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4384 | { |
| 4385 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4386 | } |
| 4387 | |
| 4388 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4389 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4390 | { |
| 4391 | char *e; |
| 4392 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4393 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4394 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4395 | if (mddev->pers == NULL || |
| 4396 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4397 | return -EINVAL; |
| 4398 | if (buf == e || (*e && *e != '\n')) |
| 4399 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4400 | |
| 4401 | mddev->suspend_hi = new; |
| 4402 | if (new <= old) |
| 4403 | /* Shrinking suspended region */ |
| 4404 | mddev->pers->quiesce(mddev, 2); |
| 4405 | else { |
| 4406 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4407 | mddev->pers->quiesce(mddev, 1); |
| 4408 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4409 | } |
| 4410 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4411 | } |
| 4412 | static struct md_sysfs_entry md_suspend_hi = |
| 4413 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4414 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4415 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4416 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4417 | { |
| 4418 | if (mddev->reshape_position != MaxSector) |
| 4419 | return sprintf(page, "%llu\n", |
| 4420 | (unsigned long long)mddev->reshape_position); |
| 4421 | strcpy(page, "none\n"); |
| 4422 | return 5; |
| 4423 | } |
| 4424 | |
| 4425 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4426 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4427 | { |
| 4428 | char *e; |
| 4429 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4430 | if (mddev->pers) |
| 4431 | return -EBUSY; |
| 4432 | if (buf == e || (*e && *e != '\n')) |
| 4433 | return -EINVAL; |
| 4434 | mddev->reshape_position = new; |
| 4435 | mddev->delta_disks = 0; |
| 4436 | mddev->new_level = mddev->level; |
| 4437 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4438 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4439 | return len; |
| 4440 | } |
| 4441 | |
| 4442 | static struct md_sysfs_entry md_reshape_position = |
| 4443 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4444 | reshape_position_store); |
| 4445 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4446 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4447 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4448 | { |
| 4449 | if (mddev->external_size) |
| 4450 | return sprintf(page, "%llu\n", |
| 4451 | (unsigned long long)mddev->array_sectors/2); |
| 4452 | else |
| 4453 | return sprintf(page, "default\n"); |
| 4454 | } |
| 4455 | |
| 4456 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4457 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4458 | { |
| 4459 | sector_t sectors; |
| 4460 | |
| 4461 | if (strncmp(buf, "default", 7) == 0) { |
| 4462 | if (mddev->pers) |
| 4463 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4464 | else |
| 4465 | sectors = mddev->array_sectors; |
| 4466 | |
| 4467 | mddev->external_size = 0; |
| 4468 | } else { |
| 4469 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4470 | return -EINVAL; |
| 4471 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4472 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4473 | |
| 4474 | mddev->external_size = 1; |
| 4475 | } |
| 4476 | |
| 4477 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4478 | if (mddev->pers) { |
| 4479 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4480 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4481 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4482 | return len; |
| 4483 | } |
| 4484 | |
| 4485 | static struct md_sysfs_entry md_array_size = |
| 4486 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4487 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4488 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4489 | static struct attribute *md_default_attrs[] = { |
| 4490 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4491 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4492 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4493 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4494 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4495 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4496 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4497 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4498 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4499 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4500 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4501 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4502 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4503 | NULL, |
| 4504 | }; |
| 4505 | |
| 4506 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4507 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4508 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4509 | &md_sync_min.attr, |
| 4510 | &md_sync_max.attr, |
| 4511 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4512 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4513 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4514 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4515 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4516 | &md_suspend_lo.attr, |
| 4517 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4518 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4519 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4520 | NULL, |
| 4521 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4522 | static struct attribute_group md_redundancy_group = { |
| 4523 | .name = NULL, |
| 4524 | .attrs = md_redundancy_attrs, |
| 4525 | }; |
| 4526 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4527 | |
| 4528 | static ssize_t |
| 4529 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4530 | { |
| 4531 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4532 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4533 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4534 | |
| 4535 | if (!entry->show) |
| 4536 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4537 | spin_lock(&all_mddevs_lock); |
| 4538 | if (list_empty(&mddev->all_mddevs)) { |
| 4539 | spin_unlock(&all_mddevs_lock); |
| 4540 | return -EBUSY; |
| 4541 | } |
| 4542 | mddev_get(mddev); |
| 4543 | spin_unlock(&all_mddevs_lock); |
| 4544 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4545 | rv = mddev_lock(mddev); |
| 4546 | if (!rv) { |
| 4547 | rv = entry->show(mddev, page); |
| 4548 | mddev_unlock(mddev); |
| 4549 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4550 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4551 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | static ssize_t |
| 4555 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4556 | const char *page, size_t length) |
| 4557 | { |
| 4558 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4559 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4560 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4561 | |
| 4562 | if (!entry->store) |
| 4563 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4564 | if (!capable(CAP_SYS_ADMIN)) |
| 4565 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4566 | spin_lock(&all_mddevs_lock); |
| 4567 | if (list_empty(&mddev->all_mddevs)) { |
| 4568 | spin_unlock(&all_mddevs_lock); |
| 4569 | return -EBUSY; |
| 4570 | } |
| 4571 | mddev_get(mddev); |
| 4572 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4573 | rv = mddev_lock(mddev); |
| 4574 | if (!rv) { |
| 4575 | rv = entry->store(mddev, page, length); |
| 4576 | mddev_unlock(mddev); |
| 4577 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4578 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4579 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | static void md_free(struct kobject *ko) |
| 4583 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4584 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4585 | |
| 4586 | if (mddev->sysfs_state) |
| 4587 | sysfs_put(mddev->sysfs_state); |
| 4588 | |
| 4589 | if (mddev->gendisk) { |
| 4590 | del_gendisk(mddev->gendisk); |
| 4591 | put_disk(mddev->gendisk); |
| 4592 | } |
| 4593 | if (mddev->queue) |
| 4594 | blk_cleanup_queue(mddev->queue); |
| 4595 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4596 | kfree(mddev); |
| 4597 | } |
| 4598 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4599 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4600 | .show = md_attr_show, |
| 4601 | .store = md_attr_store, |
| 4602 | }; |
| 4603 | static struct kobj_type md_ktype = { |
| 4604 | .release = md_free, |
| 4605 | .sysfs_ops = &md_sysfs_ops, |
| 4606 | .default_attrs = md_default_attrs, |
| 4607 | }; |
| 4608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | int mdp_major = 0; |
| 4610 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4611 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4612 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4613 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4614 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4615 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4616 | kobject_del(&mddev->kobj); |
| 4617 | kobject_put(&mddev->kobj); |
| 4618 | } |
| 4619 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4620 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4622 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4623 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4625 | int partitioned; |
| 4626 | int shift; |
| 4627 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4628 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | |
| 4630 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4631 | return -ENODEV; |
| 4632 | |
| 4633 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4634 | shift = partitioned ? MdpMinorShift : 0; |
| 4635 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4636 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4637 | /* wait for any previous instance of this device to be |
| 4638 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4639 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4640 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4641 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4642 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4643 | error = -EEXIST; |
| 4644 | if (mddev->gendisk) |
| 4645 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4646 | |
| 4647 | if (name) { |
| 4648 | /* Need to ensure that 'name' is not a duplicate. |
| 4649 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4650 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4651 | spin_lock(&all_mddevs_lock); |
| 4652 | |
| 4653 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4654 | if (mddev2->gendisk && |
| 4655 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4656 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4657 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4658 | } |
| 4659 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4661 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4662 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4663 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4664 | if (!mddev->queue) |
| 4665 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4666 | mddev->queue->queuedata = mddev; |
| 4667 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4668 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4669 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | disk = alloc_disk(1 << shift); |
| 4672 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4673 | blk_cleanup_queue(mddev->queue); |
| 4674 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4675 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4677 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4679 | if (name) |
| 4680 | strcpy(disk->disk_name, name); |
| 4681 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4683 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | disk->fops = &md_fops; |
| 4686 | disk->private_data = mddev; |
| 4687 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4688 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4689 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4690 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4691 | * remove it now. |
| 4692 | */ |
| 4693 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4695 | /* As soon as we call add_disk(), another thread could get |
| 4696 | * through to md_open, so make sure it doesn't get too far |
| 4697 | */ |
| 4698 | mutex_lock(&mddev->open_mutex); |
| 4699 | add_disk(disk); |
| 4700 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4701 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4702 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4703 | if (error) { |
| 4704 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4705 | * __must_check, we must do something with the result |
| 4706 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4707 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4708 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4709 | error = 0; |
| 4710 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4711 | if (mddev->kobj.sd && |
| 4712 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4713 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4714 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4715 | abort: |
| 4716 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4717 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4718 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4719 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4720 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4721 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4722 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4726 | { |
| 4727 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | return NULL; |
| 4729 | } |
| 4730 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4731 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4732 | { |
| 4733 | /* val must be "md_*" where * is not all digits. |
| 4734 | * We allocate an array with a large free minor number, and |
| 4735 | * set the name to val. val must not already be an active name. |
| 4736 | */ |
| 4737 | int len = strlen(val); |
| 4738 | char buf[DISK_NAME_LEN]; |
| 4739 | |
| 4740 | while (len && val[len-1] == '\n') |
| 4741 | len--; |
| 4742 | if (len >= DISK_NAME_LEN) |
| 4743 | return -E2BIG; |
| 4744 | strlcpy(buf, val, len+1); |
| 4745 | if (strncmp(buf, "md_", 3) != 0) |
| 4746 | return -EINVAL; |
| 4747 | return md_alloc(0, buf); |
| 4748 | } |
| 4749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | static void md_safemode_timeout(unsigned long data) |
| 4751 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4752 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4754 | if (!atomic_read(&mddev->writes_pending)) { |
| 4755 | mddev->safemode = 1; |
| 4756 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4757 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | md_wakeup_thread(mddev->thread); |
| 4760 | } |
| 4761 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4762 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4764 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4766 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4767 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4768 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4770 | if (list_empty(&mddev->disks)) |
| 4771 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | |
| 4774 | if (mddev->pers) |
| 4775 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4776 | /* Cannot run until previous stop completes properly */ |
| 4777 | if (mddev->sysfs_active) |
| 4778 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | /* |
| 4781 | * Analyze all RAID superblock(s) |
| 4782 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4783 | if (!mddev->raid_disks) { |
| 4784 | if (!mddev->persistent) |
| 4785 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4786 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4789 | if (mddev->level != LEVEL_NONE) |
| 4790 | request_module("md-level-%d", mddev->level); |
| 4791 | else if (mddev->clevel[0]) |
| 4792 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | |
| 4794 | /* |
| 4795 | * Drop all container device buffers, from now on |
| 4796 | * the only valid external interface is through the md |
| 4797 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4799 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4800 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | continue; |
| 4802 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4803 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4804 | |
| 4805 | /* perform some consistency tests on the device. |
| 4806 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4807 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4808 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4809 | if (rdev->meta_bdev) { |
| 4810 | /* Nothing to check */; |
| 4811 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4812 | if (mddev->dev_sectors && |
| 4813 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4814 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4815 | printk("md: %s: data overlaps metadata\n", |
| 4816 | mdname(mddev)); |
| 4817 | return -EINVAL; |
| 4818 | } |
| 4819 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4820 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4821 | > rdev->data_offset) { |
| 4822 | printk("md: %s: metadata overlaps data\n", |
| 4823 | mdname(mddev)); |
| 4824 | return -EINVAL; |
| 4825 | } |
| 4826 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4827 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | } |
| 4829 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4830 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4831 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4832 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4835 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4836 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4838 | if (mddev->level != LEVEL_NONE) |
| 4839 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4840 | mddev->level); |
| 4841 | else |
| 4842 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4843 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | return -EINVAL; |
| 4845 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4846 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4848 | if (mddev->level != pers->level) { |
| 4849 | mddev->level = pers->level; |
| 4850 | mddev->new_level = pers->level; |
| 4851 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4852 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4854 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4855 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4856 | /* This personality cannot handle reshaping... */ |
| 4857 | mddev->pers = NULL; |
| 4858 | module_put(pers->owner); |
| 4859 | return -EINVAL; |
| 4860 | } |
| 4861 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4862 | if (pers->sync_request) { |
| 4863 | /* Warn if this is a potentially silly |
| 4864 | * configuration. |
| 4865 | */ |
| 4866 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4867 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4868 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4869 | |
| 4870 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4871 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4872 | if (rdev < rdev2 && |
| 4873 | rdev->bdev->bd_contains == |
| 4874 | rdev2->bdev->bd_contains) { |
| 4875 | printk(KERN_WARNING |
| 4876 | "%s: WARNING: %s appears to be" |
| 4877 | " on the same physical disk as" |
| 4878 | " %s.\n", |
| 4879 | mdname(mddev), |
| 4880 | bdevname(rdev->bdev,b), |
| 4881 | bdevname(rdev2->bdev,b2)); |
| 4882 | warned = 1; |
| 4883 | } |
| 4884 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4885 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4886 | if (warned) |
| 4887 | printk(KERN_WARNING |
| 4888 | "True protection against single-disk" |
| 4889 | " failure might be compromised.\n"); |
| 4890 | } |
| 4891 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4892 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4893 | /* may be over-ridden by personality */ |
| 4894 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4895 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4896 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4898 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4899 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4900 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4901 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4902 | if (err) |
| 4903 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4904 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4905 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4906 | " but 'external_size' not in effect?\n", __func__); |
| 4907 | printk(KERN_ERR |
| 4908 | "md: invalid array_size %llu > default size %llu\n", |
| 4909 | (unsigned long long)mddev->array_sectors / 2, |
| 4910 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4911 | err = -EINVAL; |
| 4912 | mddev->pers->stop(mddev); |
| 4913 | } |
| 4914 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4915 | err = bitmap_create(mddev); |
| 4916 | if (err) { |
| 4917 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4918 | mdname(mddev), err); |
| 4919 | mddev->pers->stop(mddev); |
| 4920 | } |
| 4921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | module_put(mddev->pers->owner); |
| 4924 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4925 | bitmap_destroy(mddev); |
| 4926 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4928 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4929 | if (mddev->kobj.sd && |
| 4930 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4931 | printk(KERN_WARNING |
| 4932 | "md: cannot register extra attributes for %s\n", |
| 4933 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4934 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4935 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4936 | mddev->ro = 0; |
| 4937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4939 | atomic_set(&mddev->max_corr_read_errors, |
| 4940 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | mddev->safemode = 0; |
| 4942 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4943 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4944 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4946 | smp_wmb(); |
| 4947 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4948 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4949 | if (rdev->raid_disk >= 0) |
| 4950 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4951 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | |
| 4953 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4954 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4955 | if (mddev->flags) |
| 4956 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4958 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4959 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4960 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4961 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | return 0; |
| 4963 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4964 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4966 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4967 | { |
| 4968 | int err; |
| 4969 | |
| 4970 | err = md_run(mddev); |
| 4971 | if (err) |
| 4972 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4973 | err = bitmap_load(mddev); |
| 4974 | if (err) { |
| 4975 | bitmap_destroy(mddev); |
| 4976 | goto out; |
| 4977 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4978 | |
| 4979 | md_wakeup_thread(mddev->thread); |
| 4980 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4981 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4982 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4983 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4984 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4985 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4986 | out: |
| 4987 | return err; |
| 4988 | } |
| 4989 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4990 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | { |
| 4992 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4994 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4996 | return -ENXIO; |
| 4997 | if (!mddev->pers) |
| 4998 | return -EINVAL; |
| 4999 | if (!mddev->ro) |
| 5000 | return -EBUSY; |
| 5001 | mddev->safemode = 0; |
| 5002 | mddev->ro = 0; |
| 5003 | set_disk_ro(disk, 0); |
| 5004 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5005 | mdname(mddev)); |
| 5006 | /* Kick recovery or resync if necessary */ |
| 5007 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5008 | md_wakeup_thread(mddev->thread); |
| 5009 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5010 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5011 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | } |
| 5013 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5014 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5015 | * to the file ourselves */ |
| 5016 | static int deny_bitmap_write_access(struct file * file) |
| 5017 | { |
| 5018 | struct inode *inode = file->f_mapping->host; |
| 5019 | |
| 5020 | spin_lock(&inode->i_lock); |
| 5021 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5022 | spin_unlock(&inode->i_lock); |
| 5023 | return -ETXTBSY; |
| 5024 | } |
| 5025 | atomic_set(&inode->i_writecount, -1); |
| 5026 | spin_unlock(&inode->i_lock); |
| 5027 | |
| 5028 | return 0; |
| 5029 | } |
| 5030 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5031 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5032 | { |
| 5033 | struct inode *inode = file->f_mapping->host; |
| 5034 | |
| 5035 | spin_lock(&inode->i_lock); |
| 5036 | atomic_set(&inode->i_writecount, 1); |
| 5037 | spin_unlock(&inode->i_lock); |
| 5038 | } |
| 5039 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5040 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5041 | { |
| 5042 | mddev->array_sectors = 0; |
| 5043 | mddev->external_size = 0; |
| 5044 | mddev->dev_sectors = 0; |
| 5045 | mddev->raid_disks = 0; |
| 5046 | mddev->recovery_cp = 0; |
| 5047 | mddev->resync_min = 0; |
| 5048 | mddev->resync_max = MaxSector; |
| 5049 | mddev->reshape_position = MaxSector; |
| 5050 | mddev->external = 0; |
| 5051 | mddev->persistent = 0; |
| 5052 | mddev->level = LEVEL_NONE; |
| 5053 | mddev->clevel[0] = 0; |
| 5054 | mddev->flags = 0; |
| 5055 | mddev->ro = 0; |
| 5056 | mddev->metadata_type[0] = 0; |
| 5057 | mddev->chunk_sectors = 0; |
| 5058 | mddev->ctime = mddev->utime = 0; |
| 5059 | mddev->layout = 0; |
| 5060 | mddev->max_disks = 0; |
| 5061 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5062 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5063 | mddev->delta_disks = 0; |
| 5064 | mddev->new_level = LEVEL_NONE; |
| 5065 | mddev->new_layout = 0; |
| 5066 | mddev->new_chunk_sectors = 0; |
| 5067 | mddev->curr_resync = 0; |
| 5068 | mddev->resync_mismatches = 0; |
| 5069 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5070 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5071 | mddev->recovery = 0; |
| 5072 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5073 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5074 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5075 | mddev->safemode = 0; |
| 5076 | mddev->bitmap_info.offset = 0; |
| 5077 | mddev->bitmap_info.default_offset = 0; |
| 5078 | mddev->bitmap_info.chunksize = 0; |
| 5079 | mddev->bitmap_info.daemon_sleep = 0; |
| 5080 | mddev->bitmap_info.max_write_behind = 0; |
| 5081 | } |
| 5082 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5083 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5084 | { |
| 5085 | if (mddev->sync_thread) { |
| 5086 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5087 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5088 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5089 | } |
| 5090 | |
| 5091 | del_timer_sync(&mddev->safemode_timer); |
| 5092 | |
| 5093 | bitmap_flush(mddev); |
| 5094 | md_super_wait(mddev); |
| 5095 | |
| 5096 | if (!mddev->in_sync || mddev->flags) { |
| 5097 | /* mark array as shutdown cleanly */ |
| 5098 | mddev->in_sync = 1; |
| 5099 | md_update_sb(mddev, 1); |
| 5100 | } |
| 5101 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5102 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5103 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5104 | { |
| 5105 | mddev_lock(mddev); |
| 5106 | __md_stop_writes(mddev); |
| 5107 | mddev_unlock(mddev); |
| 5108 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5109 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5110 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5111 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5112 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5113 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5114 | mddev->pers->stop(mddev); |
| 5115 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5116 | mddev->to_remove = &md_redundancy_group; |
| 5117 | module_put(mddev->pers->owner); |
| 5118 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5119 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5120 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5121 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5123 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5124 | { |
| 5125 | int err = 0; |
| 5126 | mutex_lock(&mddev->open_mutex); |
| 5127 | if (atomic_read(&mddev->openers) > is_open) { |
| 5128 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5129 | err = -EBUSY; |
| 5130 | goto out; |
| 5131 | } |
| 5132 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5133 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5134 | |
| 5135 | err = -ENXIO; |
| 5136 | if (mddev->ro==1) |
| 5137 | goto out; |
| 5138 | mddev->ro = 1; |
| 5139 | set_disk_ro(mddev->gendisk, 1); |
| 5140 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5141 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5142 | err = 0; |
| 5143 | } |
| 5144 | out: |
| 5145 | mutex_unlock(&mddev->open_mutex); |
| 5146 | return err; |
| 5147 | } |
| 5148 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5149 | /* mode: |
| 5150 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5151 | * 2 - stop but do not disassemble array |
| 5152 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5153 | 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] | 5154 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5156 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5158 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5159 | if (atomic_read(&mddev->openers) > is_open || |
| 5160 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5161 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5162 | mutex_unlock(&mddev->open_mutex); |
| 5163 | return -EBUSY; |
| 5164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5166 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5167 | if (mddev->ro) |
| 5168 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5169 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5170 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5171 | md_stop(mddev); |
| 5172 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5173 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5174 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5175 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5176 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5177 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5178 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5179 | if (rdev->raid_disk >= 0) |
| 5180 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5181 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5182 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5183 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5184 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5185 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5186 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5187 | if (mddev->ro) |
| 5188 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5189 | } else |
| 5190 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | /* |
| 5192 | * Free resources if final stop |
| 5193 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5194 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5196 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5197 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5198 | if (mddev->bitmap_info.file) { |
| 5199 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5200 | fput(mddev->bitmap_info.file); |
| 5201 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5202 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5203 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | export_array(mddev); |
| 5206 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5207 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5208 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5209 | if (mddev->hold_active == UNTIL_STOP) |
| 5210 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5211 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5212 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5213 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5214 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5215 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | } |
| 5217 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5218 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5219 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5221 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | int err; |
| 5223 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5224 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | |
| 5227 | printk(KERN_INFO "md: running: "); |
| 5228 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5229 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | char b[BDEVNAME_SIZE]; |
| 5231 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5232 | } |
| 5233 | printk("\n"); |
| 5234 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5235 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | if (err) { |
| 5237 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5238 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * lets try to run arrays based on all disks that have arrived |
| 5244 | * until now. (those are in pending_raid_disks) |
| 5245 | * |
| 5246 | * the method: pick the first pending disk, collect all disks with |
| 5247 | * the same UUID, remove all from the pending list and put them into |
| 5248 | * the 'same_array' list. Then order this list based on superblock |
| 5249 | * update time (freshest comes first), kick out 'old' disks and |
| 5250 | * compare superblocks. If everything's fine then run it. |
| 5251 | * |
| 5252 | * If "unit" is allocated, then bump its reference count |
| 5253 | */ |
| 5254 | static void autorun_devices(int part) |
| 5255 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5256 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5257 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | char b[BDEVNAME_SIZE]; |
| 5259 | |
| 5260 | printk(KERN_INFO "md: autorun ...\n"); |
| 5261 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5262 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5264 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5266 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | |
| 5268 | printk(KERN_INFO "md: considering %s ...\n", |
| 5269 | bdevname(rdev0->bdev,b)); |
| 5270 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5271 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5273 | printk(KERN_INFO "md: adding %s ...\n", |
| 5274 | bdevname(rdev->bdev,b)); |
| 5275 | list_move(&rdev->same_set, &candidates); |
| 5276 | } |
| 5277 | /* |
| 5278 | * now we have a set of devices, with all of them having |
| 5279 | * mostly sane superblocks. It's time to allocate the |
| 5280 | * mddev. |
| 5281 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5282 | if (part) { |
| 5283 | dev = MKDEV(mdp_major, |
| 5284 | rdev0->preferred_minor << MdpMinorShift); |
| 5285 | unit = MINOR(dev) >> MdpMinorShift; |
| 5286 | } else { |
| 5287 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5288 | unit = MINOR(dev); |
| 5289 | } |
| 5290 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5292 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5293 | break; |
| 5294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
| 5296 | md_probe(dev, NULL, NULL); |
| 5297 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5298 | if (!mddev || !mddev->gendisk) { |
| 5299 | if (mddev) |
| 5300 | mddev_put(mddev); |
| 5301 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | "md: cannot allocate memory for md drive.\n"); |
| 5303 | break; |
| 5304 | } |
| 5305 | if (mddev_lock(mddev)) |
| 5306 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5307 | mdname(mddev)); |
| 5308 | else if (mddev->raid_disks || mddev->major_version |
| 5309 | || !list_empty(&mddev->disks)) { |
| 5310 | printk(KERN_WARNING |
| 5311 | "md: %s already running, cannot run %s\n", |
| 5312 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5313 | mddev_unlock(mddev); |
| 5314 | } else { |
| 5315 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5316 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5317 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | list_del_init(&rdev->same_set); |
| 5319 | if (bind_rdev_to_array(rdev, mddev)) |
| 5320 | export_rdev(rdev); |
| 5321 | } |
| 5322 | autorun_array(mddev); |
| 5323 | mddev_unlock(mddev); |
| 5324 | } |
| 5325 | /* on success, candidates will be empty, on error |
| 5326 | * it won't... |
| 5327 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5328 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5329 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | mddev_put(mddev); |
| 5333 | } |
| 5334 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5335 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5336 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | static int get_version(void __user * arg) |
| 5339 | { |
| 5340 | mdu_version_t ver; |
| 5341 | |
| 5342 | ver.major = MD_MAJOR_VERSION; |
| 5343 | ver.minor = MD_MINOR_VERSION; |
| 5344 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5345 | |
| 5346 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5347 | return -EFAULT; |
| 5348 | |
| 5349 | return 0; |
| 5350 | } |
| 5351 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5352 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | { |
| 5354 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5355 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5356 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5358 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5359 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5361 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | failed++; |
| 5363 | else { |
| 5364 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5365 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5366 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | else |
| 5368 | spare++; |
| 5369 | } |
| 5370 | } |
| 5371 | |
| 5372 | info.major_version = mddev->major_version; |
| 5373 | info.minor_version = mddev->minor_version; |
| 5374 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5375 | info.ctime = mddev->ctime; |
| 5376 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5377 | info.size = mddev->dev_sectors / 2; |
| 5378 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5379 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | info.nr_disks = nr; |
| 5381 | info.raid_disks = mddev->raid_disks; |
| 5382 | info.md_minor = mddev->md_minor; |
| 5383 | info.not_persistent= !mddev->persistent; |
| 5384 | |
| 5385 | info.utime = mddev->utime; |
| 5386 | info.state = 0; |
| 5387 | if (mddev->in_sync) |
| 5388 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5389 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5391 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | info.working_disks = working; |
| 5393 | info.failed_disks = failed; |
| 5394 | info.spare_disks = spare; |
| 5395 | |
| 5396 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5397 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | |
| 5399 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5400 | return -EFAULT; |
| 5401 | |
| 5402 | return 0; |
| 5403 | } |
| 5404 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5405 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5406 | { |
| 5407 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5408 | char *ptr, *buf = NULL; |
| 5409 | int err = -ENOMEM; |
| 5410 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5411 | if (md_allow_write(mddev)) |
| 5412 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5413 | else |
| 5414 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5415 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5416 | if (!file) |
| 5417 | goto out; |
| 5418 | |
| 5419 | /* bitmap disabled, zero the first byte and copy out */ |
| 5420 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5421 | file->pathname[0] = '\0'; |
| 5422 | goto copy_out; |
| 5423 | } |
| 5424 | |
| 5425 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5426 | if (!buf) |
| 5427 | goto out; |
| 5428 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5429 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5430 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5431 | goto out; |
| 5432 | |
| 5433 | strcpy(file->pathname, ptr); |
| 5434 | |
| 5435 | copy_out: |
| 5436 | err = 0; |
| 5437 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5438 | err = -EFAULT; |
| 5439 | out: |
| 5440 | kfree(buf); |
| 5441 | kfree(file); |
| 5442 | return err; |
| 5443 | } |
| 5444 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5445 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | { |
| 5447 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5448 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | |
| 5450 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5451 | return -EFAULT; |
| 5452 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5453 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | if (rdev) { |
| 5455 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5456 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5457 | info.raid_disk = rdev->raid_disk; |
| 5458 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5459 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5461 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5463 | info.state |= (1<<MD_DISK_SYNC); |
| 5464 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5465 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5466 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | } else { |
| 5468 | info.major = info.minor = 0; |
| 5469 | info.raid_disk = -1; |
| 5470 | info.state = (1<<MD_DISK_REMOVED); |
| 5471 | } |
| 5472 | |
| 5473 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5474 | return -EFAULT; |
| 5475 | |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5479 | 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] | 5480 | { |
| 5481 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5482 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | dev_t dev = MKDEV(info->major,info->minor); |
| 5484 | |
| 5485 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5486 | return -EOVERFLOW; |
| 5487 | |
| 5488 | if (!mddev->raid_disks) { |
| 5489 | int err; |
| 5490 | /* expecting a device which has a superblock */ |
| 5491 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5492 | if (IS_ERR(rdev)) { |
| 5493 | printk(KERN_WARNING |
| 5494 | "md: md_import_device returned %ld\n", |
| 5495 | PTR_ERR(rdev)); |
| 5496 | return PTR_ERR(rdev); |
| 5497 | } |
| 5498 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5499 | struct md_rdev *rdev0 |
| 5500 | = list_entry(mddev->disks.next, |
| 5501 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5502 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5504 | if (err < 0) { |
| 5505 | printk(KERN_WARNING |
| 5506 | "md: %s has different UUID to %s\n", |
| 5507 | bdevname(rdev->bdev,b), |
| 5508 | bdevname(rdev0->bdev,b2)); |
| 5509 | export_rdev(rdev); |
| 5510 | return -EINVAL; |
| 5511 | } |
| 5512 | } |
| 5513 | err = bind_rdev_to_array(rdev, mddev); |
| 5514 | if (err) |
| 5515 | export_rdev(rdev); |
| 5516 | return err; |
| 5517 | } |
| 5518 | |
| 5519 | /* |
| 5520 | * add_new_disk can be used once the array is assembled |
| 5521 | * to add "hot spares". They must already have a superblock |
| 5522 | * written |
| 5523 | */ |
| 5524 | if (mddev->pers) { |
| 5525 | int err; |
| 5526 | if (!mddev->pers->hot_add_disk) { |
| 5527 | printk(KERN_WARNING |
| 5528 | "%s: personality does not support diskops!\n", |
| 5529 | mdname(mddev)); |
| 5530 | return -EINVAL; |
| 5531 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5532 | if (mddev->persistent) |
| 5533 | rdev = md_import_device(dev, mddev->major_version, |
| 5534 | mddev->minor_version); |
| 5535 | else |
| 5536 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | if (IS_ERR(rdev)) { |
| 5538 | printk(KERN_WARNING |
| 5539 | "md: md_import_device returned %ld\n", |
| 5540 | PTR_ERR(rdev)); |
| 5541 | return PTR_ERR(rdev); |
| 5542 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5543 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5544 | if (!mddev->persistent) { |
| 5545 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5546 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5547 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5548 | set_bit(In_sync, &rdev->flags); |
| 5549 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5550 | rdev->raid_disk = -1; |
| 5551 | } else |
| 5552 | super_types[mddev->major_version]. |
| 5553 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5554 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5555 | (!test_bit(In_sync, &rdev->flags) || |
| 5556 | rdev->raid_disk != info->raid_disk)) { |
| 5557 | /* This was a hot-add request, but events doesn't |
| 5558 | * match, so reject it. |
| 5559 | */ |
| 5560 | export_rdev(rdev); |
| 5561 | return -EINVAL; |
| 5562 | } |
| 5563 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5564 | if (test_bit(In_sync, &rdev->flags)) |
| 5565 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5566 | else |
| 5567 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5568 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5569 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5570 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5571 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5572 | else |
| 5573 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | rdev->raid_disk = -1; |
| 5576 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5577 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5578 | /* If there is hot_add_disk but no hot_remove_disk |
| 5579 | * then added disks for geometry changes, |
| 5580 | * and should be added immediately. |
| 5581 | */ |
| 5582 | super_types[mddev->major_version]. |
| 5583 | validate_super(mddev, rdev); |
| 5584 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5585 | if (err) |
| 5586 | unbind_rdev_from_array(rdev); |
| 5587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | if (err) |
| 5589 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5590 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5591 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5592 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5593 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5594 | if (mddev->degraded) |
| 5595 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5596 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5597 | if (!err) |
| 5598 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5599 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | return err; |
| 5601 | } |
| 5602 | |
| 5603 | /* otherwise, add_new_disk is only allowed |
| 5604 | * for major_version==0 superblocks |
| 5605 | */ |
| 5606 | if (mddev->major_version != 0) { |
| 5607 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5608 | mdname(mddev)); |
| 5609 | return -EINVAL; |
| 5610 | } |
| 5611 | |
| 5612 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5613 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5614 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | if (IS_ERR(rdev)) { |
| 5616 | printk(KERN_WARNING |
| 5617 | "md: error, md_import_device() returned %ld\n", |
| 5618 | PTR_ERR(rdev)); |
| 5619 | return PTR_ERR(rdev); |
| 5620 | } |
| 5621 | rdev->desc_nr = info->number; |
| 5622 | if (info->raid_disk < mddev->raid_disks) |
| 5623 | rdev->raid_disk = info->raid_disk; |
| 5624 | else |
| 5625 | rdev->raid_disk = -1; |
| 5626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5628 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5629 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5631 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5632 | set_bit(WriteMostly, &rdev->flags); |
| 5633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | if (!mddev->persistent) { |
| 5635 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5636 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5637 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5638 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5639 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5641 | err = bind_rdev_to_array(rdev, mddev); |
| 5642 | if (err) { |
| 5643 | export_rdev(rdev); |
| 5644 | return err; |
| 5645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | return 0; |
| 5649 | } |
| 5650 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5651 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | { |
| 5653 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5654 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | rdev = find_rdev(mddev, dev); |
| 5657 | if (!rdev) |
| 5658 | return -ENXIO; |
| 5659 | |
| 5660 | if (rdev->raid_disk >= 0) |
| 5661 | goto busy; |
| 5662 | |
| 5663 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5664 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5665 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | |
| 5667 | return 0; |
| 5668 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5669 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5671 | return -EBUSY; |
| 5672 | } |
| 5673 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5674 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | { |
| 5676 | char b[BDEVNAME_SIZE]; |
| 5677 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5678 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | |
| 5680 | if (!mddev->pers) |
| 5681 | return -ENODEV; |
| 5682 | |
| 5683 | if (mddev->major_version != 0) { |
| 5684 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5685 | " version-0 superblocks.\n", |
| 5686 | mdname(mddev)); |
| 5687 | return -EINVAL; |
| 5688 | } |
| 5689 | if (!mddev->pers->hot_add_disk) { |
| 5690 | printk(KERN_WARNING |
| 5691 | "%s: personality does not support diskops!\n", |
| 5692 | mdname(mddev)); |
| 5693 | return -EINVAL; |
| 5694 | } |
| 5695 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5696 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | if (IS_ERR(rdev)) { |
| 5698 | printk(KERN_WARNING |
| 5699 | "md: error, md_import_device() returned %ld\n", |
| 5700 | PTR_ERR(rdev)); |
| 5701 | return -EINVAL; |
| 5702 | } |
| 5703 | |
| 5704 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5705 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5707 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5709 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5711 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | printk(KERN_WARNING |
| 5713 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5714 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5715 | err = -EINVAL; |
| 5716 | goto abort_export; |
| 5717 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5718 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5720 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5721 | err = bind_rdev_to_array(rdev, mddev); |
| 5722 | if (err) |
| 5723 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | |
| 5725 | /* |
| 5726 | * The rest should better be atomic, we can have disk failures |
| 5727 | * noticed in interrupt contexts ... |
| 5728 | */ |
| 5729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | rdev->raid_disk = -1; |
| 5731 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5732 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | |
| 5734 | /* |
| 5735 | * Kick recovery, maybe this spare has to be added to the |
| 5736 | * array immediately. |
| 5737 | */ |
| 5738 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5739 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5740 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | return 0; |
| 5742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | abort_export: |
| 5744 | export_rdev(rdev); |
| 5745 | return err; |
| 5746 | } |
| 5747 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5748 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5749 | { |
| 5750 | int err; |
| 5751 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5752 | if (mddev->pers) { |
| 5753 | if (!mddev->pers->quiesce) |
| 5754 | return -EBUSY; |
| 5755 | if (mddev->recovery || mddev->sync_thread) |
| 5756 | return -EBUSY; |
| 5757 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5758 | } |
| 5759 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5760 | |
| 5761 | if (fd >= 0) { |
| 5762 | if (mddev->bitmap) |
| 5763 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5764 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5765 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5766 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5767 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5768 | mdname(mddev)); |
| 5769 | return -EBADF; |
| 5770 | } |
| 5771 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5772 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5773 | if (err) { |
| 5774 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5775 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5776 | fput(mddev->bitmap_info.file); |
| 5777 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5778 | return err; |
| 5779 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5780 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5781 | } else if (mddev->bitmap == NULL) |
| 5782 | return -ENOENT; /* cannot remove what isn't there */ |
| 5783 | err = 0; |
| 5784 | if (mddev->pers) { |
| 5785 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5786 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5787 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5788 | if (!err) |
| 5789 | err = bitmap_load(mddev); |
| 5790 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5791 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5792 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5793 | fd = -1; /* make sure to put the file */ |
| 5794 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5795 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5796 | } |
| 5797 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5798 | if (mddev->bitmap_info.file) { |
| 5799 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5800 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5801 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5802 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5803 | } |
| 5804 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5805 | return err; |
| 5806 | } |
| 5807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | /* |
| 5809 | * set_array_info is used two different ways |
| 5810 | * The original usage is when creating a new array. |
| 5811 | * In this usage, raid_disks is > 0 and it together with |
| 5812 | * level, size, not_persistent,layout,chunksize determine the |
| 5813 | * shape of the array. |
| 5814 | * This will always create an array with a type-0.90.0 superblock. |
| 5815 | * The newer usage is when assembling an array. |
| 5816 | * In this case raid_disks will be 0, and the major_version field is |
| 5817 | * use to determine which style super-blocks are to be found on the devices. |
| 5818 | * The minor and patch _version numbers are also kept incase the |
| 5819 | * super_block handler wishes to interpret them. |
| 5820 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5821 | 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] | 5822 | { |
| 5823 | |
| 5824 | if (info->raid_disks == 0) { |
| 5825 | /* just setting version number for superblock loading */ |
| 5826 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5827 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | super_types[info->major_version].name == NULL) { |
| 5829 | /* maybe try to auto-load a module? */ |
| 5830 | printk(KERN_INFO |
| 5831 | "md: superblock version %d not known\n", |
| 5832 | info->major_version); |
| 5833 | return -EINVAL; |
| 5834 | } |
| 5835 | mddev->major_version = info->major_version; |
| 5836 | mddev->minor_version = info->minor_version; |
| 5837 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5838 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5839 | /* ensure mddev_put doesn't delete this now that there |
| 5840 | * is some minimal configuration. |
| 5841 | */ |
| 5842 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | return 0; |
| 5844 | } |
| 5845 | mddev->major_version = MD_MAJOR_VERSION; |
| 5846 | mddev->minor_version = MD_MINOR_VERSION; |
| 5847 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5848 | mddev->ctime = get_seconds(); |
| 5849 | |
| 5850 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5851 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5852 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | mddev->raid_disks = info->raid_disks; |
| 5854 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5855 | * openned |
| 5856 | */ |
| 5857 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5858 | mddev->recovery_cp = MaxSector; |
| 5859 | else |
| 5860 | mddev->recovery_cp = 0; |
| 5861 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5862 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | |
| 5864 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5865 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | |
| 5867 | mddev->max_disks = MD_SB_DISKS; |
| 5868 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5869 | if (mddev->persistent) |
| 5870 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5871 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5873 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5874 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5875 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5876 | mddev->reshape_position = MaxSector; |
| 5877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | /* |
| 5879 | * Generate a 128 bit UUID |
| 5880 | */ |
| 5881 | get_random_bytes(mddev->uuid, 16); |
| 5882 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5883 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5884 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5885 | mddev->new_layout = mddev->layout; |
| 5886 | mddev->delta_disks = 0; |
| 5887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | return 0; |
| 5889 | } |
| 5890 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5891 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5892 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5893 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5894 | |
| 5895 | if (mddev->external_size) |
| 5896 | return; |
| 5897 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5898 | mddev->array_sectors = array_sectors; |
| 5899 | } |
| 5900 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5901 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5902 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5903 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5904 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5905 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5906 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5907 | |
| 5908 | if (mddev->pers->resize == NULL) |
| 5909 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5910 | /* The "num_sectors" is the number of sectors of each device that |
| 5911 | * is used. This can only make sense for arrays with redundancy. |
| 5912 | * linear and raid0 always use whatever space is available. We can only |
| 5913 | * consider changing this number if no resync or reconstruction is |
| 5914 | * 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] | 5915 | * 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] | 5916 | * of each device. If num_sectors is zero, we find the largest size |
| 5917 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5918 | */ |
| 5919 | if (mddev->sync_thread) |
| 5920 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5921 | if (mddev->bitmap) |
| 5922 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5923 | * grow, and re-add. |
| 5924 | */ |
| 5925 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5926 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5927 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5928 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5929 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5930 | num_sectors = avail; |
| 5931 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5932 | return -ENOSPC; |
| 5933 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5934 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5935 | if (!rv) |
| 5936 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5937 | return rv; |
| 5938 | } |
| 5939 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5940 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5941 | { |
| 5942 | int rv; |
| 5943 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5944 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5945 | return -EINVAL; |
| 5946 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5947 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5948 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5949 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5950 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5951 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5952 | |
| 5953 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5954 | if (rv < 0) |
| 5955 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5956 | return rv; |
| 5957 | } |
| 5958 | |
| 5959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | /* |
| 5961 | * update_array_info is used to change the configuration of an |
| 5962 | * on-line array. |
| 5963 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5964 | * fields in the info are checked against the array. |
| 5965 | * Any differences that cannot be handled will cause an error. |
| 5966 | * Normally, only one change can be managed at a time. |
| 5967 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5968 | 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] | 5969 | { |
| 5970 | int rv = 0; |
| 5971 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5972 | int state = 0; |
| 5973 | |
| 5974 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5975 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5976 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | |
| 5978 | if (mddev->major_version != info->major_version || |
| 5979 | mddev->minor_version != info->minor_version || |
| 5980 | /* mddev->patch_version != info->patch_version || */ |
| 5981 | mddev->ctime != info->ctime || |
| 5982 | mddev->level != info->level || |
| 5983 | /* mddev->layout != info->layout || */ |
| 5984 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5985 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5986 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5987 | ((state^info->state) & 0xfffffe00) |
| 5988 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | return -EINVAL; |
| 5990 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5991 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5992 | cnt++; |
| 5993 | if (mddev->raid_disks != info->raid_disks) |
| 5994 | cnt++; |
| 5995 | if (mddev->layout != info->layout) |
| 5996 | cnt++; |
| 5997 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5998 | cnt++; |
| 5999 | if (cnt == 0) |
| 6000 | return 0; |
| 6001 | if (cnt > 1) |
| 6002 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | |
| 6004 | if (mddev->layout != info->layout) { |
| 6005 | /* Change layout |
| 6006 | * we don't need to do anything at the md level, the |
| 6007 | * personality will take care of it all. |
| 6008 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6009 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6011 | else { |
| 6012 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6013 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6014 | if (rv) |
| 6015 | mddev->new_layout = mddev->layout; |
| 6016 | return rv; |
| 6017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6019 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6020 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6022 | if (mddev->raid_disks != info->raid_disks) |
| 6023 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6025 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6026 | if (mddev->pers->quiesce == NULL) |
| 6027 | return -EINVAL; |
| 6028 | if (mddev->recovery || mddev->sync_thread) |
| 6029 | return -EBUSY; |
| 6030 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6031 | /* add the bitmap */ |
| 6032 | if (mddev->bitmap) |
| 6033 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6034 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6035 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6036 | mddev->bitmap_info.offset = |
| 6037 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6038 | mddev->pers->quiesce(mddev, 1); |
| 6039 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6040 | if (!rv) |
| 6041 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6042 | if (rv) |
| 6043 | bitmap_destroy(mddev); |
| 6044 | mddev->pers->quiesce(mddev, 0); |
| 6045 | } else { |
| 6046 | /* remove the bitmap */ |
| 6047 | if (!mddev->bitmap) |
| 6048 | return -ENOENT; |
| 6049 | if (mddev->bitmap->file) |
| 6050 | return -EINVAL; |
| 6051 | mddev->pers->quiesce(mddev, 1); |
| 6052 | bitmap_destroy(mddev); |
| 6053 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6054 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6055 | } |
| 6056 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6057 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | return rv; |
| 6059 | } |
| 6060 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6061 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6063 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | |
| 6065 | if (mddev->pers == NULL) |
| 6066 | return -ENODEV; |
| 6067 | |
| 6068 | rdev = find_rdev(mddev, dev); |
| 6069 | if (!rdev) |
| 6070 | return -ENODEV; |
| 6071 | |
| 6072 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6073 | if (!test_bit(Faulty, &rdev->flags)) |
| 6074 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | return 0; |
| 6076 | } |
| 6077 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6078 | /* |
| 6079 | * We have a problem here : there is no easy way to give a CHS |
| 6080 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6081 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6082 | * dosfs just mad... ;-) |
| 6083 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6084 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6085 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6086 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6087 | |
| 6088 | geo->heads = 2; |
| 6089 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6090 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6091 | return 0; |
| 6092 | } |
| 6093 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6094 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | unsigned int cmd, unsigned long arg) |
| 6096 | { |
| 6097 | int err = 0; |
| 6098 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6099 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6100 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6102 | switch (cmd) { |
| 6103 | case RAID_VERSION: |
| 6104 | case GET_ARRAY_INFO: |
| 6105 | case GET_DISK_INFO: |
| 6106 | break; |
| 6107 | default: |
| 6108 | if (!capable(CAP_SYS_ADMIN)) |
| 6109 | return -EACCES; |
| 6110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | |
| 6112 | /* |
| 6113 | * Commands dealing with the RAID driver but not any |
| 6114 | * particular array: |
| 6115 | */ |
| 6116 | switch (cmd) |
| 6117 | { |
| 6118 | case RAID_VERSION: |
| 6119 | err = get_version(argp); |
| 6120 | goto done; |
| 6121 | |
| 6122 | case PRINT_RAID_DEBUG: |
| 6123 | err = 0; |
| 6124 | md_print_devices(); |
| 6125 | goto done; |
| 6126 | |
| 6127 | #ifndef MODULE |
| 6128 | case RAID_AUTORUN: |
| 6129 | err = 0; |
| 6130 | autostart_arrays(arg); |
| 6131 | goto done; |
| 6132 | #endif |
| 6133 | default:; |
| 6134 | } |
| 6135 | |
| 6136 | /* |
| 6137 | * Commands creating/starting a new array: |
| 6138 | */ |
| 6139 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6140 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | |
| 6142 | if (!mddev) { |
| 6143 | BUG(); |
| 6144 | goto abort; |
| 6145 | } |
| 6146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | err = mddev_lock(mddev); |
| 6148 | if (err) { |
| 6149 | printk(KERN_INFO |
| 6150 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6151 | err, cmd); |
| 6152 | goto abort; |
| 6153 | } |
| 6154 | |
| 6155 | switch (cmd) |
| 6156 | { |
| 6157 | case SET_ARRAY_INFO: |
| 6158 | { |
| 6159 | mdu_array_info_t info; |
| 6160 | if (!arg) |
| 6161 | memset(&info, 0, sizeof(info)); |
| 6162 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6163 | err = -EFAULT; |
| 6164 | goto abort_unlock; |
| 6165 | } |
| 6166 | if (mddev->pers) { |
| 6167 | err = update_array_info(mddev, &info); |
| 6168 | if (err) { |
| 6169 | printk(KERN_WARNING "md: couldn't update" |
| 6170 | " array info. %d\n", err); |
| 6171 | goto abort_unlock; |
| 6172 | } |
| 6173 | goto done_unlock; |
| 6174 | } |
| 6175 | if (!list_empty(&mddev->disks)) { |
| 6176 | printk(KERN_WARNING |
| 6177 | "md: array %s already has disks!\n", |
| 6178 | mdname(mddev)); |
| 6179 | err = -EBUSY; |
| 6180 | goto abort_unlock; |
| 6181 | } |
| 6182 | if (mddev->raid_disks) { |
| 6183 | printk(KERN_WARNING |
| 6184 | "md: array %s already initialised!\n", |
| 6185 | mdname(mddev)); |
| 6186 | err = -EBUSY; |
| 6187 | goto abort_unlock; |
| 6188 | } |
| 6189 | err = set_array_info(mddev, &info); |
| 6190 | if (err) { |
| 6191 | printk(KERN_WARNING "md: couldn't set" |
| 6192 | " array info. %d\n", err); |
| 6193 | goto abort_unlock; |
| 6194 | } |
| 6195 | } |
| 6196 | goto done_unlock; |
| 6197 | |
| 6198 | default:; |
| 6199 | } |
| 6200 | |
| 6201 | /* |
| 6202 | * Commands querying/configuring an existing array: |
| 6203 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6204 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6205 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6206 | if ((!mddev->raid_disks && !mddev->external) |
| 6207 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6208 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6209 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | err = -ENODEV; |
| 6211 | goto abort_unlock; |
| 6212 | } |
| 6213 | |
| 6214 | /* |
| 6215 | * Commands even a read-only array can execute: |
| 6216 | */ |
| 6217 | switch (cmd) |
| 6218 | { |
| 6219 | case GET_ARRAY_INFO: |
| 6220 | err = get_array_info(mddev, argp); |
| 6221 | goto done_unlock; |
| 6222 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6223 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6224 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6225 | goto done_unlock; |
| 6226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | case GET_DISK_INFO: |
| 6228 | err = get_disk_info(mddev, argp); |
| 6229 | goto done_unlock; |
| 6230 | |
| 6231 | case RESTART_ARRAY_RW: |
| 6232 | err = restart_array(mddev); |
| 6233 | goto done_unlock; |
| 6234 | |
| 6235 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6236 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | goto done_unlock; |
| 6238 | |
| 6239 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6240 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | goto done_unlock; |
| 6242 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6243 | case BLKROSET: |
| 6244 | if (get_user(ro, (int __user *)(arg))) { |
| 6245 | err = -EFAULT; |
| 6246 | goto done_unlock; |
| 6247 | } |
| 6248 | err = -EINVAL; |
| 6249 | |
| 6250 | /* if the bdev is going readonly the value of mddev->ro |
| 6251 | * does not matter, no writes are coming |
| 6252 | */ |
| 6253 | if (ro) |
| 6254 | goto done_unlock; |
| 6255 | |
| 6256 | /* are we are already prepared for writes? */ |
| 6257 | if (mddev->ro != 1) |
| 6258 | goto done_unlock; |
| 6259 | |
| 6260 | /* transitioning to readauto need only happen for |
| 6261 | * arrays that call md_write_start |
| 6262 | */ |
| 6263 | if (mddev->pers) { |
| 6264 | err = restart_array(mddev); |
| 6265 | if (err == 0) { |
| 6266 | mddev->ro = 2; |
| 6267 | set_disk_ro(mddev->gendisk, 0); |
| 6268 | } |
| 6269 | } |
| 6270 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | /* |
| 6274 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6275 | * superblock, so we do not allow them on read-only arrays. |
| 6276 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6277 | * here and hit the 'default' below, so only disallow |
| 6278 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6280 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6281 | if (mddev->ro == 2) { |
| 6282 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6283 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6284 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6285 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6286 | } else { |
| 6287 | err = -EROFS; |
| 6288 | goto abort_unlock; |
| 6289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | } |
| 6291 | |
| 6292 | switch (cmd) |
| 6293 | { |
| 6294 | case ADD_NEW_DISK: |
| 6295 | { |
| 6296 | mdu_disk_info_t info; |
| 6297 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6298 | err = -EFAULT; |
| 6299 | else |
| 6300 | err = add_new_disk(mddev, &info); |
| 6301 | goto done_unlock; |
| 6302 | } |
| 6303 | |
| 6304 | case HOT_REMOVE_DISK: |
| 6305 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6306 | goto done_unlock; |
| 6307 | |
| 6308 | case HOT_ADD_DISK: |
| 6309 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6310 | goto done_unlock; |
| 6311 | |
| 6312 | case SET_DISK_FAULTY: |
| 6313 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6314 | goto done_unlock; |
| 6315 | |
| 6316 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6317 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | goto done_unlock; |
| 6319 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6320 | case SET_BITMAP_FILE: |
| 6321 | err = set_bitmap_file(mddev, (int)arg); |
| 6322 | goto done_unlock; |
| 6323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | err = -EINVAL; |
| 6326 | goto abort_unlock; |
| 6327 | } |
| 6328 | |
| 6329 | done_unlock: |
| 6330 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6331 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6332 | err != -EINVAL) |
| 6333 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | mddev_unlock(mddev); |
| 6335 | |
| 6336 | return err; |
| 6337 | done: |
| 6338 | if (err) |
| 6339 | MD_BUG(); |
| 6340 | abort: |
| 6341 | return err; |
| 6342 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6343 | #ifdef CONFIG_COMPAT |
| 6344 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6345 | unsigned int cmd, unsigned long arg) |
| 6346 | { |
| 6347 | switch (cmd) { |
| 6348 | case HOT_REMOVE_DISK: |
| 6349 | case HOT_ADD_DISK: |
| 6350 | case SET_DISK_FAULTY: |
| 6351 | case SET_BITMAP_FILE: |
| 6352 | /* These take in integer arg, do not convert */ |
| 6353 | break; |
| 6354 | default: |
| 6355 | arg = (unsigned long)compat_ptr(arg); |
| 6356 | break; |
| 6357 | } |
| 6358 | |
| 6359 | return md_ioctl(bdev, mode, cmd, arg); |
| 6360 | } |
| 6361 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6363 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | { |
| 6365 | /* |
| 6366 | * Succeed if we can lock the mddev, which confirms that |
| 6367 | * it isn't being stopped right now. |
| 6368 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6369 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | int err; |
| 6371 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6372 | if (mddev->gendisk != bdev->bd_disk) { |
| 6373 | /* we are racing with mddev_put which is discarding this |
| 6374 | * bd_disk. |
| 6375 | */ |
| 6376 | mddev_put(mddev); |
| 6377 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6378 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6379 | /* Then retry the open from the top */ |
| 6380 | return -ERESTARTSYS; |
| 6381 | } |
| 6382 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6383 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6384 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | goto out; |
| 6386 | |
| 6387 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6388 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6389 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6391 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | out: |
| 6393 | return err; |
| 6394 | } |
| 6395 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6396 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6398 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6400 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6401 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | mddev_put(mddev); |
| 6403 | |
| 6404 | return 0; |
| 6405 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6406 | |
| 6407 | static int md_media_changed(struct gendisk *disk) |
| 6408 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6409 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6410 | |
| 6411 | return mddev->changed; |
| 6412 | } |
| 6413 | |
| 6414 | static int md_revalidate(struct gendisk *disk) |
| 6415 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6416 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6417 | |
| 6418 | mddev->changed = 0; |
| 6419 | return 0; |
| 6420 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6421 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | { |
| 6423 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6424 | .open = md_open, |
| 6425 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6426 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6427 | #ifdef CONFIG_COMPAT |
| 6428 | .compat_ioctl = md_compat_ioctl, |
| 6429 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6430 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6431 | .media_changed = md_media_changed, |
| 6432 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | }; |
| 6434 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6435 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6437 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | /* |
| 6440 | * md_thread is a 'system-thread', it's priority should be very |
| 6441 | * high. We avoid resource deadlocks individually in each |
| 6442 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6443 | * the very same RT priority as kswapd, thus we will never get |
| 6444 | * into a priority inversion deadlock. |
| 6445 | * |
| 6446 | * we definitely have to have equal or higher priority than |
| 6447 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6448 | * many dirty RAID5 blocks. |
| 6449 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6451 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6452 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6454 | /* We need to wait INTERRUPTIBLE so that |
| 6455 | * we don't add to the load-average. |
| 6456 | * That means we need to be sure no signals are |
| 6457 | * pending |
| 6458 | */ |
| 6459 | if (signal_pending(current)) |
| 6460 | flush_signals(current); |
| 6461 | |
| 6462 | wait_event_interruptible_timeout |
| 6463 | (thread->wqueue, |
| 6464 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6465 | || kthread_should_stop(), |
| 6466 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6468 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6469 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6470 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | return 0; |
| 6474 | } |
| 6475 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6476 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | { |
| 6478 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6479 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6481 | wake_up(&thread->wqueue); |
| 6482 | } |
| 6483 | } |
| 6484 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6485 | 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] | 6486 | const char *name) |
| 6487 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6488 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6490 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | if (!thread) |
| 6492 | return NULL; |
| 6493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | init_waitqueue_head(&thread->wqueue); |
| 6495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | thread->run = run; |
| 6497 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6498 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6499 | thread->tsk = kthread_run(md_thread, thread, |
| 6500 | "%s_%s", |
| 6501 | mdname(thread->mddev), |
| 6502 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6503 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | kfree(thread); |
| 6505 | return NULL; |
| 6506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | return thread; |
| 6508 | } |
| 6509 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6510 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6512 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6513 | if (!thread) |
| 6514 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6515 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6516 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6517 | * non-existent thread |
| 6518 | */ |
| 6519 | spin_lock(&pers_lock); |
| 6520 | *threadp = NULL; |
| 6521 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6522 | |
| 6523 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | kfree(thread); |
| 6525 | } |
| 6526 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6527 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | { |
| 6529 | if (!mddev) { |
| 6530 | MD_BUG(); |
| 6531 | return; |
| 6532 | } |
| 6533 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6534 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6536 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6537 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | return; |
| 6539 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6540 | if (mddev->degraded) |
| 6541 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6542 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6544 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6545 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6546 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6547 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6548 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | /* seq_file implementation /proc/mdstat */ |
| 6552 | |
| 6553 | static void status_unused(struct seq_file *seq) |
| 6554 | { |
| 6555 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6556 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | |
| 6558 | seq_printf(seq, "unused devices: "); |
| 6559 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6560 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | char b[BDEVNAME_SIZE]; |
| 6562 | i++; |
| 6563 | seq_printf(seq, "%s ", |
| 6564 | bdevname(rdev->bdev,b)); |
| 6565 | } |
| 6566 | if (!i) |
| 6567 | seq_printf(seq, "<none>"); |
| 6568 | |
| 6569 | seq_printf(seq, "\n"); |
| 6570 | } |
| 6571 | |
| 6572 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6573 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6575 | sector_t max_sectors, resync, res; |
| 6576 | unsigned long dt, db; |
| 6577 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6578 | int scale; |
| 6579 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6580 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6581 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | |
| 6583 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6584 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6586 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | |
| 6588 | /* |
| 6589 | * Should not happen. |
| 6590 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6591 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | MD_BUG(); |
| 6593 | return; |
| 6594 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6595 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6596 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6597 | * u32, as those are the requirements for sector_div. |
| 6598 | * Thus 'scale' must be at least 10 |
| 6599 | */ |
| 6600 | scale = 10; |
| 6601 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6602 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6603 | scale++; |
| 6604 | } |
| 6605 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6606 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6607 | |
| 6608 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6610 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | seq_printf(seq, "["); |
| 6612 | for (i = 0; i < x; i++) |
| 6613 | seq_printf(seq, "="); |
| 6614 | seq_printf(seq, ">"); |
| 6615 | for (i = 0; i < y; i++) |
| 6616 | seq_printf(seq, "."); |
| 6617 | seq_printf(seq, "] "); |
| 6618 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6619 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6620 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6621 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6622 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6623 | "check" : |
| 6624 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6625 | "resync" : "recovery"))), |
| 6626 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6627 | (unsigned long long) resync/2, |
| 6628 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | |
| 6630 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | * dt: time from mark until now |
| 6632 | * db: blocks written from mark until now |
| 6633 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6634 | * |
| 6635 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6636 | * So we divide before multiply in case it is 32bit and close |
| 6637 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6638 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6639 | * near the end of resync when the number of remaining sectors |
| 6640 | * is close to 'db'. |
| 6641 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6642 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | */ |
| 6644 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6645 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6646 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6647 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6649 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6650 | sector_div(rt, db/32+1); |
| 6651 | rt *= dt; |
| 6652 | rt >>= 5; |
| 6653 | |
| 6654 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6655 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6657 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | } |
| 6659 | |
| 6660 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6661 | { |
| 6662 | struct list_head *tmp; |
| 6663 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6664 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | |
| 6666 | if (l >= 0x10000) |
| 6667 | return NULL; |
| 6668 | if (!l--) |
| 6669 | /* header */ |
| 6670 | return (void*)1; |
| 6671 | |
| 6672 | spin_lock(&all_mddevs_lock); |
| 6673 | list_for_each(tmp,&all_mddevs) |
| 6674 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6675 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | mddev_get(mddev); |
| 6677 | spin_unlock(&all_mddevs_lock); |
| 6678 | return mddev; |
| 6679 | } |
| 6680 | spin_unlock(&all_mddevs_lock); |
| 6681 | if (!l--) |
| 6682 | return (void*)2;/* tail */ |
| 6683 | return NULL; |
| 6684 | } |
| 6685 | |
| 6686 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6687 | { |
| 6688 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6689 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | |
| 6691 | ++*pos; |
| 6692 | if (v == (void*)2) |
| 6693 | return NULL; |
| 6694 | |
| 6695 | spin_lock(&all_mddevs_lock); |
| 6696 | if (v == (void*)1) |
| 6697 | tmp = all_mddevs.next; |
| 6698 | else |
| 6699 | tmp = mddev->all_mddevs.next; |
| 6700 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6701 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | else { |
| 6703 | next_mddev = (void*)2; |
| 6704 | *pos = 0x10000; |
| 6705 | } |
| 6706 | spin_unlock(&all_mddevs_lock); |
| 6707 | |
| 6708 | if (v != (void*)1) |
| 6709 | mddev_put(mddev); |
| 6710 | return next_mddev; |
| 6711 | |
| 6712 | } |
| 6713 | |
| 6714 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6715 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6716 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | |
| 6718 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6719 | mddev_put(mddev); |
| 6720 | } |
| 6721 | |
| 6722 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6723 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6724 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6725 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6726 | struct md_rdev *rdev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6727 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | |
| 6729 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6730 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | seq_printf(seq, "Personalities : "); |
| 6732 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6733 | list_for_each_entry(pers, &pers_list, list) |
| 6734 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | |
| 6736 | spin_unlock(&pers_lock); |
| 6737 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6738 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | return 0; |
| 6740 | } |
| 6741 | if (v == (void*)2) { |
| 6742 | status_unused(seq); |
| 6743 | return 0; |
| 6744 | } |
| 6745 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6746 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6750 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6751 | mddev->pers ? "" : "in"); |
| 6752 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6753 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6755 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6756 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6757 | seq_printf(seq, " %s", mddev->pers->name); |
| 6758 | } |
| 6759 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6760 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6761 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | char b[BDEVNAME_SIZE]; |
| 6763 | seq_printf(seq, " %s[%d]", |
| 6764 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6765 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6766 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6767 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | seq_printf(seq, "(F)"); |
| 6769 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6770 | } |
| 6771 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6772 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6773 | if (test_bit(Replacement, &rdev->flags)) |
| 6774 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6775 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | } |
| 6777 | |
| 6778 | if (!list_empty(&mddev->disks)) { |
| 6779 | if (mddev->pers) |
| 6780 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6781 | (unsigned long long) |
| 6782 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | else |
| 6784 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6785 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6787 | if (mddev->persistent) { |
| 6788 | if (mddev->major_version != 0 || |
| 6789 | mddev->minor_version != 90) { |
| 6790 | seq_printf(seq," super %d.%d", |
| 6791 | mddev->major_version, |
| 6792 | mddev->minor_version); |
| 6793 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6794 | } else if (mddev->external) |
| 6795 | seq_printf(seq, " super external:%s", |
| 6796 | mddev->metadata_type); |
| 6797 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6798 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | |
| 6800 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6801 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6803 | if (mddev->pers->sync_request) { |
| 6804 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6805 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6806 | seq_printf(seq, "\n "); |
| 6807 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6808 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6809 | else if (mddev->recovery_cp < MaxSector) |
| 6810 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6811 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6812 | } else |
| 6813 | seq_printf(seq, "\n "); |
| 6814 | |
| 6815 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6816 | unsigned long chunk_kb; |
| 6817 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6818 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6819 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6820 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6821 | "%lu%s chunk", |
| 6822 | bitmap->pages - bitmap->missing_pages, |
| 6823 | bitmap->pages, |
| 6824 | (bitmap->pages - bitmap->missing_pages) |
| 6825 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6826 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6827 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6828 | if (bitmap->file) { |
| 6829 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6830 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6831 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6832 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6833 | seq_printf(seq, "\n"); |
| 6834 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | seq_printf(seq, "\n"); |
| 6838 | } |
| 6839 | mddev_unlock(mddev); |
| 6840 | |
| 6841 | return 0; |
| 6842 | } |
| 6843 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6844 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | .start = md_seq_start, |
| 6846 | .next = md_seq_next, |
| 6847 | .stop = md_seq_stop, |
| 6848 | .show = md_seq_show, |
| 6849 | }; |
| 6850 | |
| 6851 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6852 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6853 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | int error; |
| 6855 | |
| 6856 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6857 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6858 | return error; |
| 6859 | |
| 6860 | seq = file->private_data; |
| 6861 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | return error; |
| 6863 | } |
| 6864 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6865 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6866 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6867 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6868 | int mask; |
| 6869 | |
| 6870 | poll_wait(filp, &md_event_waiters, wait); |
| 6871 | |
| 6872 | /* always allow read */ |
| 6873 | mask = POLLIN | POLLRDNORM; |
| 6874 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6875 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6876 | mask |= POLLERR | POLLPRI; |
| 6877 | return mask; |
| 6878 | } |
| 6879 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6880 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6881 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | .open = md_seq_open, |
| 6883 | .read = seq_read, |
| 6884 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6885 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6886 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | }; |
| 6888 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6889 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6892 | list_add_tail(&p->list, &pers_list); |
| 6893 | 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] | 6894 | spin_unlock(&pers_lock); |
| 6895 | return 0; |
| 6896 | } |
| 6897 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6898 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6900 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6902 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | spin_unlock(&pers_lock); |
| 6904 | return 0; |
| 6905 | } |
| 6906 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6907 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6909 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6911 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | |
| 6913 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6914 | rcu_read_lock(); |
| 6915 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6917 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6918 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6919 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6920 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6921 | * as sync_io is counted when a request starts, and |
| 6922 | * disk_stats is counted when it completes. |
| 6923 | * So resync activity will cause curr_events to be smaller than |
| 6924 | * when there was no such activity. |
| 6925 | * non-sync IO will cause disk_stat to increase without |
| 6926 | * increasing sync_io so curr_events will (eventually) |
| 6927 | * be larger than it was before. Once it becomes |
| 6928 | * substantially larger, the test below will cause |
| 6929 | * the array to appear non-idle, and resync will slow |
| 6930 | * down. |
| 6931 | * If there is a lot of outstanding resync activity when |
| 6932 | * we set last_event to curr_events, then all that activity |
| 6933 | * completing might cause the array to appear non-idle |
| 6934 | * and resync will be slowed down even though there might |
| 6935 | * not have been non-resync activity. This will only |
| 6936 | * happen once though. 'last_events' will soon reflect |
| 6937 | * the state where there is little or no outstanding |
| 6938 | * resync requests, and further resync activity will |
| 6939 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6940 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6942 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | rdev->last_events = curr_events; |
| 6944 | idle = 0; |
| 6945 | } |
| 6946 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6947 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | return idle; |
| 6949 | } |
| 6950 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6951 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | { |
| 6953 | /* another "blocks" (512byte) blocks have been synced */ |
| 6954 | atomic_sub(blocks, &mddev->recovery_active); |
| 6955 | wake_up(&mddev->recovery_wait); |
| 6956 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6957 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | md_wakeup_thread(mddev->thread); |
| 6959 | // stop recovery, signal do_sync .... |
| 6960 | } |
| 6961 | } |
| 6962 | |
| 6963 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6964 | /* md_write_start(mddev, bi) |
| 6965 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6966 | * in superblock) before writing, schedule a superblock update |
| 6967 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6968 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6969 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6971 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6972 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6973 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6974 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6975 | BUG_ON(mddev->ro == 1); |
| 6976 | if (mddev->ro == 2) { |
| 6977 | /* need to switch to read/write */ |
| 6978 | mddev->ro = 0; |
| 6979 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6980 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6981 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6982 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6983 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6984 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6985 | if (mddev->safemode == 1) |
| 6986 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6987 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6988 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6989 | if (mddev->in_sync) { |
| 6990 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6991 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6992 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6993 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6994 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6995 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6996 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6997 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6998 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6999 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7000 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7001 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | } |
| 7003 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7004 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | { |
| 7006 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7007 | if (mddev->safemode == 2) |
| 7008 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7009 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7011 | } |
| 7012 | } |
| 7013 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7014 | /* md_allow_write(mddev) |
| 7015 | * Calling this ensures that the array is marked 'active' so that writes |
| 7016 | * may proceed without blocking. It is important to call this before |
| 7017 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7018 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7019 | * |
| 7020 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7021 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7022 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7023 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7024 | { |
| 7025 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7026 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7027 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7028 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7029 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7030 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7031 | |
| 7032 | spin_lock_irq(&mddev->write_lock); |
| 7033 | if (mddev->in_sync) { |
| 7034 | mddev->in_sync = 0; |
| 7035 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7036 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7037 | if (mddev->safemode_delay && |
| 7038 | mddev->safemode == 0) |
| 7039 | mddev->safemode = 1; |
| 7040 | spin_unlock_irq(&mddev->write_lock); |
| 7041 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7042 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7043 | } else |
| 7044 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7045 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7046 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7047 | return -EAGAIN; |
| 7048 | else |
| 7049 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7050 | } |
| 7051 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | #define SYNC_MARKS 10 |
| 7054 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7055 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7057 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | unsigned int currspeed = 0, |
| 7059 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7060 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | unsigned long mark[SYNC_MARKS]; |
| 7062 | sector_t mark_cnt[SYNC_MARKS]; |
| 7063 | int last_mark,m; |
| 7064 | struct list_head *tmp; |
| 7065 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7066 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7067 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7068 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | |
| 7070 | /* just incase thread restarts... */ |
| 7071 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7072 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7073 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7074 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7076 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7077 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7078 | desc = "data-check"; |
| 7079 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7080 | desc = "requested-resync"; |
| 7081 | else |
| 7082 | desc = "resync"; |
| 7083 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7084 | desc = "reshape"; |
| 7085 | else |
| 7086 | desc = "recovery"; |
| 7087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | /* we overload curr_resync somewhat here. |
| 7089 | * 0 == not engaged in resync at all |
| 7090 | * 2 == checking that there is no conflict with another sync |
| 7091 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7092 | * commense |
| 7093 | * other == active in resync - this many blocks |
| 7094 | * |
| 7095 | * Before starting a resync we must have set curr_resync to |
| 7096 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7097 | * less than ours. When we find one that is the same or higher |
| 7098 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7099 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7100 | * This will mean we have to start checking from the beginning again. |
| 7101 | * |
| 7102 | */ |
| 7103 | |
| 7104 | do { |
| 7105 | mddev->curr_resync = 2; |
| 7106 | |
| 7107 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7108 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7109 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7110 | |
| 7111 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7113 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | if (mddev2 == mddev) |
| 7115 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7116 | if (!mddev->parallel_resync |
| 7117 | && mddev2->curr_resync |
| 7118 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | DEFINE_WAIT(wq); |
| 7120 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7121 | /* arbitrarily yield */ |
| 7122 | mddev->curr_resync = 1; |
| 7123 | wake_up(&resync_wait); |
| 7124 | } |
| 7125 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7126 | /* no need to wait here, we can wait the next |
| 7127 | * time 'round when curr_resync == 2 |
| 7128 | */ |
| 7129 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7130 | /* We need to wait 'interruptible' so as not to |
| 7131 | * contribute to the load average, and not to |
| 7132 | * be caught by 'softlockup' |
| 7133 | */ |
| 7134 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7135 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7136 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7137 | printk(KERN_INFO "md: delaying %s of %s" |
| 7138 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7140 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7142 | if (signal_pending(current)) |
| 7143 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7144 | schedule(); |
| 7145 | finish_wait(&resync_wait, &wq); |
| 7146 | goto try_again; |
| 7147 | } |
| 7148 | finish_wait(&resync_wait, &wq); |
| 7149 | } |
| 7150 | } |
| 7151 | } while (mddev->curr_resync < 2); |
| 7152 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7153 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7154 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7156 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7157 | */ |
| 7158 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7159 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7160 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7161 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7162 | j = mddev->resync_min; |
| 7163 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7164 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7165 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7166 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7167 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7168 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7169 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7170 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7171 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7172 | rcu_read_lock(); |
| 7173 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7174 | if (rdev->raid_disk >= 0 && |
| 7175 | !test_bit(Faulty, &rdev->flags) && |
| 7176 | !test_bit(In_sync, &rdev->flags) && |
| 7177 | rdev->recovery_offset < j) |
| 7178 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7179 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7182 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7183 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7184 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7185 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7186 | "(but not more than %d KB/sec) for %s.\n", |
| 7187 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7189 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7190 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7191 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7193 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7194 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7195 | } |
| 7196 | last_mark = 0; |
| 7197 | mddev->resync_mark = mark[last_mark]; |
| 7198 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7199 | |
| 7200 | /* |
| 7201 | * Tune reconstruction: |
| 7202 | */ |
| 7203 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7204 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7205 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | |
| 7207 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7208 | last_check = 0; |
| 7209 | |
| 7210 | if (j>2) { |
| 7211 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7212 | "md: resuming %s of %s from checkpoint.\n", |
| 7213 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | mddev->curr_resync = j; |
| 7215 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7216 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | |
| 7218 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7219 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7221 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7222 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7223 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7224 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7225 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7226 | > (max_sectors >> 4)) || |
| 7227 | (j - mddev->curr_resync_completed)*2 |
| 7228 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7229 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7230 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7231 | wait_event(mddev->recovery_wait, |
| 7232 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7233 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7234 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7235 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7236 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7237 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7238 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7239 | /* As this condition is controlled by user-space, |
| 7240 | * we can block indefinitely, so use '_interruptible' |
| 7241 | * to avoid triggering warnings. |
| 7242 | */ |
| 7243 | flush_signals(current); /* just in case */ |
| 7244 | wait_event_interruptible(mddev->recovery_wait, |
| 7245 | mddev->resync_max > j |
| 7246 | || kthread_should_stop()); |
| 7247 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7248 | |
| 7249 | if (kthread_should_stop()) |
| 7250 | goto interrupted; |
| 7251 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7252 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7253 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7254 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7255 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7256 | goto out; |
| 7257 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7258 | |
| 7259 | if (!skipped) { /* actual IO requested */ |
| 7260 | io_sectors += sectors; |
| 7261 | atomic_add(sectors, &mddev->recovery_active); |
| 7262 | } |
| 7263 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7264 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7265 | break; |
| 7266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | j += sectors; |
| 7268 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7269 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7270 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7271 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7272 | * visible in /proc/mdstat |
| 7273 | */ |
| 7274 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7275 | |
| 7276 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7277 | continue; |
| 7278 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7279 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7280 | repeat: |
| 7281 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7282 | /* step marks */ |
| 7283 | int next = (last_mark+1) % SYNC_MARKS; |
| 7284 | |
| 7285 | mddev->resync_mark = mark[next]; |
| 7286 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7287 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7288 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | last_mark = next; |
| 7290 | } |
| 7291 | |
| 7292 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7293 | if (kthread_should_stop()) |
| 7294 | goto interrupted; |
| 7295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | |
| 7297 | /* |
| 7298 | * this loop exits only if either when we are slower than |
| 7299 | * the 'hard' speed limit, or the system was IO-idle for |
| 7300 | * a jiffy. |
| 7301 | * the system might be non-idle CPU-wise, but we only care |
| 7302 | * about not overloading the IO subsystem. (things like an |
| 7303 | * e2fsck being done on the RAID array should execute fast) |
| 7304 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | cond_resched(); |
| 7306 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7307 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7308 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7309 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7310 | if (currspeed > speed_min(mddev)) { |
| 7311 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7312 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7313 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7314 | goto repeat; |
| 7315 | } |
| 7316 | } |
| 7317 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7318 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | /* |
| 7320 | * this also signals 'finished resyncing' to md_stop |
| 7321 | */ |
| 7322 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7324 | |
| 7325 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7326 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7328 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7329 | mddev->curr_resync > 2) { |
| 7330 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7331 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7332 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7333 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7334 | "md: checkpointing %s of %s.\n", |
| 7335 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7336 | mddev->recovery_cp = mddev->curr_resync; |
| 7337 | } |
| 7338 | } else |
| 7339 | mddev->recovery_cp = MaxSector; |
| 7340 | } else { |
| 7341 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7342 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7343 | rcu_read_lock(); |
| 7344 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7345 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7346 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7347 | !test_bit(Faulty, &rdev->flags) && |
| 7348 | !test_bit(In_sync, &rdev->flags) && |
| 7349 | rdev->recovery_offset < mddev->curr_resync) |
| 7350 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7351 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7354 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7356 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7357 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7358 | /* We completed so min/max setting can be forgotten if used. */ |
| 7359 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7360 | mddev->resync_min = 0; |
| 7361 | mddev->resync_max = MaxSector; |
| 7362 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7363 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7364 | mddev->curr_resync = 0; |
| 7365 | wake_up(&resync_wait); |
| 7366 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7367 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7368 | return; |
| 7369 | |
| 7370 | interrupted: |
| 7371 | /* |
| 7372 | * got a signal, exit. |
| 7373 | */ |
| 7374 | printk(KERN_INFO |
| 7375 | "md: md_do_sync() got signal ... exiting\n"); |
| 7376 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7377 | goto out; |
| 7378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7379 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7380 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7382 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7383 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7384 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7385 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7386 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7387 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7388 | mddev->curr_resync_completed = 0; |
| 7389 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7390 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7391 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7392 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7393 | (test_bit(Faulty, &rdev->flags) || |
| 7394 | ! test_bit(In_sync, &rdev->flags)) && |
| 7395 | atomic_read(&rdev->nr_pending)==0) { |
| 7396 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7397 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7398 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7399 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7400 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7401 | } |
| 7402 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7403 | if (removed) |
| 7404 | sysfs_notify(&mddev->kobj, NULL, |
| 7405 | "degraded"); |
| 7406 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7407 | |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7408 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 7409 | if (rdev->raid_disk >= 0 && |
| 7410 | !test_bit(In_sync, &rdev->flags) && |
| 7411 | !test_bit(Faulty, &rdev->flags)) |
| 7412 | spares++; |
| 7413 | if (rdev->raid_disk < 0 |
| 7414 | && !test_bit(Faulty, &rdev->flags)) { |
| 7415 | rdev->recovery_offset = 0; |
| 7416 | if (mddev->pers-> |
| 7417 | hot_add_disk(mddev, rdev) == 0) { |
| 7418 | if (sysfs_link_rdev(mddev, rdev)) |
| 7419 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7420 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7421 | md_new_event(mddev); |
| 7422 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7423 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7424 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7425 | } |
| 7426 | return spares; |
| 7427 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7428 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7429 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7430 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7431 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7432 | |
| 7433 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7434 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7435 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7436 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7437 | /* success...*/ |
| 7438 | /* activate any spares */ |
| 7439 | if (mddev->pers->spare_active(mddev)) |
| 7440 | sysfs_notify(&mddev->kobj, NULL, |
| 7441 | "degraded"); |
| 7442 | } |
| 7443 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7444 | mddev->pers->finish_reshape) |
| 7445 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7446 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7447 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7448 | * information must be scrapped. Also if any device is now |
| 7449 | * In_sync we must scrape the saved_raid_disk for that device |
| 7450 | * do the superblock for an incrementally recovered device |
| 7451 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7452 | */ |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7453 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7454 | if (!mddev->degraded || |
| 7455 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7456 | rdev->saved_raid_disk = -1; |
| 7457 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7458 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7459 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7460 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7461 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7462 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7463 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7464 | /* flag recovery needed just to double check */ |
| 7465 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7466 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7467 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7468 | if (mddev->event_work.func) |
| 7469 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7470 | } |
| 7471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | /* |
| 7473 | * This routine is regularly called by all per-raid-array threads to |
| 7474 | * deal with generic issues like resync and super-block update. |
| 7475 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7476 | * need this as they never do any recovery or update the superblock. |
| 7477 | * |
| 7478 | * It does not do any resync itself, but rather "forks" off other threads |
| 7479 | * to do that as needed. |
| 7480 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7481 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7482 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7483 | * and wakeups up this thread which will reap the thread and finish up. |
| 7484 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7485 | * |
| 7486 | * The overall approach is: |
| 7487 | * 1/ if the superblock needs updating, update it. |
| 7488 | * 2/ If a recovery thread is running, don't do anything else. |
| 7489 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7490 | * 4/ If there are any faulty devices, remove them. |
| 7491 | * 5/ If array is degraded, try to add spares devices |
| 7492 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7493 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7494 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7495 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7496 | if (mddev->suspended) |
| 7497 | return; |
| 7498 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7499 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7500 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7501 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7502 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7503 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7504 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7505 | mdname(mddev)); |
| 7506 | mddev->safemode = 2; |
| 7507 | } |
| 7508 | flush_signals(current); |
| 7509 | } |
| 7510 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7511 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7512 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7514 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7516 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7517 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7518 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7519 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | )) |
| 7521 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7522 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7523 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7524 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7525 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7526 | if (mddev->ro) { |
| 7527 | /* Only thing we do on a ro array is remove |
| 7528 | * failed devices. |
| 7529 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7530 | struct md_rdev *rdev; |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7531 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7532 | if (rdev->raid_disk >= 0 && |
| 7533 | !test_bit(Blocked, &rdev->flags) && |
| 7534 | test_bit(Faulty, &rdev->flags) && |
| 7535 | atomic_read(&rdev->nr_pending)==0) { |
| 7536 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7537 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7538 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7539 | rdev->raid_disk = -1; |
| 7540 | } |
| 7541 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7542 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7543 | goto unlock; |
| 7544 | } |
| 7545 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7546 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7547 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7548 | spin_lock_irq(&mddev->write_lock); |
| 7549 | if (mddev->safemode && |
| 7550 | !atomic_read(&mddev->writes_pending) && |
| 7551 | !mddev->in_sync && |
| 7552 | mddev->recovery_cp == MaxSector) { |
| 7553 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7554 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7555 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7556 | } |
| 7557 | if (mddev->safemode == 1) |
| 7558 | mddev->safemode = 0; |
| 7559 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7560 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7561 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7562 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7563 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7564 | if (mddev->flags) |
| 7565 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7567 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7568 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7569 | /* resync/recovery still happening */ |
| 7570 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7571 | goto unlock; |
| 7572 | } |
| 7573 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7574 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7575 | goto unlock; |
| 7576 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7577 | /* Set RUNNING before clearing NEEDED to avoid |
| 7578 | * any transients in the value of "sync_action". |
| 7579 | */ |
| 7580 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7581 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7582 | /* Clear some bits that don't mean anything, but |
| 7583 | * might be left set |
| 7584 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7585 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7586 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7587 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7588 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7589 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7590 | /* no recovery is running. |
| 7591 | * remove any failed drives, then |
| 7592 | * add spares if possible. |
| 7593 | * Spare are also removed and re-added, to allow |
| 7594 | * the personality to fail the re-add. |
| 7595 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7596 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7597 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7598 | if (mddev->pers->check_reshape == NULL || |
| 7599 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7600 | /* Cannot proceed */ |
| 7601 | goto unlock; |
| 7602 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7603 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7604 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7605 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7606 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7607 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7608 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7609 | } else if (mddev->recovery_cp < MaxSector) { |
| 7610 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7611 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7612 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7613 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7614 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7616 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7617 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7618 | /* We are adding a device or devices to an array |
| 7619 | * which has the bitmap stored on all devices. |
| 7620 | * So make sure all bitmap pages get written |
| 7621 | */ |
| 7622 | bitmap_write_all(mddev->bitmap); |
| 7623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7624 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7625 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7626 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7627 | if (!mddev->sync_thread) { |
| 7628 | printk(KERN_ERR "%s: could not start resync" |
| 7629 | " thread...\n", |
| 7630 | mdname(mddev)); |
| 7631 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7632 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7633 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7634 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7635 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7636 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7637 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7638 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7639 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7640 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7641 | } |
| 7642 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7643 | if (!mddev->sync_thread) { |
| 7644 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7645 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7646 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7647 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7648 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7650 | mddev_unlock(mddev); |
| 7651 | } |
| 7652 | } |
| 7653 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7654 | 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] | 7655 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7656 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7657 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7658 | !test_bit(Blocked, &rdev->flags) && |
| 7659 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7660 | msecs_to_jiffies(5000)); |
| 7661 | rdev_dec_pending(rdev, mddev); |
| 7662 | } |
| 7663 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7664 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7665 | |
| 7666 | /* Bad block management. |
| 7667 | * We can record which blocks on each device are 'bad' and so just |
| 7668 | * fail those blocks, or that stripe, rather than the whole device. |
| 7669 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7670 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7671 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7672 | * A 'shift' can be set so that larger blocks are tracked and |
| 7673 | * consequently larger devices can be covered. |
| 7674 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7675 | * |
| 7676 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7677 | * might need to retry if it is very unlucky. |
| 7678 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7679 | * so we use the write_seqlock_irq variant. |
| 7680 | * |
| 7681 | * When looking for a bad block we specify a range and want to |
| 7682 | * know if any block in the range is bad. So we binary-search |
| 7683 | * to the last range that starts at-or-before the given endpoint, |
| 7684 | * (or "before the sector after the target range") |
| 7685 | * then see if it ends after the given start. |
| 7686 | * We return |
| 7687 | * 0 if there are no known bad blocks in the range |
| 7688 | * 1 if there are known bad block which are all acknowledged |
| 7689 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7690 | * plus the start/length of the first bad section we overlap. |
| 7691 | */ |
| 7692 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7693 | sector_t *first_bad, int *bad_sectors) |
| 7694 | { |
| 7695 | int hi; |
| 7696 | int lo = 0; |
| 7697 | u64 *p = bb->page; |
| 7698 | int rv = 0; |
| 7699 | sector_t target = s + sectors; |
| 7700 | unsigned seq; |
| 7701 | |
| 7702 | if (bb->shift > 0) { |
| 7703 | /* round the start down, and the end up */ |
| 7704 | s >>= bb->shift; |
| 7705 | target += (1<<bb->shift) - 1; |
| 7706 | target >>= bb->shift; |
| 7707 | sectors = target - s; |
| 7708 | } |
| 7709 | /* 'target' is now the first block after the bad range */ |
| 7710 | |
| 7711 | retry: |
| 7712 | seq = read_seqbegin(&bb->lock); |
| 7713 | |
| 7714 | hi = bb->count; |
| 7715 | |
| 7716 | /* Binary search between lo and hi for 'target' |
| 7717 | * i.e. for the last range that starts before 'target' |
| 7718 | */ |
| 7719 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7720 | * are known not to be the last range before target. |
| 7721 | * VARIANT: hi-lo is the number of possible |
| 7722 | * ranges, and decreases until it reaches 1 |
| 7723 | */ |
| 7724 | while (hi - lo > 1) { |
| 7725 | int mid = (lo + hi) / 2; |
| 7726 | sector_t a = BB_OFFSET(p[mid]); |
| 7727 | if (a < target) |
| 7728 | /* This could still be the one, earlier ranges |
| 7729 | * could not. */ |
| 7730 | lo = mid; |
| 7731 | else |
| 7732 | /* This and later ranges are definitely out. */ |
| 7733 | hi = mid; |
| 7734 | } |
| 7735 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7736 | if (hi > lo) { |
| 7737 | /* need to check all range that end after 's' to see if |
| 7738 | * any are unacknowledged. |
| 7739 | */ |
| 7740 | while (lo >= 0 && |
| 7741 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7742 | if (BB_OFFSET(p[lo]) < target) { |
| 7743 | /* starts before the end, and finishes after |
| 7744 | * the start, so they must overlap |
| 7745 | */ |
| 7746 | if (rv != -1 && BB_ACK(p[lo])) |
| 7747 | rv = 1; |
| 7748 | else |
| 7749 | rv = -1; |
| 7750 | *first_bad = BB_OFFSET(p[lo]); |
| 7751 | *bad_sectors = BB_LEN(p[lo]); |
| 7752 | } |
| 7753 | lo--; |
| 7754 | } |
| 7755 | } |
| 7756 | |
| 7757 | if (read_seqretry(&bb->lock, seq)) |
| 7758 | goto retry; |
| 7759 | |
| 7760 | return rv; |
| 7761 | } |
| 7762 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7763 | |
| 7764 | /* |
| 7765 | * Add a range of bad blocks to the table. |
| 7766 | * This might extend the table, or might contract it |
| 7767 | * if two adjacent ranges can be merged. |
| 7768 | * We binary-search to find the 'insertion' point, then |
| 7769 | * decide how best to handle it. |
| 7770 | */ |
| 7771 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7772 | int acknowledged) |
| 7773 | { |
| 7774 | u64 *p; |
| 7775 | int lo, hi; |
| 7776 | int rv = 1; |
| 7777 | |
| 7778 | if (bb->shift < 0) |
| 7779 | /* badblocks are disabled */ |
| 7780 | return 0; |
| 7781 | |
| 7782 | if (bb->shift) { |
| 7783 | /* round the start down, and the end up */ |
| 7784 | sector_t next = s + sectors; |
| 7785 | s >>= bb->shift; |
| 7786 | next += (1<<bb->shift) - 1; |
| 7787 | next >>= bb->shift; |
| 7788 | sectors = next - s; |
| 7789 | } |
| 7790 | |
| 7791 | write_seqlock_irq(&bb->lock); |
| 7792 | |
| 7793 | p = bb->page; |
| 7794 | lo = 0; |
| 7795 | hi = bb->count; |
| 7796 | /* Find the last range that starts at-or-before 's' */ |
| 7797 | while (hi - lo > 1) { |
| 7798 | int mid = (lo + hi) / 2; |
| 7799 | sector_t a = BB_OFFSET(p[mid]); |
| 7800 | if (a <= s) |
| 7801 | lo = mid; |
| 7802 | else |
| 7803 | hi = mid; |
| 7804 | } |
| 7805 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7806 | hi = lo; |
| 7807 | |
| 7808 | if (hi > lo) { |
| 7809 | /* we found a range that might merge with the start |
| 7810 | * of our new range |
| 7811 | */ |
| 7812 | sector_t a = BB_OFFSET(p[lo]); |
| 7813 | sector_t e = a + BB_LEN(p[lo]); |
| 7814 | int ack = BB_ACK(p[lo]); |
| 7815 | if (e >= s) { |
| 7816 | /* Yes, we can merge with a previous range */ |
| 7817 | if (s == a && s + sectors >= e) |
| 7818 | /* new range covers old */ |
| 7819 | ack = acknowledged; |
| 7820 | else |
| 7821 | ack = ack && acknowledged; |
| 7822 | |
| 7823 | if (e < s + sectors) |
| 7824 | e = s + sectors; |
| 7825 | if (e - a <= BB_MAX_LEN) { |
| 7826 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7827 | s = e; |
| 7828 | } else { |
| 7829 | /* does not all fit in one range, |
| 7830 | * make p[lo] maximal |
| 7831 | */ |
| 7832 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7833 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7834 | s = a + BB_MAX_LEN; |
| 7835 | } |
| 7836 | sectors = e - s; |
| 7837 | } |
| 7838 | } |
| 7839 | if (sectors && hi < bb->count) { |
| 7840 | /* 'hi' points to the first range that starts after 's'. |
| 7841 | * Maybe we can merge with the start of that range */ |
| 7842 | sector_t a = BB_OFFSET(p[hi]); |
| 7843 | sector_t e = a + BB_LEN(p[hi]); |
| 7844 | int ack = BB_ACK(p[hi]); |
| 7845 | if (a <= s + sectors) { |
| 7846 | /* merging is possible */ |
| 7847 | if (e <= s + sectors) { |
| 7848 | /* full overlap */ |
| 7849 | e = s + sectors; |
| 7850 | ack = acknowledged; |
| 7851 | } else |
| 7852 | ack = ack && acknowledged; |
| 7853 | |
| 7854 | a = s; |
| 7855 | if (e - a <= BB_MAX_LEN) { |
| 7856 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7857 | s = e; |
| 7858 | } else { |
| 7859 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7860 | s = a + BB_MAX_LEN; |
| 7861 | } |
| 7862 | sectors = e - s; |
| 7863 | lo = hi; |
| 7864 | hi++; |
| 7865 | } |
| 7866 | } |
| 7867 | if (sectors == 0 && hi < bb->count) { |
| 7868 | /* we might be able to combine lo and hi */ |
| 7869 | /* Note: 's' is at the end of 'lo' */ |
| 7870 | sector_t a = BB_OFFSET(p[hi]); |
| 7871 | int lolen = BB_LEN(p[lo]); |
| 7872 | int hilen = BB_LEN(p[hi]); |
| 7873 | int newlen = lolen + hilen - (s - a); |
| 7874 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7875 | /* yes, we can combine them */ |
| 7876 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7877 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7878 | memmove(p + hi, p + hi + 1, |
| 7879 | (bb->count - hi - 1) * 8); |
| 7880 | bb->count--; |
| 7881 | } |
| 7882 | } |
| 7883 | while (sectors) { |
| 7884 | /* didn't merge (it all). |
| 7885 | * Need to add a range just before 'hi' */ |
| 7886 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7887 | /* No room for more */ |
| 7888 | rv = 0; |
| 7889 | break; |
| 7890 | } else { |
| 7891 | int this_sectors = sectors; |
| 7892 | memmove(p + hi + 1, p + hi, |
| 7893 | (bb->count - hi) * 8); |
| 7894 | bb->count++; |
| 7895 | |
| 7896 | if (this_sectors > BB_MAX_LEN) |
| 7897 | this_sectors = BB_MAX_LEN; |
| 7898 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7899 | sectors -= this_sectors; |
| 7900 | s += this_sectors; |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7905 | if (!acknowledged) |
| 7906 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7907 | write_sequnlock_irq(&bb->lock); |
| 7908 | |
| 7909 | return rv; |
| 7910 | } |
| 7911 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7912 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7913 | int acknowledged) |
| 7914 | { |
| 7915 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7916 | s + rdev->data_offset, sectors, acknowledged); |
| 7917 | if (rv) { |
| 7918 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 7919 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7920 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7921 | md_wakeup_thread(rdev->mddev->thread); |
| 7922 | } |
| 7923 | return rv; |
| 7924 | } |
| 7925 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7926 | |
| 7927 | /* |
| 7928 | * Remove a range of bad blocks from the table. |
| 7929 | * This may involve extending the table if we spilt a region, |
| 7930 | * but it must not fail. So if the table becomes full, we just |
| 7931 | * drop the remove request. |
| 7932 | */ |
| 7933 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7934 | { |
| 7935 | u64 *p; |
| 7936 | int lo, hi; |
| 7937 | sector_t target = s + sectors; |
| 7938 | int rv = 0; |
| 7939 | |
| 7940 | if (bb->shift > 0) { |
| 7941 | /* When clearing we round the start up and the end down. |
| 7942 | * This should not matter as the shift should align with |
| 7943 | * the block size and no rounding should ever be needed. |
| 7944 | * However it is better the think a block is bad when it |
| 7945 | * isn't than to think a block is not bad when it is. |
| 7946 | */ |
| 7947 | s += (1<<bb->shift) - 1; |
| 7948 | s >>= bb->shift; |
| 7949 | target >>= bb->shift; |
| 7950 | sectors = target - s; |
| 7951 | } |
| 7952 | |
| 7953 | write_seqlock_irq(&bb->lock); |
| 7954 | |
| 7955 | p = bb->page; |
| 7956 | lo = 0; |
| 7957 | hi = bb->count; |
| 7958 | /* Find the last range that starts before 'target' */ |
| 7959 | while (hi - lo > 1) { |
| 7960 | int mid = (lo + hi) / 2; |
| 7961 | sector_t a = BB_OFFSET(p[mid]); |
| 7962 | if (a < target) |
| 7963 | lo = mid; |
| 7964 | else |
| 7965 | hi = mid; |
| 7966 | } |
| 7967 | if (hi > lo) { |
| 7968 | /* p[lo] is the last range that could overlap the |
| 7969 | * current range. Earlier ranges could also overlap, |
| 7970 | * but only this one can overlap the end of the range. |
| 7971 | */ |
| 7972 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7973 | /* Partial overlap, leave the tail of this range */ |
| 7974 | int ack = BB_ACK(p[lo]); |
| 7975 | sector_t a = BB_OFFSET(p[lo]); |
| 7976 | sector_t end = a + BB_LEN(p[lo]); |
| 7977 | |
| 7978 | if (a < s) { |
| 7979 | /* we need to split this range */ |
| 7980 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7981 | rv = 0; |
| 7982 | goto out; |
| 7983 | } |
| 7984 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7985 | bb->count++; |
| 7986 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7987 | lo++; |
| 7988 | } |
| 7989 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7990 | /* there is no longer an overlap */ |
| 7991 | hi = lo; |
| 7992 | lo--; |
| 7993 | } |
| 7994 | while (lo >= 0 && |
| 7995 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7996 | /* This range does overlap */ |
| 7997 | if (BB_OFFSET(p[lo]) < s) { |
| 7998 | /* Keep the early parts of this range. */ |
| 7999 | int ack = BB_ACK(p[lo]); |
| 8000 | sector_t start = BB_OFFSET(p[lo]); |
| 8001 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8002 | /* now low doesn't overlap, so.. */ |
| 8003 | break; |
| 8004 | } |
| 8005 | lo--; |
| 8006 | } |
| 8007 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8008 | * anything between needs to be discarded |
| 8009 | */ |
| 8010 | if (hi - lo > 1) { |
| 8011 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8012 | bb->count -= (hi - lo - 1); |
| 8013 | } |
| 8014 | } |
| 8015 | |
| 8016 | bb->changed = 1; |
| 8017 | out: |
| 8018 | write_sequnlock_irq(&bb->lock); |
| 8019 | return rv; |
| 8020 | } |
| 8021 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8022 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8023 | { |
| 8024 | return md_clear_badblocks(&rdev->badblocks, |
| 8025 | s + rdev->data_offset, |
| 8026 | sectors); |
| 8027 | } |
| 8028 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8029 | |
| 8030 | /* |
| 8031 | * Acknowledge all bad blocks in a list. |
| 8032 | * This only succeeds if ->changed is clear. It is used by |
| 8033 | * in-kernel metadata updates |
| 8034 | */ |
| 8035 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8036 | { |
| 8037 | if (bb->page == NULL || bb->changed) |
| 8038 | /* no point even trying */ |
| 8039 | return; |
| 8040 | write_seqlock_irq(&bb->lock); |
| 8041 | |
| 8042 | if (bb->changed == 0) { |
| 8043 | u64 *p = bb->page; |
| 8044 | int i; |
| 8045 | for (i = 0; i < bb->count ; i++) { |
| 8046 | if (!BB_ACK(p[i])) { |
| 8047 | sector_t start = BB_OFFSET(p[i]); |
| 8048 | int len = BB_LEN(p[i]); |
| 8049 | p[i] = BB_MAKE(start, len, 1); |
| 8050 | } |
| 8051 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8052 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8053 | } |
| 8054 | write_sequnlock_irq(&bb->lock); |
| 8055 | } |
| 8056 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8057 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8058 | /* sysfs access to bad-blocks list. |
| 8059 | * We present two files. |
| 8060 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8061 | * are recorded as bad. The list is truncated to fit within |
| 8062 | * the one-page limit of sysfs. |
| 8063 | * Writing "sector length" to this file adds an acknowledged |
| 8064 | * bad block list. |
| 8065 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8066 | * been acknowledged. Writing to this file adds bad blocks |
| 8067 | * without acknowledging them. This is largely for testing. |
| 8068 | */ |
| 8069 | |
| 8070 | static ssize_t |
| 8071 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8072 | { |
| 8073 | size_t len; |
| 8074 | int i; |
| 8075 | u64 *p = bb->page; |
| 8076 | unsigned seq; |
| 8077 | |
| 8078 | if (bb->shift < 0) |
| 8079 | return 0; |
| 8080 | |
| 8081 | retry: |
| 8082 | seq = read_seqbegin(&bb->lock); |
| 8083 | |
| 8084 | len = 0; |
| 8085 | i = 0; |
| 8086 | |
| 8087 | while (len < PAGE_SIZE && i < bb->count) { |
| 8088 | sector_t s = BB_OFFSET(p[i]); |
| 8089 | unsigned int length = BB_LEN(p[i]); |
| 8090 | int ack = BB_ACK(p[i]); |
| 8091 | i++; |
| 8092 | |
| 8093 | if (unack && ack) |
| 8094 | continue; |
| 8095 | |
| 8096 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8097 | (unsigned long long)s << bb->shift, |
| 8098 | length << bb->shift); |
| 8099 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8100 | if (unack && len == 0) |
| 8101 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8102 | |
| 8103 | if (read_seqretry(&bb->lock, seq)) |
| 8104 | goto retry; |
| 8105 | |
| 8106 | return len; |
| 8107 | } |
| 8108 | |
| 8109 | #define DO_DEBUG 1 |
| 8110 | |
| 8111 | static ssize_t |
| 8112 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8113 | { |
| 8114 | unsigned long long sector; |
| 8115 | int length; |
| 8116 | char newline; |
| 8117 | #ifdef DO_DEBUG |
| 8118 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8119 | * Normally only a successful write may clear a badblock |
| 8120 | */ |
| 8121 | int clear = 0; |
| 8122 | if (page[0] == '-') { |
| 8123 | clear = 1; |
| 8124 | page++; |
| 8125 | } |
| 8126 | #endif /* DO_DEBUG */ |
| 8127 | |
| 8128 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8129 | case 3: |
| 8130 | if (newline != '\n') |
| 8131 | return -EINVAL; |
| 8132 | case 2: |
| 8133 | if (length <= 0) |
| 8134 | return -EINVAL; |
| 8135 | break; |
| 8136 | default: |
| 8137 | return -EINVAL; |
| 8138 | } |
| 8139 | |
| 8140 | #ifdef DO_DEBUG |
| 8141 | if (clear) { |
| 8142 | md_clear_badblocks(bb, sector, length); |
| 8143 | return len; |
| 8144 | } |
| 8145 | #endif /* DO_DEBUG */ |
| 8146 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8147 | return len; |
| 8148 | else |
| 8149 | return -ENOSPC; |
| 8150 | } |
| 8151 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8152 | static int md_notify_reboot(struct notifier_block *this, |
| 8153 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8154 | { |
| 8155 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8156 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8157 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8158 | |
| 8159 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 8160 | |
| 8161 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 8162 | |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8163 | for_each_mddev(mddev, tmp) { |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8164 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8165 | /* Force a switch to readonly even array |
| 8166 | * appears to still be in use. Hence |
| 8167 | * the '100'. |
| 8168 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 8169 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8170 | mddev_unlock(mddev); |
| 8171 | } |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8172 | need_delay = 1; |
| 8173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | /* |
| 8175 | * certain more exotic SCSI devices are known to be |
| 8176 | * volatile wrt too early system reboots. While the |
| 8177 | * right place to handle this issue is the given |
| 8178 | * driver, we do want to have a safe RAID driver ... |
| 8179 | */ |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8180 | if (need_delay) |
| 8181 | mdelay(1000*1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8182 | } |
| 8183 | return NOTIFY_DONE; |
| 8184 | } |
| 8185 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8186 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8187 | .notifier_call = md_notify_reboot, |
| 8188 | .next = NULL, |
| 8189 | .priority = INT_MAX, /* before any real devices */ |
| 8190 | }; |
| 8191 | |
| 8192 | static void md_geninit(void) |
| 8193 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8194 | 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] | 8195 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8196 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8197 | } |
| 8198 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8199 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8200 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8201 | int ret = -ENOMEM; |
| 8202 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8203 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8204 | if (!md_wq) |
| 8205 | goto err_wq; |
| 8206 | |
| 8207 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8208 | if (!md_misc_wq) |
| 8209 | goto err_misc_wq; |
| 8210 | |
| 8211 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8212 | goto err_md; |
| 8213 | |
| 8214 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8215 | goto err_mdp; |
| 8216 | mdp_major = ret; |
| 8217 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8218 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8219 | md_probe, NULL, NULL); |
| 8220 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8221 | md_probe, NULL, NULL); |
| 8222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8223 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8224 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8225 | |
| 8226 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8227 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8228 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8229 | err_mdp: |
| 8230 | unregister_blkdev(MD_MAJOR, "md"); |
| 8231 | err_md: |
| 8232 | destroy_workqueue(md_misc_wq); |
| 8233 | err_misc_wq: |
| 8234 | destroy_workqueue(md_wq); |
| 8235 | err_wq: |
| 8236 | return ret; |
| 8237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | |
| 8239 | #ifndef MODULE |
| 8240 | |
| 8241 | /* |
| 8242 | * Searches all registered partitions for autorun RAID arrays |
| 8243 | * at boot time. |
| 8244 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8245 | |
| 8246 | static LIST_HEAD(all_detected_devices); |
| 8247 | struct detected_devices_node { |
| 8248 | struct list_head list; |
| 8249 | dev_t dev; |
| 8250 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8251 | |
| 8252 | void md_autodetect_dev(dev_t dev) |
| 8253 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8254 | struct detected_devices_node *node_detected_dev; |
| 8255 | |
| 8256 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8257 | if (node_detected_dev) { |
| 8258 | node_detected_dev->dev = dev; |
| 8259 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8260 | } else { |
| 8261 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8262 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | } |
| 8265 | |
| 8266 | |
| 8267 | static void autostart_arrays(int part) |
| 8268 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8269 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8270 | struct detected_devices_node *node_detected_dev; |
| 8271 | dev_t dev; |
| 8272 | int i_scanned, i_passed; |
| 8273 | |
| 8274 | i_scanned = 0; |
| 8275 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8276 | |
| 8277 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8278 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8279 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8280 | i_scanned++; |
| 8281 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8282 | struct detected_devices_node, list); |
| 8283 | list_del(&node_detected_dev->list); |
| 8284 | dev = node_detected_dev->dev; |
| 8285 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8286 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8287 | if (IS_ERR(rdev)) |
| 8288 | continue; |
| 8289 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8290 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8291 | MD_BUG(); |
| 8292 | continue; |
| 8293 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8294 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8295 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8296 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8297 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8298 | |
| 8299 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8300 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8301 | |
| 8302 | autorun_devices(part); |
| 8303 | } |
| 8304 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8305 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8306 | |
| 8307 | static __exit void md_exit(void) |
| 8308 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8309 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8311 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8312 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8313 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8314 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8315 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8316 | unregister_blkdev(mdp_major, "mdp"); |
| 8317 | unregister_reboot_notifier(&md_notifier); |
| 8318 | unregister_sysctl_table(raid_table_header); |
| 8319 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8320 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8321 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8322 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8323 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8324 | destroy_workqueue(md_misc_wq); |
| 8325 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8326 | } |
| 8327 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8328 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8329 | module_exit(md_exit) |
| 8330 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8331 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8332 | { |
| 8333 | return sprintf(buffer, "%d", start_readonly); |
| 8334 | } |
| 8335 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8336 | { |
| 8337 | char *e; |
| 8338 | int num = simple_strtoul(val, &e, 10); |
| 8339 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8340 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8341 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8342 | } |
| 8343 | return -EINVAL; |
| 8344 | } |
| 8345 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8346 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8347 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8348 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8349 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8351 | EXPORT_SYMBOL(register_md_personality); |
| 8352 | EXPORT_SYMBOL(unregister_md_personality); |
| 8353 | EXPORT_SYMBOL(md_error); |
| 8354 | EXPORT_SYMBOL(md_done_sync); |
| 8355 | EXPORT_SYMBOL(md_write_start); |
| 8356 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8357 | EXPORT_SYMBOL(md_register_thread); |
| 8358 | EXPORT_SYMBOL(md_unregister_thread); |
| 8359 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8360 | EXPORT_SYMBOL(md_check_recovery); |
| 8361 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8362 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8363 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8364 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |