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> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
| 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 | */ |
| 335 | static int 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 | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 339 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 340 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 341 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 342 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 343 | if (mddev == NULL || mddev->pers == NULL |
| 344 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 345 | bio_io_error(bio); |
| 346 | return 0; |
| 347 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 348 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 349 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 350 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 351 | DEFINE_WAIT(__wait); |
| 352 | for (;;) { |
| 353 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 354 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 355 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 356 | break; |
| 357 | rcu_read_unlock(); |
| 358 | schedule(); |
| 359 | rcu_read_lock(); |
| 360 | } |
| 361 | finish_wait(&mddev->sb_wait, &__wait); |
| 362 | } |
| 363 | atomic_inc(&mddev->active_io); |
| 364 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 365 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 366 | /* |
| 367 | * save the sectors now since our bio can |
| 368 | * go away inside make_request |
| 369 | */ |
| 370 | sectors = bio_sectors(bio); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 371 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 372 | |
| 373 | cpu = part_stat_lock(); |
| 374 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 375 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 376 | part_stat_unlock(); |
| 377 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 378 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 379 | wake_up(&mddev->sb_wait); |
| 380 | |
| 381 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 384 | /* mddev_suspend makes sure no new requests are submitted |
| 385 | * to the device, and that any requests that have been submitted |
| 386 | * are completely handled. |
| 387 | * Once ->stop is called and completes, the module will be completely |
| 388 | * unused. |
| 389 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 390 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 391 | { |
| 392 | BUG_ON(mddev->suspended); |
| 393 | mddev->suspended = 1; |
| 394 | synchronize_rcu(); |
| 395 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 396 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 397 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 398 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 399 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 400 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 401 | { |
| 402 | mddev->suspended = 0; |
| 403 | wake_up(&mddev->sb_wait); |
| 404 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 405 | |
| 406 | md_wakeup_thread(mddev->thread); |
| 407 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 408 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 409 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 410 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 411 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 412 | { |
| 413 | return mddev->suspended; |
| 414 | } |
| 415 | EXPORT_SYMBOL(mddev_congested); |
| 416 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 418 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | */ |
| 420 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 423 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 424 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | |
| 426 | rdev_dec_pending(rdev, mddev); |
| 427 | |
| 428 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 429 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 430 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
| 432 | bio_put(bio); |
| 433 | } |
| 434 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 435 | static void md_submit_flush_data(struct work_struct *ws); |
| 436 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 437 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 439 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 440 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 442 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 443 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | rcu_read_lock(); |
| 445 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 446 | if (rdev->raid_disk >= 0 && |
| 447 | !test_bit(Faulty, &rdev->flags)) { |
| 448 | /* Take two references, one is dropped |
| 449 | * when request finishes, one after |
| 450 | * we reclaim rcu_read_lock |
| 451 | */ |
| 452 | struct bio *bi; |
| 453 | atomic_inc(&rdev->nr_pending); |
| 454 | atomic_inc(&rdev->nr_pending); |
| 455 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 456 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 457 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 458 | bi->bi_private = rdev; |
| 459 | bi->bi_bdev = rdev->bdev; |
| 460 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 461 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 462 | rcu_read_lock(); |
| 463 | rdev_dec_pending(rdev, mddev); |
| 464 | } |
| 465 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 466 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 467 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 468 | } |
| 469 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 470 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 471 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 472 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 473 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 475 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 476 | /* an empty barrier - all done */ |
| 477 | bio_endio(bio, 0); |
| 478 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 479 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 480 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 481 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 482 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 483 | |
| 484 | mddev->flush_bio = NULL; |
| 485 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 486 | } |
| 487 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 488 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 489 | { |
| 490 | spin_lock_irq(&mddev->write_lock); |
| 491 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 492 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 493 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 494 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 495 | spin_unlock_irq(&mddev->write_lock); |
| 496 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 497 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 498 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 499 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 500 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 501 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 502 | /* Support for plugging. |
| 503 | * This mirrors the plugging support in request_queue, but does not |
| 504 | * require having a whole queue or request structures. |
| 505 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 506 | * plugged on. This links tot the private plug_handle structure in the |
| 507 | * personality data where we keep a count of the number of outstanding |
| 508 | * plugs so other code can see if a plug is active. |
| 509 | */ |
| 510 | struct md_plug_cb { |
| 511 | struct blk_plug_cb cb; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 512 | struct mddev *mddev; |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 516 | { |
| 517 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 518 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 519 | md_wakeup_thread(mdcb->mddev->thread); |
| 520 | kfree(mdcb); |
| 521 | } |
| 522 | |
| 523 | /* Check that an unplug wakeup will come shortly. |
| 524 | * If not, wakeup the md thread immediately |
| 525 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 526 | int mddev_check_plugged(struct mddev *mddev) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 527 | { |
| 528 | struct blk_plug *plug = current->plug; |
| 529 | struct md_plug_cb *mdcb; |
| 530 | |
| 531 | if (!plug) |
| 532 | return 0; |
| 533 | |
| 534 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 535 | if (mdcb->cb.callback == plugger_unplug && |
| 536 | mdcb->mddev == mddev) { |
| 537 | /* Already on the list, move to top */ |
| 538 | if (mdcb != list_first_entry(&plug->cb_list, |
| 539 | struct md_plug_cb, |
| 540 | cb.list)) |
| 541 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 542 | return 1; |
| 543 | } |
| 544 | } |
| 545 | /* Not currently on the callback list */ |
| 546 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 547 | if (!mdcb) |
| 548 | return 0; |
| 549 | |
| 550 | mdcb->mddev = mddev; |
| 551 | mdcb->cb.callback = plugger_unplug; |
| 552 | atomic_inc(&mddev->plug_cnt); |
| 553 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 554 | return 1; |
| 555 | } |
| 556 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 557 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 558 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | { |
| 560 | atomic_inc(&mddev->active); |
| 561 | return mddev; |
| 562 | } |
| 563 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 564 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 565 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 566 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 568 | struct bio_set *bs = NULL; |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 571 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 572 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 573 | mddev->ctime == 0 && !mddev->hold_active) { |
| 574 | /* Array is not configured at all, and not held active, |
| 575 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 577 | bs = mddev->bio_set; |
| 578 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 579 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 580 | /* We did a probe so need to clean up. Call |
| 581 | * queue_work inside the spinlock so that |
| 582 | * flush_workqueue() after mddev_find will |
| 583 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 584 | */ |
| 585 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 586 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 587 | } else |
| 588 | kfree(mddev); |
| 589 | } |
| 590 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 591 | if (bs) |
| 592 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 595 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 596 | { |
| 597 | mutex_init(&mddev->open_mutex); |
| 598 | mutex_init(&mddev->reconfig_mutex); |
| 599 | mutex_init(&mddev->bitmap_info.mutex); |
| 600 | INIT_LIST_HEAD(&mddev->disks); |
| 601 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 602 | init_timer(&mddev->safemode_timer); |
| 603 | atomic_set(&mddev->active, 1); |
| 604 | atomic_set(&mddev->openers, 0); |
| 605 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 606 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 607 | spin_lock_init(&mddev->write_lock); |
| 608 | atomic_set(&mddev->flush_pending, 0); |
| 609 | init_waitqueue_head(&mddev->sb_wait); |
| 610 | init_waitqueue_head(&mddev->recovery_wait); |
| 611 | mddev->reshape_position = MaxSector; |
| 612 | mddev->resync_min = 0; |
| 613 | mddev->resync_max = MaxSector; |
| 614 | mddev->level = LEVEL_NONE; |
| 615 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 616 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 617 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 618 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 620 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 622 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 623 | unit &= ~((1<<MdpMinorShift)-1); |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | retry: |
| 626 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 628 | if (unit) { |
| 629 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 630 | if (mddev->unit == unit) { |
| 631 | mddev_get(mddev); |
| 632 | spin_unlock(&all_mddevs_lock); |
| 633 | kfree(new); |
| 634 | return mddev; |
| 635 | } |
| 636 | |
| 637 | if (new) { |
| 638 | list_add(&new->all_mddevs, &all_mddevs); |
| 639 | spin_unlock(&all_mddevs_lock); |
| 640 | new->hold_active = UNTIL_IOCTL; |
| 641 | return new; |
| 642 | } |
| 643 | } else if (new) { |
| 644 | /* find an unused unit number */ |
| 645 | static int next_minor = 512; |
| 646 | int start = next_minor; |
| 647 | int is_free = 0; |
| 648 | int dev = 0; |
| 649 | while (!is_free) { |
| 650 | dev = MKDEV(MD_MAJOR, next_minor); |
| 651 | next_minor++; |
| 652 | if (next_minor > MINORMASK) |
| 653 | next_minor = 0; |
| 654 | if (next_minor == start) { |
| 655 | /* Oh dear, all in use. */ |
| 656 | spin_unlock(&all_mddevs_lock); |
| 657 | kfree(new); |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | is_free = 1; |
| 662 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 663 | if (mddev->unit == dev) { |
| 664 | is_free = 0; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | new->unit = dev; |
| 669 | new->md_minor = MINOR(dev); |
| 670 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | list_add(&new->all_mddevs, &all_mddevs); |
| 672 | spin_unlock(&all_mddevs_lock); |
| 673 | return new; |
| 674 | } |
| 675 | spin_unlock(&all_mddevs_lock); |
| 676 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 677 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (!new) |
| 679 | return NULL; |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | new->unit = unit; |
| 682 | if (MAJOR(unit) == MD_MAJOR) |
| 683 | new->md_minor = MINOR(unit); |
| 684 | else |
| 685 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 686 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 687 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | goto retry; |
| 690 | } |
| 691 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 692 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 694 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 697 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 698 | { |
| 699 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 700 | } |
| 701 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 702 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 704 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 707 | static struct attribute_group md_redundancy_group; |
| 708 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 709 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 711 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 712 | /* These cannot be removed under reconfig_mutex as |
| 713 | * an access to the files will try to take reconfig_mutex |
| 714 | * while holding the file unremovable, which leads to |
| 715 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 716 | * So hold set sysfs_active while the remove in happeing, |
| 717 | * and anything else which might set ->to_remove or my |
| 718 | * otherwise change the sysfs namespace will fail with |
| 719 | * -EBUSY if sysfs_active is still set. |
| 720 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 721 | * test it under the same mutex to ensure its correct value |
| 722 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 723 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 724 | struct attribute_group *to_remove = mddev->to_remove; |
| 725 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 726 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 727 | mutex_unlock(&mddev->reconfig_mutex); |
| 728 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 729 | if (mddev->kobj.sd) { |
| 730 | if (to_remove != &md_redundancy_group) |
| 731 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 732 | if (mddev->pers == NULL || |
| 733 | mddev->pers->sync_request == NULL) { |
| 734 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 735 | if (mddev->sysfs_action) |
| 736 | sysfs_put(mddev->sysfs_action); |
| 737 | mddev->sysfs_action = NULL; |
| 738 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 739 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 740 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 741 | } else |
| 742 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 744 | /* was we've dropped the mutex we need a spinlock to |
| 745 | * make sur the thread doesn't disappear |
| 746 | */ |
| 747 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 748 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 749 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 752 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 754 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 756 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | if (rdev->desc_nr == nr) |
| 758 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | return NULL; |
| 761 | } |
| 762 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 763 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 765 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 767 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | if (rdev->bdev->bd_dev == dev) |
| 769 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return NULL; |
| 772 | } |
| 773 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 774 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 775 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 776 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 777 | list_for_each_entry(pers, &pers_list, list) { |
| 778 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 779 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 780 | if (strcmp(pers->name, clevel)==0) |
| 781 | return pers; |
| 782 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
| 785 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 786 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 787 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 789 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 790 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 793 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
| 795 | if (rdev->sb_page) |
| 796 | MD_BUG(); |
| 797 | |
| 798 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 799 | if (!rdev->sb_page) { |
| 800 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 801 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 807 | static void free_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
| 809 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 810 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | rdev->sb_loaded = 0; |
| 812 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 813 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 814 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 816 | if (rdev->bb_page) { |
| 817 | put_page(rdev->bb_page); |
| 818 | rdev->bb_page = NULL; |
| 819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 823 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 824 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 825 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 826 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 827 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 828 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 829 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 830 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 831 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 832 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 833 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 834 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 835 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 836 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 837 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 838 | } |
| 839 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 840 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 841 | sector_t sector, int size, struct page *page) |
| 842 | { |
| 843 | /* write first size bytes of page to sector of rdev |
| 844 | * Increment mddev->pending_writes before returning |
| 845 | * and decrement it on completion, waking up sb_wait |
| 846 | * if zero is reached. |
| 847 | * If an error occurred, call md_error |
| 848 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 849 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 850 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 851 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 852 | bio->bi_sector = sector; |
| 853 | bio_add_page(bio, page, size, 0); |
| 854 | bio->bi_private = rdev; |
| 855 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 856 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 857 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 858 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 859 | } |
| 860 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 861 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 862 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 863 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 864 | DEFINE_WAIT(wq); |
| 865 | for(;;) { |
| 866 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 867 | if (atomic_read(&mddev->pending_writes)==0) |
| 868 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 869 | schedule(); |
| 870 | } |
| 871 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 872 | } |
| 873 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 874 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 879 | 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] | 880 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 882 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | struct completion event; |
| 884 | int ret; |
| 885 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 886 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 888 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 889 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 890 | if (metadata_op) |
| 891 | bio->bi_sector = sector + rdev->sb_start; |
| 892 | else |
| 893 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | bio_add_page(bio, page, size, 0); |
| 895 | init_completion(&event); |
| 896 | bio->bi_private = &event; |
| 897 | bio->bi_end_io = bi_complete; |
| 898 | submit_bio(rw, bio); |
| 899 | wait_for_completion(&event); |
| 900 | |
| 901 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 902 | bio_put(bio); |
| 903 | return ret; |
| 904 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 907 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
| 909 | char b[BDEVNAME_SIZE]; |
| 910 | if (!rdev->sb_page) { |
| 911 | MD_BUG(); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | if (rdev->sb_loaded) |
| 915 | return 0; |
| 916 | |
| 917 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 918 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | goto fail; |
| 920 | rdev->sb_loaded = 1; |
| 921 | return 0; |
| 922 | |
| 923 | fail: |
| 924 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 925 | bdevname(rdev->bdev,b)); |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | |
| 929 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 930 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 931 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 932 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 933 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 934 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 938 | { |
| 939 | int ret; |
| 940 | mdp_super_t *tmp1, *tmp2; |
| 941 | |
| 942 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 943 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 944 | |
| 945 | if (!tmp1 || !tmp2) { |
| 946 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 947 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | goto abort; |
| 949 | } |
| 950 | |
| 951 | *tmp1 = *sb1; |
| 952 | *tmp2 = *sb2; |
| 953 | |
| 954 | /* |
| 955 | * nr_disks is not constant |
| 956 | */ |
| 957 | tmp1->nr_disks = 0; |
| 958 | tmp2->nr_disks = 0; |
| 959 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 960 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 962 | kfree(tmp1); |
| 963 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return ret; |
| 965 | } |
| 966 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 967 | |
| 968 | static u32 md_csum_fold(u32 csum) |
| 969 | { |
| 970 | csum = (csum & 0xffff) + (csum >> 16); |
| 971 | return (csum & 0xffff) + (csum >> 16); |
| 972 | } |
| 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 975 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 976 | u64 newcsum = 0; |
| 977 | u32 *sb32 = (u32*)sb; |
| 978 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | unsigned int disk_csum, csum; |
| 980 | |
| 981 | disk_csum = sb->sb_csum; |
| 982 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 983 | |
| 984 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 985 | newcsum += sb32[i]; |
| 986 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 987 | |
| 988 | |
| 989 | #ifdef CONFIG_ALPHA |
| 990 | /* This used to use csum_partial, which was wrong for several |
| 991 | * reasons including that different results are returned on |
| 992 | * different architectures. It isn't critical that we get exactly |
| 993 | * the same return value as before (we always csum_fold before |
| 994 | * testing, and that removes any differences). However as we |
| 995 | * know that csum_partial always returned a 16bit value on |
| 996 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 997 | */ |
| 998 | sb->sb_csum = md_csum_fold(disk_csum); |
| 999 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1001 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | return csum; |
| 1003 | } |
| 1004 | |
| 1005 | |
| 1006 | /* |
| 1007 | * Handle superblock details. |
| 1008 | * We want to be able to handle multiple superblock formats |
| 1009 | * so we have a common interface to them all, and an array of |
| 1010 | * different handlers. |
| 1011 | * We rely on user-space to write the initial superblock, and support |
| 1012 | * reading and updating of superblocks. |
| 1013 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1014 | * 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] | 1015 | * loads and validates a superblock on dev. |
| 1016 | * if refdev != NULL, compare superblocks on both devices |
| 1017 | * Return: |
| 1018 | * 0 - dev has a superblock that is compatible with refdev |
| 1019 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1020 | * so dev should be used as the refdev in future |
| 1021 | * -EINVAL superblock incompatible or invalid |
| 1022 | * -othererror e.g. -EIO |
| 1023 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1024 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | * Verify that dev is acceptable into mddev. |
| 1026 | * The first time, mddev->raid_disks will be 0, and data from |
| 1027 | * dev should be merged in. Subsequent calls check that dev |
| 1028 | * is new enough. Return 0 or -EINVAL |
| 1029 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1030 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * Update the superblock for rdev with data in mddev |
| 1032 | * This does not write to disc. |
| 1033 | * |
| 1034 | */ |
| 1035 | |
| 1036 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1037 | char *name; |
| 1038 | struct module *owner; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1039 | int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1040 | int minor_version); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1041 | int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev); |
| 1042 | void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1043 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1044 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | }; |
| 1046 | |
| 1047 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1048 | * Check that the given mddev has no bitmap. |
| 1049 | * |
| 1050 | * This function is called from the run method of all personalities that do not |
| 1051 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1052 | * has a bitmap. Otherwise, it returns 0. |
| 1053 | * |
| 1054 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1055 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1056 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1057 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1058 | return 0; |
| 1059 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1060 | mdname(mddev), mddev->pers->name); |
| 1061 | return 1; |
| 1062 | } |
| 1063 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1064 | |
| 1065 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | * load_super for 0.90.0 |
| 1067 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1068 | 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] | 1069 | { |
| 1070 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1071 | mdp_super_t *sb; |
| 1072 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1075 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * it's at the end of the disk. |
| 1077 | * |
| 1078 | * It also happens to be a multiple of 4Kb. |
| 1079 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1080 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1082 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if (ret) return ret; |
| 1084 | |
| 1085 | ret = -EINVAL; |
| 1086 | |
| 1087 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1088 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1091 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1092 | b); |
| 1093 | goto abort; |
| 1094 | } |
| 1095 | |
| 1096 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1097 | sb->minor_version < 90 || |
| 1098 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1100 | sb->major_version, sb->minor_version, |
| 1101 | b); |
| 1102 | goto abort; |
| 1103 | } |
| 1104 | |
| 1105 | if (sb->raid_disks <= 0) |
| 1106 | goto abort; |
| 1107 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1108 | 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] | 1109 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1110 | b); |
| 1111 | goto abort; |
| 1112 | } |
| 1113 | |
| 1114 | rdev->preferred_minor = sb->md_minor; |
| 1115 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1116 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1117 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (sb->level == LEVEL_MULTIPATH) |
| 1120 | rdev->desc_nr = -1; |
| 1121 | else |
| 1122 | rdev->desc_nr = sb->this_disk.number; |
| 1123 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1124 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1126 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1128 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (!uuid_equal(refsb, sb)) { |
| 1130 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1131 | b, bdevname(refdev->bdev,b2)); |
| 1132 | goto abort; |
| 1133 | } |
| 1134 | if (!sb_equal(refsb, sb)) { |
| 1135 | printk(KERN_WARNING "md: %s has same UUID" |
| 1136 | " but different superblock to %s\n", |
| 1137 | b, bdevname(refdev->bdev, b2)); |
| 1138 | goto abort; |
| 1139 | } |
| 1140 | ev1 = md_event(sb); |
| 1141 | ev2 = md_event(refsb); |
| 1142 | if (ev1 > ev2) |
| 1143 | ret = 1; |
| 1144 | else |
| 1145 | ret = 0; |
| 1146 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1147 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1148 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1149 | if (rdev->sectors >= (2ULL << 32)) |
| 1150 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1152 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1153 | /* "this cannot possibly happen" ... */ |
| 1154 | ret = -EINVAL; |
| 1155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | abort: |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * validate_super for 0.90.0 |
| 1162 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1163 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
| 1165 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1166 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1167 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1169 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1170 | clear_bit(Faulty, &rdev->flags); |
| 1171 | clear_bit(In_sync, &rdev->flags); |
| 1172 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | if (mddev->raid_disks == 0) { |
| 1175 | mddev->major_version = 0; |
| 1176 | mddev->minor_version = sb->minor_version; |
| 1177 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1178 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1179 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | mddev->ctime = sb->ctime; |
| 1181 | mddev->utime = sb->utime; |
| 1182 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1183 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | mddev->layout = sb->layout; |
| 1185 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1186 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1187 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1188 | mddev->bitmap_info.offset = 0; |
| 1189 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1191 | if (mddev->minor_version >= 91) { |
| 1192 | mddev->reshape_position = sb->reshape_position; |
| 1193 | mddev->delta_disks = sb->delta_disks; |
| 1194 | mddev->new_level = sb->new_level; |
| 1195 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1196 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1197 | } else { |
| 1198 | mddev->reshape_position = MaxSector; |
| 1199 | mddev->delta_disks = 0; |
| 1200 | mddev->new_level = mddev->level; |
| 1201 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1202 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1206 | mddev->recovery_cp = MaxSector; |
| 1207 | else { |
| 1208 | if (sb->events_hi == sb->cp_events_hi && |
| 1209 | sb->events_lo == sb->cp_events_lo) { |
| 1210 | mddev->recovery_cp = sb->recovery_cp; |
| 1211 | } else |
| 1212 | mddev->recovery_cp = 0; |
| 1213 | } |
| 1214 | |
| 1215 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1216 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1217 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1218 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1219 | |
| 1220 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1221 | |
| 1222 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1223 | mddev->bitmap_info.file == NULL) |
| 1224 | mddev->bitmap_info.offset = |
| 1225 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1226 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1227 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1228 | /* Insist on good event counter while assembling, except |
| 1229 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1231 | if (sb->disks[rdev->desc_nr].state & ( |
| 1232 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1233 | if (ev1 < mddev->events) |
| 1234 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1235 | } else if (mddev->bitmap) { |
| 1236 | /* if adding to array with a bitmap, then we can accept an |
| 1237 | * older device ... but not too old. |
| 1238 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1239 | if (ev1 < mddev->bitmap->events_cleared) |
| 1240 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1241 | } else { |
| 1242 | if (ev1 < mddev->events) |
| 1243 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1244 | return 0; |
| 1245 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | desc = sb->disks + rdev->desc_nr; |
| 1249 | |
| 1250 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1251 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1252 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1253 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1254 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1256 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1257 | /* active but not in sync implies recovery up to |
| 1258 | * reshape position. We don't know exactly where |
| 1259 | * that is, so set to zero for now */ |
| 1260 | if (mddev->minor_version >= 91) { |
| 1261 | rdev->recovery_offset = 0; |
| 1262 | rdev->raid_disk = desc->raid_disk; |
| 1263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1265 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1266 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1267 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1268 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * sync_super for 0.90.0 |
| 1274 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1275 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | { |
| 1277 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1278 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
| 1282 | /* make rdev->sb match mddev data.. |
| 1283 | * |
| 1284 | * 1/ zero out disks |
| 1285 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1286 | * 3/ any empty disks < next_spare become removed |
| 1287 | * |
| 1288 | * disks[0] gets initialised to REMOVED because |
| 1289 | * we cannot be sure from other fields if it has |
| 1290 | * been initialised or not. |
| 1291 | */ |
| 1292 | int i; |
| 1293 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1294 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1295 | rdev->sb_size = MD_SB_BYTES; |
| 1296 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1297 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
| 1299 | memset(sb, 0, sizeof(*sb)); |
| 1300 | |
| 1301 | sb->md_magic = MD_SB_MAGIC; |
| 1302 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | sb->patch_version = mddev->patch_version; |
| 1304 | sb->gvalid_words = 0; /* ignored */ |
| 1305 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1306 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1307 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1308 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1309 | |
| 1310 | sb->ctime = mddev->ctime; |
| 1311 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1312 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | sb->raid_disks = mddev->raid_disks; |
| 1314 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1315 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | sb->utime = mddev->utime; |
| 1317 | sb->state = 0; |
| 1318 | sb->events_hi = (mddev->events>>32); |
| 1319 | sb->events_lo = (u32)mddev->events; |
| 1320 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1321 | if (mddev->reshape_position == MaxSector) |
| 1322 | sb->minor_version = 90; |
| 1323 | else { |
| 1324 | sb->minor_version = 91; |
| 1325 | sb->reshape_position = mddev->reshape_position; |
| 1326 | sb->new_level = mddev->new_level; |
| 1327 | sb->delta_disks = mddev->delta_disks; |
| 1328 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1329 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1330 | } |
| 1331 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | if (mddev->in_sync) |
| 1333 | { |
| 1334 | sb->recovery_cp = mddev->recovery_cp; |
| 1335 | sb->cp_events_hi = (mddev->events>>32); |
| 1336 | sb->cp_events_lo = (u32)mddev->events; |
| 1337 | if (mddev->recovery_cp == MaxSector) |
| 1338 | sb->state = (1<< MD_SB_CLEAN); |
| 1339 | } else |
| 1340 | sb->recovery_cp = 0; |
| 1341 | |
| 1342 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1343 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1345 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1346 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1349 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1351 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1352 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1353 | |
| 1354 | if (rdev2->raid_disk >= 0 && |
| 1355 | sb->minor_version >= 91) |
| 1356 | /* we have nowhere to store the recovery_offset, |
| 1357 | * but if it is not below the reshape_position, |
| 1358 | * we can piggy-back on that. |
| 1359 | */ |
| 1360 | is_active = 1; |
| 1361 | if (rdev2->raid_disk < 0 || |
| 1362 | test_bit(Faulty, &rdev2->flags)) |
| 1363 | is_active = 0; |
| 1364 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1365 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1367 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1368 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | d = &sb->disks[rdev2->desc_nr]; |
| 1370 | nr_disks++; |
| 1371 | d->number = rdev2->desc_nr; |
| 1372 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1373 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1374 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | d->raid_disk = rdev2->raid_disk; |
| 1376 | else |
| 1377 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1378 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1380 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1382 | if (test_bit(In_sync, &rdev2->flags)) |
| 1383 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | active++; |
| 1385 | working++; |
| 1386 | } else { |
| 1387 | d->state = 0; |
| 1388 | spare++; |
| 1389 | working++; |
| 1390 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1391 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1392 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1395 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1396 | mdp_disk_t *d = &sb->disks[i]; |
| 1397 | if (d->state == 0 && d->number == 0) { |
| 1398 | d->number = i; |
| 1399 | d->raid_disk = i; |
| 1400 | d->state = (1<<MD_DISK_REMOVED); |
| 1401 | d->state |= (1<<MD_DISK_FAULTY); |
| 1402 | failed++; |
| 1403 | } |
| 1404 | } |
| 1405 | sb->nr_disks = nr_disks; |
| 1406 | sb->active_disks = active; |
| 1407 | sb->working_disks = working; |
| 1408 | sb->failed_disks = failed; |
| 1409 | sb->spare_disks = spare; |
| 1410 | |
| 1411 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1412 | sb->sb_csum = calc_sb_csum(sb); |
| 1413 | } |
| 1414 | |
| 1415 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1416 | * rdev_size_change for 0.90.0 |
| 1417 | */ |
| 1418 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1419 | 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] | 1420 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1421 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1422 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1423 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1424 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1425 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1426 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1427 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1428 | /* Limit to 4TB as metadata cannot record more than that. |
| 1429 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1430 | */ |
| 1431 | if (num_sectors >= (2ULL << 32)) |
| 1432 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1433 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1434 | rdev->sb_page); |
| 1435 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1436 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | * version 1 superblock |
| 1442 | */ |
| 1443 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1444 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1446 | __le32 disk_csum; |
| 1447 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | unsigned long long newcsum; |
| 1449 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1450 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | int i; |
| 1452 | |
| 1453 | disk_csum = sb->sb_csum; |
| 1454 | sb->sb_csum = 0; |
| 1455 | newcsum = 0; |
| 1456 | for (i=0; size>=4; size -= 4 ) |
| 1457 | newcsum += le32_to_cpu(*isuper++); |
| 1458 | |
| 1459 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1460 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
| 1462 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1463 | sb->sb_csum = disk_csum; |
| 1464 | return cpu_to_le32(csum); |
| 1465 | } |
| 1466 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1467 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1468 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1469 | 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] | 1470 | { |
| 1471 | struct mdp_superblock_1 *sb; |
| 1472 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1473 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1475 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
| 1477 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1478 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | * It is always aligned to a 4K boundary and |
| 1480 | * depeding on minor_version, it can be: |
| 1481 | * 0: At least 8K, but less than 12K, from end of device |
| 1482 | * 1: At start of device |
| 1483 | * 2: 4K from start of device. |
| 1484 | */ |
| 1485 | switch(minor_version) { |
| 1486 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1487 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1488 | sb_start -= 8*2; |
| 1489 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | break; |
| 1491 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1492 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | break; |
| 1494 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1495 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | default: |
| 1498 | return -EINVAL; |
| 1499 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1500 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1502 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1503 | * and it is safe to read 4k, so we do that |
| 1504 | */ |
| 1505 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | if (ret) return ret; |
| 1507 | |
| 1508 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1509 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
| 1511 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1512 | sb->major_version != cpu_to_le32(1) || |
| 1513 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1514 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1515 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | return -EINVAL; |
| 1517 | |
| 1518 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1519 | printk("md: invalid superblock checksum on %s\n", |
| 1520 | bdevname(rdev->bdev,b)); |
| 1521 | return -EINVAL; |
| 1522 | } |
| 1523 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1524 | printk("md: data_size too small on %s\n", |
| 1525 | bdevname(rdev->bdev,b)); |
| 1526 | return -EINVAL; |
| 1527 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | rdev->preferred_minor = 0xffff; |
| 1530 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1531 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1533 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1534 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1535 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1536 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1537 | |
| 1538 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1539 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1540 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1541 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1542 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1543 | rdev->desc_nr = -1; |
| 1544 | else |
| 1545 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1546 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1547 | if (!rdev->bb_page) { |
| 1548 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1549 | if (!rdev->bb_page) |
| 1550 | return -ENOMEM; |
| 1551 | } |
| 1552 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1553 | rdev->badblocks.count == 0) { |
| 1554 | /* need to load the bad block list. |
| 1555 | * Currently we limit it to one page. |
| 1556 | */ |
| 1557 | s32 offset; |
| 1558 | sector_t bb_sector; |
| 1559 | u64 *bbp; |
| 1560 | int i; |
| 1561 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1562 | if (sectors > (PAGE_SIZE / 512)) |
| 1563 | return -EINVAL; |
| 1564 | offset = le32_to_cpu(sb->bblog_offset); |
| 1565 | if (offset == 0) |
| 1566 | return -EINVAL; |
| 1567 | bb_sector = (long long)offset; |
| 1568 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1569 | rdev->bb_page, READ, true)) |
| 1570 | return -EIO; |
| 1571 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1572 | rdev->badblocks.shift = sb->bblog_shift; |
| 1573 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1574 | u64 bb = le64_to_cpu(*bbp); |
| 1575 | int count = bb & (0x3ff); |
| 1576 | u64 sector = bb >> 10; |
| 1577 | sector <<= sb->bblog_shift; |
| 1578 | count <<= sb->bblog_shift; |
| 1579 | if (bb + 1 == 0) |
| 1580 | break; |
| 1581 | if (md_set_badblocks(&rdev->badblocks, |
| 1582 | sector, count, 1) == 0) |
| 1583 | return -EINVAL; |
| 1584 | } |
| 1585 | } else if (sb->bblog_offset == 0) |
| 1586 | rdev->badblocks.shift = -1; |
| 1587 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1588 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1589 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1590 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1592 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1595 | sb->level != refsb->level || |
| 1596 | sb->layout != refsb->layout || |
| 1597 | sb->chunksize != refsb->chunksize) { |
| 1598 | printk(KERN_WARNING "md: %s has strangely different" |
| 1599 | " superblock to %s\n", |
| 1600 | bdevname(rdev->bdev,b), |
| 1601 | bdevname(refdev->bdev,b2)); |
| 1602 | return -EINVAL; |
| 1603 | } |
| 1604 | ev1 = le64_to_cpu(sb->events); |
| 1605 | ev2 = le64_to_cpu(refsb->events); |
| 1606 | |
| 1607 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1608 | ret = 1; |
| 1609 | else |
| 1610 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1612 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1613 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1614 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1616 | rdev->sectors = rdev->sb_start; |
| 1617 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1619 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1620 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1621 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1622 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1625 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1627 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1628 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1630 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1631 | clear_bit(Faulty, &rdev->flags); |
| 1632 | clear_bit(In_sync, &rdev->flags); |
| 1633 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | if (mddev->raid_disks == 0) { |
| 1636 | mddev->major_version = 1; |
| 1637 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1638 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1639 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1641 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1642 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1643 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | mddev->layout = le32_to_cpu(sb->layout); |
| 1645 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1646 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1647 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1648 | mddev->bitmap_info.offset = 0; |
| 1649 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1652 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1653 | |
| 1654 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1655 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1656 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1657 | mddev->bitmap_info.file == NULL ) |
| 1658 | mddev->bitmap_info.offset = |
| 1659 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1660 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1661 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1662 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1663 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1664 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1665 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1666 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1667 | } else { |
| 1668 | mddev->reshape_position = MaxSector; |
| 1669 | mddev->delta_disks = 0; |
| 1670 | mddev->new_level = mddev->level; |
| 1671 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1672 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1675 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1676 | /* Insist of good event counter while assembling, except for |
| 1677 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1679 | if (rdev->desc_nr >= 0 && |
| 1680 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1681 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1682 | if (ev1 < mddev->events) |
| 1683 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1684 | } else if (mddev->bitmap) { |
| 1685 | /* If adding to array with a bitmap, then we can accept an |
| 1686 | * older device, but not too old. |
| 1687 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1688 | if (ev1 < mddev->bitmap->events_cleared) |
| 1689 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1690 | } else { |
| 1691 | if (ev1 < mddev->events) |
| 1692 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1693 | return 0; |
| 1694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1696 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1697 | if (rdev->desc_nr < 0 || |
| 1698 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1699 | role = 0xffff; |
| 1700 | rdev->desc_nr = -1; |
| 1701 | } else |
| 1702 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | switch(role) { |
| 1704 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | break; |
| 1706 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1707 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | break; |
| 1709 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1710 | if ((le32_to_cpu(sb->feature_map) & |
| 1711 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1712 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1713 | else |
| 1714 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | rdev->raid_disk = role; |
| 1716 | break; |
| 1717 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1718 | if (sb->devflags & WriteMostly1) |
| 1719 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1720 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1721 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1726 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | { |
| 1728 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1729 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | int max_dev, i; |
| 1731 | /* make rdev->sb match mddev and rdev data. */ |
| 1732 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1733 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
| 1735 | sb->feature_map = 0; |
| 1736 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1737 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1740 | |
| 1741 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1742 | sb->events = cpu_to_le64(mddev->events); |
| 1743 | if (mddev->in_sync) |
| 1744 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1745 | else |
| 1746 | sb->resync_offset = cpu_to_le64(0); |
| 1747 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1748 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1749 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1750 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1751 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1752 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1753 | sb->level = cpu_to_le32(mddev->level); |
| 1754 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1755 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1756 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1757 | sb->devflags |= WriteMostly1; |
| 1758 | else |
| 1759 | sb->devflags &= ~WriteMostly1; |
| 1760 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1761 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1762 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1763 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1764 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1767 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1768 | sb->feature_map |= |
| 1769 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1770 | sb->recovery_offset = |
| 1771 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1774 | if (mddev->reshape_position != MaxSector) { |
| 1775 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1776 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1777 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1778 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1779 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1780 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1781 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1782 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1783 | if (rdev->badblocks.count == 0) |
| 1784 | /* Nothing to do for bad blocks*/ ; |
| 1785 | else if (sb->bblog_offset == 0) |
| 1786 | /* Cannot record bad blocks on this device */ |
| 1787 | md_error(mddev, rdev); |
| 1788 | else { |
| 1789 | struct badblocks *bb = &rdev->badblocks; |
| 1790 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1791 | u64 *p = bb->page; |
| 1792 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1793 | if (bb->changed) { |
| 1794 | unsigned seq; |
| 1795 | |
| 1796 | retry: |
| 1797 | seq = read_seqbegin(&bb->lock); |
| 1798 | |
| 1799 | memset(bbp, 0xff, PAGE_SIZE); |
| 1800 | |
| 1801 | for (i = 0 ; i < bb->count ; i++) { |
| 1802 | u64 internal_bb = *p++; |
| 1803 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1804 | | BB_LEN(internal_bb)); |
| 1805 | *bbp++ = cpu_to_le64(store_bb); |
| 1806 | } |
| 1807 | if (read_seqretry(&bb->lock, seq)) |
| 1808 | goto retry; |
| 1809 | |
| 1810 | bb->sector = (rdev->sb_start + |
| 1811 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1812 | bb->size = le16_to_cpu(sb->bblog_size); |
| 1813 | bb->changed = 0; |
| 1814 | } |
| 1815 | } |
| 1816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1818 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | if (rdev2->desc_nr+1 > max_dev) |
| 1820 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1821 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1822 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1823 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1824 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1825 | rdev->sb_size = max_dev * 2 + 256; |
| 1826 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1827 | if (rdev->sb_size & bmask) |
| 1828 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1829 | } else |
| 1830 | max_dev = le32_to_cpu(sb->max_dev); |
| 1831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | for (i=0; i<max_dev;i++) |
| 1833 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1834 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1835 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1837 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1839 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1841 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1842 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | else |
| 1844 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1845 | } |
| 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1848 | } |
| 1849 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1850 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1851 | 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] | 1852 | { |
| 1853 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1854 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1855 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1856 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1857 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1858 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1859 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1860 | max_sectors -= rdev->data_offset; |
| 1861 | if (!num_sectors || num_sectors > max_sectors) |
| 1862 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1863 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | /* minor version 0 with bitmap we can't move */ |
| 1865 | return 0; |
| 1866 | } else { |
| 1867 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1868 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1869 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1870 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1871 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1872 | if (!num_sectors || num_sectors > max_sectors) |
| 1873 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1874 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1875 | } |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1876 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1877 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1878 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1879 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1880 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1881 | rdev->sb_page); |
| 1882 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1883 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1886 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | [0] = { |
| 1888 | .name = "0.90.0", |
| 1889 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1890 | .load_super = super_90_load, |
| 1891 | .validate_super = super_90_validate, |
| 1892 | .sync_super = super_90_sync, |
| 1893 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | }, |
| 1895 | [1] = { |
| 1896 | .name = "md-1", |
| 1897 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1898 | .load_super = super_1_load, |
| 1899 | .validate_super = super_1_validate, |
| 1900 | .sync_super = super_1_sync, |
| 1901 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | }, |
| 1903 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1905 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1906 | { |
| 1907 | if (mddev->sync_super) { |
| 1908 | mddev->sync_super(mddev, rdev); |
| 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1913 | |
| 1914 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1915 | } |
| 1916 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1917 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1919 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1921 | rcu_read_lock(); |
| 1922 | rdev_for_each_rcu(rdev, mddev1) |
| 1923 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1924 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1925 | rdev2->bdev->bd_contains) { |
| 1926 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1927 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1928 | } |
| 1929 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static LIST_HEAD(pending_raid_disks); |
| 1934 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1935 | /* |
| 1936 | * Try to register data integrity profile for an mddev |
| 1937 | * |
| 1938 | * This is called when an array is started and after a disk has been kicked |
| 1939 | * from the array. It only succeeds if all working and active component devices |
| 1940 | * are integrity capable with matching profiles. |
| 1941 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1942 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1943 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1944 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1945 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1946 | if (list_empty(&mddev->disks)) |
| 1947 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1948 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1949 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1950 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1951 | /* skip spares and non-functional disks */ |
| 1952 | if (test_bit(Faulty, &rdev->flags)) |
| 1953 | continue; |
| 1954 | if (rdev->raid_disk < 0) |
| 1955 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1956 | if (!reference) { |
| 1957 | /* Use the first rdev as the reference */ |
| 1958 | reference = rdev; |
| 1959 | continue; |
| 1960 | } |
| 1961 | /* does this rdev's profile match the reference profile? */ |
| 1962 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1963 | rdev->bdev->bd_disk) < 0) |
| 1964 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1965 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1966 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1967 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1968 | /* |
| 1969 | * All component devices are integrity capable and have matching |
| 1970 | * profiles, register the common profile for the md device. |
| 1971 | */ |
| 1972 | if (blk_integrity_register(mddev->gendisk, |
| 1973 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1974 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1975 | mdname(mddev)); |
| 1976 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1977 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1978 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1979 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1980 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1981 | mdname(mddev)); |
| 1982 | return -EINVAL; |
| 1983 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1984 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1985 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1986 | EXPORT_SYMBOL(md_integrity_register); |
| 1987 | |
| 1988 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1989 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | { |
| 1991 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1992 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1993 | |
| 1994 | if (!bi_mddev) /* nothing to do */ |
| 1995 | return; |
| 1996 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1997 | return; |
| 1998 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1999 | rdev->bdev->bd_disk) >= 0) |
| 2000 | return; |
| 2001 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2002 | blk_integrity_unregister(mddev->gendisk); |
| 2003 | } |
| 2004 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2005 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2006 | 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] | 2007 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2008 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2009 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2010 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2011 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
| 2013 | if (rdev->mddev) { |
| 2014 | MD_BUG(); |
| 2015 | return -EINVAL; |
| 2016 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2017 | |
| 2018 | /* prevent duplicates */ |
| 2019 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2020 | return -EEXIST; |
| 2021 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2022 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2023 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2024 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2025 | if (mddev->pers) { |
| 2026 | /* Cannot change size, so fail |
| 2027 | * If mddev->level <= 0, then we don't care |
| 2028 | * about aligning sizes (e.g. linear) |
| 2029 | */ |
| 2030 | if (mddev->level > 0) |
| 2031 | return -ENOSPC; |
| 2032 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2033 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | |
| 2036 | /* Verify rdev->desc_nr is unique. |
| 2037 | * If it is -1, assign a free number, else |
| 2038 | * check number is not in use |
| 2039 | */ |
| 2040 | if (rdev->desc_nr < 0) { |
| 2041 | int choice = 0; |
| 2042 | if (mddev->pers) choice = mddev->raid_disks; |
| 2043 | while (find_rdev_nr(mddev, choice)) |
| 2044 | choice++; |
| 2045 | rdev->desc_nr = choice; |
| 2046 | } else { |
| 2047 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2048 | return -EBUSY; |
| 2049 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2050 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2051 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2052 | mdname(mddev), mddev->max_disks); |
| 2053 | return -EBUSY; |
| 2054 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2055 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2056 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2057 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2060 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2061 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2062 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2063 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2064 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2065 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2066 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2067 | /* failure here is OK */; |
| 2068 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2069 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2070 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2071 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2072 | |
| 2073 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2074 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2077 | |
| 2078 | fail: |
| 2079 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2080 | b, mdname(mddev)); |
| 2081 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2084 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2085 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2086 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2087 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2088 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2091 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | { |
| 2093 | char b[BDEVNAME_SIZE]; |
| 2094 | if (!rdev->mddev) { |
| 2095 | MD_BUG(); |
| 2096 | return; |
| 2097 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2098 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2099 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2101 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2102 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2103 | sysfs_put(rdev->sysfs_state); |
| 2104 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2105 | kfree(rdev->badblocks.page); |
| 2106 | rdev->badblocks.count = 0; |
| 2107 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2108 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2109 | * writing to 'remove' to "dev/state". We also need |
| 2110 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2111 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2112 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2113 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2114 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2115 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * prevent the device from being mounted, repartitioned or |
| 2120 | * otherwise reused by a RAID array (or any other kernel |
| 2121 | * subsystem), by bd_claiming the device. |
| 2122 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2123 | 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] | 2124 | { |
| 2125 | int err = 0; |
| 2126 | struct block_device *bdev; |
| 2127 | char b[BDEVNAME_SIZE]; |
| 2128 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2129 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2130 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | if (IS_ERR(bdev)) { |
| 2132 | printk(KERN_ERR "md: could not open %s.\n", |
| 2133 | __bdevname(dev, b)); |
| 2134 | return PTR_ERR(bdev); |
| 2135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | rdev->bdev = bdev; |
| 2137 | return err; |
| 2138 | } |
| 2139 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2140 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | { |
| 2142 | struct block_device *bdev = rdev->bdev; |
| 2143 | rdev->bdev = NULL; |
| 2144 | if (!bdev) |
| 2145 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2146 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | void md_autodetect_dev(dev_t dev); |
| 2150 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2151 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | { |
| 2153 | char b[BDEVNAME_SIZE]; |
| 2154 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2155 | bdevname(rdev->bdev,b)); |
| 2156 | if (rdev->mddev) |
| 2157 | MD_BUG(); |
| 2158 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2160 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2161 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | #endif |
| 2163 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2164 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2167 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | { |
| 2169 | unbind_rdev_from_array(rdev); |
| 2170 | export_rdev(rdev); |
| 2171 | } |
| 2172 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2173 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2175 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2177 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | if (!rdev->mddev) { |
| 2179 | MD_BUG(); |
| 2180 | continue; |
| 2181 | } |
| 2182 | kick_rdev_from_array(rdev); |
| 2183 | } |
| 2184 | if (!list_empty(&mddev->disks)) |
| 2185 | MD_BUG(); |
| 2186 | mddev->raid_disks = 0; |
| 2187 | mddev->major_version = 0; |
| 2188 | } |
| 2189 | |
| 2190 | static void print_desc(mdp_disk_t *desc) |
| 2191 | { |
| 2192 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2193 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2194 | } |
| 2195 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2196 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { |
| 2198 | int i; |
| 2199 | |
| 2200 | printk(KERN_INFO |
| 2201 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2202 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2203 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2204 | sb->ctime); |
| 2205 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2206 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2207 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2208 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2209 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2210 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2211 | sb->failed_disks, sb->spare_disks, |
| 2212 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2213 | |
| 2214 | printk(KERN_INFO); |
| 2215 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2216 | mdp_disk_t *desc; |
| 2217 | |
| 2218 | desc = sb->disks + i; |
| 2219 | if (desc->number || desc->major || desc->minor || |
| 2220 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2221 | printk(" D %2d: ", i); |
| 2222 | print_desc(desc); |
| 2223 | } |
| 2224 | } |
| 2225 | printk(KERN_INFO "md: THIS: "); |
| 2226 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2229 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2230 | { |
| 2231 | __u8 *uuid; |
| 2232 | |
| 2233 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2234 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2235 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2236 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2237 | le32_to_cpu(sb->major_version), |
| 2238 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2239 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2240 | sb->set_name, |
| 2241 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2242 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2243 | |
| 2244 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2245 | printk(KERN_INFO |
| 2246 | "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] | 2247 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2248 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2249 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2250 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2251 | le32_to_cpu(sb->level), |
| 2252 | (unsigned long long)le64_to_cpu(sb->size), |
| 2253 | le32_to_cpu(sb->raid_disks), |
| 2254 | le32_to_cpu(sb->layout), |
| 2255 | le32_to_cpu(sb->chunksize), |
| 2256 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2257 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2258 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2259 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2260 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2261 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2262 | sb->devflags, |
| 2263 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2264 | (unsigned long long)le64_to_cpu(sb->events), |
| 2265 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2266 | le32_to_cpu(sb->sb_csum), |
| 2267 | le32_to_cpu(sb->max_dev) |
| 2268 | ); |
| 2269 | } |
| 2270 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2271 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | { |
| 2273 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2274 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2275 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2276 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2277 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2279 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2280 | switch (major_version) { |
| 2281 | case 0: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2282 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2283 | break; |
| 2284 | case 1: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2285 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2286 | break; |
| 2287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | } else |
| 2289 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2290 | } |
| 2291 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2292 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2294 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2295 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2296 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | char b[BDEVNAME_SIZE]; |
| 2298 | |
| 2299 | printk("\n"); |
| 2300 | printk("md: **********************************\n"); |
| 2301 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2302 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2303 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2305 | if (mddev->bitmap) |
| 2306 | bitmap_print_sb(mddev->bitmap); |
| 2307 | else |
| 2308 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2309 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2311 | printk("\n"); |
| 2312 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2313 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2314 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | } |
| 2316 | printk("md: **********************************\n"); |
| 2317 | printk("\n"); |
| 2318 | } |
| 2319 | |
| 2320 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2321 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2323 | /* Update each superblock (in-memory image), but |
| 2324 | * if we are allowed to, skip spares which already |
| 2325 | * have the right event counter, or have one earlier |
| 2326 | * (which would mean they aren't being marked as dirty |
| 2327 | * with the rest of the array) |
| 2328 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2329 | struct md_rdev *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2330 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2331 | if (rdev->sb_events == mddev->events || |
| 2332 | (nospares && |
| 2333 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2334 | rdev->sb_events+1 == mddev->events)) { |
| 2335 | /* Don't update this superblock */ |
| 2336 | rdev->sb_loaded = 2; |
| 2337 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2338 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2339 | rdev->sb_loaded = 1; |
| 2340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2344 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2346 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2347 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2348 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2349 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2352 | /* First make sure individual recovery_offsets are correct */ |
| 2353 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2354 | if (rdev->raid_disk >= 0 && |
| 2355 | mddev->delta_disks >= 0 && |
| 2356 | !test_bit(In_sync, &rdev->flags) && |
| 2357 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2358 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2359 | |
| 2360 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2361 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2362 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2363 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2364 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2365 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2366 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2367 | if (rdev->badblocks.changed) { |
| 2368 | md_ack_all_badblocks(&rdev->badblocks); |
| 2369 | md_error(mddev, rdev); |
| 2370 | } |
| 2371 | clear_bit(Blocked, &rdev->flags); |
| 2372 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2373 | wake_up(&rdev->blocked_wait); |
| 2374 | } |
| 2375 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2376 | wake_up(&mddev->sb_wait); |
| 2377 | return; |
| 2378 | } |
| 2379 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2380 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2381 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2382 | mddev->utime = get_seconds(); |
| 2383 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2384 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2385 | force_change = 1; |
| 2386 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2387 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2388 | * though if events isn't the right even/odd, we will have to do |
| 2389 | * spares after all |
| 2390 | */ |
| 2391 | nospares = 1; |
| 2392 | if (force_change) |
| 2393 | nospares = 0; |
| 2394 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2395 | /* If the array is degraded, then skipping spares is both |
| 2396 | * dangerous and fairly pointless. |
| 2397 | * Dangerous because a device that was removed from the array |
| 2398 | * might have a event_count that still looks up-to-date, |
| 2399 | * so it can be re-added without a resync. |
| 2400 | * Pointless because if there are any spares to skip, |
| 2401 | * then a recovery will happen and soon that array won't |
| 2402 | * be degraded any more and the spare can go back to sleep then. |
| 2403 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2404 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2405 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2406 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2407 | |
| 2408 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2409 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2410 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2412 | && mddev->can_decrease_events |
| 2413 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2414 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2415 | mddev->can_decrease_events = 0; |
| 2416 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2417 | /* otherwise we have to go forward and ... */ |
| 2418 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2419 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | |
| 2422 | if (!mddev->events) { |
| 2423 | /* |
| 2424 | * oops, this 64-bit counter should never wrap. |
| 2425 | * Either we are in around ~1 trillion A.C., assuming |
| 2426 | * 1 reboot per second, or we have a bug: |
| 2427 | */ |
| 2428 | MD_BUG(); |
| 2429 | mddev->events --; |
| 2430 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2431 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2432 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2433 | if (rdev->badblocks.changed) |
| 2434 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2435 | if (test_bit(Faulty, &rdev->flags)) |
| 2436 | set_bit(FaultRecorded, &rdev->flags); |
| 2437 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2438 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2439 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2440 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2442 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2443 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2445 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2446 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2448 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2449 | if (rdev->sb_loaded != 1) |
| 2450 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2452 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2453 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2454 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2455 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2456 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2457 | bdevname(rdev->bdev, b), |
| 2458 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2459 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2460 | if (rdev->badblocks.size) { |
| 2461 | md_super_write(mddev, rdev, |
| 2462 | rdev->badblocks.sector, |
| 2463 | rdev->badblocks.size << 9, |
| 2464 | rdev->bb_page); |
| 2465 | rdev->badblocks.size = 0; |
| 2466 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2469 | pr_debug("md: %s (skipping faulty)\n", |
| 2470 | bdevname(rdev->bdev, b)); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2471 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | /* only need to write one superblock... */ |
| 2473 | break; |
| 2474 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2475 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2476 | /* 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] | 2477 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2478 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2479 | if (mddev->in_sync != sync_req || |
| 2480 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2481 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2482 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2483 | goto repeat; |
| 2484 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2485 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2486 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2487 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2488 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2489 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2490 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2491 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2492 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2493 | clear_bit(Blocked, &rdev->flags); |
| 2494 | |
| 2495 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2496 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2497 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2498 | wake_up(&rdev->blocked_wait); |
| 2499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2502 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2503 | * We want to accept with case. For this we use cmd_match. |
| 2504 | */ |
| 2505 | static int cmd_match(const char *cmd, const char *str) |
| 2506 | { |
| 2507 | /* See if cmd, written into a sysfs file, matches |
| 2508 | * str. They must either be the same, or cmd can |
| 2509 | * have a trailing newline |
| 2510 | */ |
| 2511 | while (*cmd && *str && *cmd == *str) { |
| 2512 | cmd++; |
| 2513 | str++; |
| 2514 | } |
| 2515 | if (*cmd == '\n') |
| 2516 | cmd++; |
| 2517 | if (*str || *cmd) |
| 2518 | return 0; |
| 2519 | return 1; |
| 2520 | } |
| 2521 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2522 | struct rdev_sysfs_entry { |
| 2523 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2524 | ssize_t (*show)(struct md_rdev *, char *); |
| 2525 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2526 | }; |
| 2527 | |
| 2528 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2529 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2530 | { |
| 2531 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2532 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2533 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2534 | if (test_bit(Faulty, &rdev->flags) || |
| 2535 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2536 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2537 | sep = ","; |
| 2538 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2539 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2540 | len += sprintf(page+len, "%sin_sync",sep); |
| 2541 | sep = ","; |
| 2542 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2543 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2544 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2545 | sep = ","; |
| 2546 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2547 | if (test_bit(Blocked, &rdev->flags) || |
| 2548 | rdev->badblocks.unacked_exist) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2549 | len += sprintf(page+len, "%sblocked", sep); |
| 2550 | sep = ","; |
| 2551 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2552 | if (!test_bit(Faulty, &rdev->flags) && |
| 2553 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2554 | len += sprintf(page+len, "%sspare", sep); |
| 2555 | sep = ","; |
| 2556 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2557 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2558 | len += sprintf(page+len, "%swrite_error", sep); |
| 2559 | sep = ","; |
| 2560 | } |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2561 | return len+sprintf(page+len, "\n"); |
| 2562 | } |
| 2563 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2564 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2565 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2566 | { |
| 2567 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2568 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2569 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2570 | * writemostly - sets write_mostly |
| 2571 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2572 | * blocked - sets the Blocked flags |
| 2573 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2574 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2575 | * write_error - sets WriteErrorSeen |
| 2576 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2577 | */ |
| 2578 | int err = -EINVAL; |
| 2579 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2580 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2581 | if (test_bit(Faulty, &rdev->flags)) |
| 2582 | err = 0; |
| 2583 | else |
| 2584 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2585 | } else if (cmd_match(buf, "remove")) { |
| 2586 | if (rdev->raid_disk >= 0) |
| 2587 | err = -EBUSY; |
| 2588 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2589 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2590 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2591 | if (mddev->pers) |
| 2592 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2593 | md_new_event(mddev); |
| 2594 | err = 0; |
| 2595 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2596 | } else if (cmd_match(buf, "writemostly")) { |
| 2597 | set_bit(WriteMostly, &rdev->flags); |
| 2598 | err = 0; |
| 2599 | } else if (cmd_match(buf, "-writemostly")) { |
| 2600 | clear_bit(WriteMostly, &rdev->flags); |
| 2601 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2602 | } else if (cmd_match(buf, "blocked")) { |
| 2603 | set_bit(Blocked, &rdev->flags); |
| 2604 | err = 0; |
| 2605 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2606 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2607 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2608 | /* metadata handler doesn't understand badblocks, |
| 2609 | * so we need to fail the device |
| 2610 | */ |
| 2611 | md_error(rdev->mddev, rdev); |
| 2612 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2613 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2614 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2615 | wake_up(&rdev->blocked_wait); |
| 2616 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2617 | md_wakeup_thread(rdev->mddev->thread); |
| 2618 | |
| 2619 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2620 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2621 | set_bit(In_sync, &rdev->flags); |
| 2622 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2623 | } else if (cmd_match(buf, "write_error")) { |
| 2624 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2625 | err = 0; |
| 2626 | } else if (cmd_match(buf, "-write_error")) { |
| 2627 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2628 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2629 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2630 | if (!err) |
| 2631 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2632 | return err ? err : len; |
| 2633 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2634 | static struct rdev_sysfs_entry rdev_state = |
| 2635 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2636 | |
| 2637 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2638 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2639 | { |
| 2640 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2641 | } |
| 2642 | |
| 2643 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2644 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2645 | { |
| 2646 | char *e; |
| 2647 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2648 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2649 | atomic_set(&rdev->corrected_errors, n); |
| 2650 | return len; |
| 2651 | } |
| 2652 | return -EINVAL; |
| 2653 | } |
| 2654 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2655 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2656 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2657 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2658 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2659 | { |
| 2660 | if (rdev->raid_disk < 0) |
| 2661 | return sprintf(page, "none\n"); |
| 2662 | else |
| 2663 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2664 | } |
| 2665 | |
| 2666 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2667 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2668 | { |
| 2669 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2670 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2671 | int slot = simple_strtoul(buf, &e, 10); |
| 2672 | if (strncmp(buf, "none", 4)==0) |
| 2673 | slot = -1; |
| 2674 | else if (e==buf || (*e && *e!= '\n')) |
| 2675 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2676 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2677 | /* Setting 'slot' on an active array requires also |
| 2678 | * updating the 'rd%d' link, and communicating |
| 2679 | * with the personality with ->hot_*_disk. |
| 2680 | * For now we only support removing |
| 2681 | * failed/spare devices. This normally happens automatically, |
| 2682 | * but not when the metadata is externally managed. |
| 2683 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2684 | if (rdev->raid_disk == -1) |
| 2685 | return -EEXIST; |
| 2686 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2687 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2688 | return -EINVAL; |
| 2689 | err = rdev->mddev->pers-> |
| 2690 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2691 | if (err) |
| 2692 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2693 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2694 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2695 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2696 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2697 | } else if (rdev->mddev->pers) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2698 | struct md_rdev *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2699 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2700 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2701 | */ |
| 2702 | |
| 2703 | if (rdev->raid_disk != -1) |
| 2704 | return -EBUSY; |
| 2705 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2706 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2707 | return -EBUSY; |
| 2708 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2709 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2710 | return -EINVAL; |
| 2711 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2712 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2713 | if (rdev2->raid_disk == slot) |
| 2714 | return -EEXIST; |
| 2715 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2716 | if (slot >= rdev->mddev->raid_disks && |
| 2717 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2718 | return -ENOSPC; |
| 2719 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2720 | rdev->raid_disk = slot; |
| 2721 | if (test_bit(In_sync, &rdev->flags)) |
| 2722 | rdev->saved_raid_disk = slot; |
| 2723 | else |
| 2724 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame^] | 2725 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2726 | err = rdev->mddev->pers-> |
| 2727 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2728 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2729 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2730 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2731 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2732 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2733 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2734 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2735 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2736 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2737 | if (slot >= rdev->mddev->raid_disks && |
| 2738 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2739 | return -ENOSPC; |
| 2740 | rdev->raid_disk = slot; |
| 2741 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2742 | clear_bit(Faulty, &rdev->flags); |
| 2743 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2744 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2745 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2746 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2747 | return len; |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2752 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2753 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2754 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2755 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2756 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2757 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2761 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2762 | { |
| 2763 | char *e; |
| 2764 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2765 | if (e==buf || (*e && *e != '\n')) |
| 2766 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2767 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2768 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2769 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2770 | /* Must set offset before size, so overlap checks |
| 2771 | * can be sane */ |
| 2772 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2773 | rdev->data_offset = offset; |
| 2774 | return len; |
| 2775 | } |
| 2776 | |
| 2777 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2778 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2779 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2780 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2781 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2782 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2783 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2784 | } |
| 2785 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2786 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2787 | { |
| 2788 | /* check if two start/length pairs overlap */ |
| 2789 | if (s1+l1 <= s2) |
| 2790 | return 0; |
| 2791 | if (s2+l2 <= s1) |
| 2792 | return 0; |
| 2793 | return 1; |
| 2794 | } |
| 2795 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2796 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2797 | { |
| 2798 | unsigned long long blocks; |
| 2799 | sector_t new; |
| 2800 | |
| 2801 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2802 | return -EINVAL; |
| 2803 | |
| 2804 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2805 | return -EINVAL; /* sector conversion overflow */ |
| 2806 | |
| 2807 | new = blocks * 2; |
| 2808 | if (new != blocks * 2) |
| 2809 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2810 | |
| 2811 | *sectors = new; |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2815 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2816 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2817 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2818 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2819 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2820 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2821 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2822 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2823 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2824 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2825 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2826 | sectors = super_types[my_mddev->major_version]. |
| 2827 | rdev_size_change(rdev, sectors); |
| 2828 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2829 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2830 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2831 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2832 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2833 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2834 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2835 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2836 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2837 | rdev->sectors = sectors; |
| 2838 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2839 | /* need to check that all other rdevs with the same ->bdev |
| 2840 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2841 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2842 | * we have to change it back, we will have the lock again. |
| 2843 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2844 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2845 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2846 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2847 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2848 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2849 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2850 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2851 | |
| 2852 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2853 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2854 | if (rdev->bdev == rdev2->bdev && |
| 2855 | rdev != rdev2 && |
| 2856 | overlaps(rdev->data_offset, rdev->sectors, |
| 2857 | rdev2->data_offset, |
| 2858 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2859 | overlap = 1; |
| 2860 | break; |
| 2861 | } |
| 2862 | mddev_unlock(mddev); |
| 2863 | if (overlap) { |
| 2864 | mddev_put(mddev); |
| 2865 | break; |
| 2866 | } |
| 2867 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2868 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2869 | if (overlap) { |
| 2870 | /* Someone else could have slipped in a size |
| 2871 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2872 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2873 | * safe, and trust userspace not to race with |
| 2874 | * itself |
| 2875 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2876 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2877 | return -EBUSY; |
| 2878 | } |
| 2879 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2880 | return len; |
| 2881 | } |
| 2882 | |
| 2883 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2884 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2885 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2886 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2887 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2888 | { |
| 2889 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2890 | |
| 2891 | if (test_bit(In_sync, &rdev->flags) || |
| 2892 | recovery_start == MaxSector) |
| 2893 | return sprintf(page, "none\n"); |
| 2894 | |
| 2895 | return sprintf(page, "%llu\n", recovery_start); |
| 2896 | } |
| 2897 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2898 | 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] | 2899 | { |
| 2900 | unsigned long long recovery_start; |
| 2901 | |
| 2902 | if (cmd_match(buf, "none")) |
| 2903 | recovery_start = MaxSector; |
| 2904 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2905 | return -EINVAL; |
| 2906 | |
| 2907 | if (rdev->mddev->pers && |
| 2908 | rdev->raid_disk >= 0) |
| 2909 | return -EBUSY; |
| 2910 | |
| 2911 | rdev->recovery_offset = recovery_start; |
| 2912 | if (recovery_start == MaxSector) |
| 2913 | set_bit(In_sync, &rdev->flags); |
| 2914 | else |
| 2915 | clear_bit(In_sync, &rdev->flags); |
| 2916 | return len; |
| 2917 | } |
| 2918 | |
| 2919 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2920 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2921 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2922 | |
| 2923 | static ssize_t |
| 2924 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2925 | static ssize_t |
| 2926 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2927 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2928 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2929 | { |
| 2930 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2931 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2932 | 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] | 2933 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2934 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2935 | /* Maybe that ack was all we needed */ |
| 2936 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2937 | wake_up(&rdev->blocked_wait); |
| 2938 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2939 | } |
| 2940 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2941 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2942 | |
| 2943 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2944 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2945 | { |
| 2946 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2947 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2948 | 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] | 2949 | { |
| 2950 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2951 | } |
| 2952 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2953 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2954 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2955 | static struct attribute *rdev_default_attrs[] = { |
| 2956 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2957 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2958 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2959 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2960 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2961 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2962 | &rdev_bad_blocks.attr, |
| 2963 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2964 | NULL, |
| 2965 | }; |
| 2966 | static ssize_t |
| 2967 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2968 | { |
| 2969 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2970 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2971 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2972 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2973 | |
| 2974 | if (!entry->show) |
| 2975 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2976 | |
| 2977 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2978 | if (!rv) { |
| 2979 | if (rdev->mddev == NULL) |
| 2980 | rv = -EBUSY; |
| 2981 | else |
| 2982 | rv = entry->show(rdev, page); |
| 2983 | mddev_unlock(mddev); |
| 2984 | } |
| 2985 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | static ssize_t |
| 2989 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2990 | const char *page, size_t length) |
| 2991 | { |
| 2992 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2993 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2994 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2995 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2996 | |
| 2997 | if (!entry->store) |
| 2998 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2999 | if (!capable(CAP_SYS_ADMIN)) |
| 3000 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3001 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3002 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3003 | if (rdev->mddev == NULL) |
| 3004 | rv = -EBUSY; |
| 3005 | else |
| 3006 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3007 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3008 | } |
| 3009 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | static void rdev_free(struct kobject *ko) |
| 3013 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3014 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3015 | kfree(rdev); |
| 3016 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3017 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3018 | .show = rdev_attr_show, |
| 3019 | .store = rdev_attr_store, |
| 3020 | }; |
| 3021 | static struct kobj_type rdev_ktype = { |
| 3022 | .release = rdev_free, |
| 3023 | .sysfs_ops = &rdev_sysfs_ops, |
| 3024 | .default_attrs = rdev_default_attrs, |
| 3025 | }; |
| 3026 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3027 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3028 | { |
| 3029 | rdev->desc_nr = -1; |
| 3030 | rdev->saved_raid_disk = -1; |
| 3031 | rdev->raid_disk = -1; |
| 3032 | rdev->flags = 0; |
| 3033 | rdev->data_offset = 0; |
| 3034 | rdev->sb_events = 0; |
| 3035 | rdev->last_read_error.tv_sec = 0; |
| 3036 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3037 | rdev->sb_loaded = 0; |
| 3038 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3039 | atomic_set(&rdev->nr_pending, 0); |
| 3040 | atomic_set(&rdev->read_errors, 0); |
| 3041 | atomic_set(&rdev->corrected_errors, 0); |
| 3042 | |
| 3043 | INIT_LIST_HEAD(&rdev->same_set); |
| 3044 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3045 | |
| 3046 | /* Add space to store bad block list. |
| 3047 | * This reserves the space even on arrays where it cannot |
| 3048 | * be used - I wonder if that matters |
| 3049 | */ |
| 3050 | rdev->badblocks.count = 0; |
| 3051 | rdev->badblocks.shift = 0; |
| 3052 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3053 | seqlock_init(&rdev->badblocks.lock); |
| 3054 | if (rdev->badblocks.page == NULL) |
| 3055 | return -ENOMEM; |
| 3056 | |
| 3057 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3058 | } |
| 3059 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | /* |
| 3061 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3062 | * |
| 3063 | * mark the device faulty if: |
| 3064 | * |
| 3065 | * - the device is nonexistent (zero size) |
| 3066 | * - the device has no valid superblock |
| 3067 | * |
| 3068 | * a faulty rdev _never_ has rdev->sb set. |
| 3069 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3070 | 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] | 3071 | { |
| 3072 | char b[BDEVNAME_SIZE]; |
| 3073 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3074 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | sector_t size; |
| 3076 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3077 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | if (!rdev) { |
| 3079 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3080 | return ERR_PTR(-ENOMEM); |
| 3081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3083 | err = md_rdev_init(rdev); |
| 3084 | if (err) |
| 3085 | goto abort_free; |
| 3086 | err = alloc_disk_sb(rdev); |
| 3087 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | goto abort_free; |
| 3089 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3090 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | if (err) |
| 3092 | goto abort_free; |
| 3093 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3094 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3095 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3096 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | if (!size) { |
| 3098 | printk(KERN_WARNING |
| 3099 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3100 | bdevname(rdev->bdev,b)); |
| 3101 | err = -EINVAL; |
| 3102 | goto abort_free; |
| 3103 | } |
| 3104 | |
| 3105 | if (super_format >= 0) { |
| 3106 | err = super_types[super_format]. |
| 3107 | load_super(rdev, NULL, super_minor); |
| 3108 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3109 | printk(KERN_WARNING |
| 3110 | "md: %s does not have a valid v%d.%d " |
| 3111 | "superblock, not importing!\n", |
| 3112 | bdevname(rdev->bdev,b), |
| 3113 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | goto abort_free; |
| 3115 | } |
| 3116 | if (err < 0) { |
| 3117 | printk(KERN_WARNING |
| 3118 | "md: could not read %s's sb, not importing!\n", |
| 3119 | bdevname(rdev->bdev,b)); |
| 3120 | goto abort_free; |
| 3121 | } |
| 3122 | } |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3123 | if (super_format == -1) |
| 3124 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3125 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | return rdev; |
| 3128 | |
| 3129 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3130 | if (rdev->bdev) |
| 3131 | unlock_rdev(rdev); |
| 3132 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3133 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | kfree(rdev); |
| 3135 | return ERR_PTR(err); |
| 3136 | } |
| 3137 | |
| 3138 | /* |
| 3139 | * Check a full RAID array for plausibility |
| 3140 | */ |
| 3141 | |
| 3142 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3143 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | { |
| 3145 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3146 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | char b[BDEVNAME_SIZE]; |
| 3148 | |
| 3149 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3150 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | switch (super_types[mddev->major_version]. |
| 3152 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3153 | case 1: |
| 3154 | freshest = rdev; |
| 3155 | break; |
| 3156 | case 0: |
| 3157 | break; |
| 3158 | default: |
| 3159 | printk( KERN_ERR \ |
| 3160 | "md: fatal superblock inconsistency in %s" |
| 3161 | " -- removing from array\n", |
| 3162 | bdevname(rdev->bdev,b)); |
| 3163 | kick_rdev_from_array(rdev); |
| 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | super_types[mddev->major_version]. |
| 3168 | validate_super(mddev, freshest); |
| 3169 | |
| 3170 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3171 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3172 | if (mddev->max_disks && |
| 3173 | (rdev->desc_nr >= mddev->max_disks || |
| 3174 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3175 | printk(KERN_WARNING |
| 3176 | "md: %s: %s: only %d devices permitted\n", |
| 3177 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3178 | mddev->max_disks); |
| 3179 | kick_rdev_from_array(rdev); |
| 3180 | continue; |
| 3181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3182 | if (rdev != freshest) |
| 3183 | if (super_types[mddev->major_version]. |
| 3184 | validate_super(mddev, rdev)) { |
| 3185 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3186 | " from array!\n", |
| 3187 | bdevname(rdev->bdev,b)); |
| 3188 | kick_rdev_from_array(rdev); |
| 3189 | continue; |
| 3190 | } |
| 3191 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3192 | rdev->desc_nr = i++; |
| 3193 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3194 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3195 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3196 | rdev->raid_disk = -1; |
| 3197 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | } |
| 3199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3202 | /* Read a fixed-point number. |
| 3203 | * Numbers in sysfs attributes should be in "standard" units where |
| 3204 | * possible, so time should be in seconds. |
| 3205 | * However we internally use a a much smaller unit such as |
| 3206 | * milliseconds or jiffies. |
| 3207 | * This function takes a decimal number with a possible fractional |
| 3208 | * component, and produces an integer which is the result of |
| 3209 | * multiplying that number by 10^'scale'. |
| 3210 | * all without any floating-point arithmetic. |
| 3211 | */ |
| 3212 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3213 | { |
| 3214 | unsigned long result = 0; |
| 3215 | long decimals = -1; |
| 3216 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3217 | if (*cp == '.') |
| 3218 | decimals = 0; |
| 3219 | else if (decimals < scale) { |
| 3220 | unsigned int value; |
| 3221 | value = *cp - '0'; |
| 3222 | result = result * 10 + value; |
| 3223 | if (decimals >= 0) |
| 3224 | decimals++; |
| 3225 | } |
| 3226 | cp++; |
| 3227 | } |
| 3228 | if (*cp == '\n') |
| 3229 | cp++; |
| 3230 | if (*cp) |
| 3231 | return -EINVAL; |
| 3232 | if (decimals < 0) |
| 3233 | decimals = 0; |
| 3234 | while (decimals < scale) { |
| 3235 | result *= 10; |
| 3236 | decimals ++; |
| 3237 | } |
| 3238 | *res = result; |
| 3239 | return 0; |
| 3240 | } |
| 3241 | |
| 3242 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3243 | static void md_safemode_timeout(unsigned long data); |
| 3244 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3245 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3246 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3247 | { |
| 3248 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3249 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3250 | } |
| 3251 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3252 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3253 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3254 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3255 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3256 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3257 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3258 | if (msec == 0) |
| 3259 | mddev->safemode_delay = 0; |
| 3260 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3261 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3262 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3263 | if (mddev->safemode_delay == 0) |
| 3264 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3265 | if (mddev->safemode_delay < old_delay) |
| 3266 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3267 | } |
| 3268 | return len; |
| 3269 | } |
| 3270 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3271 | __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] | 3272 | |
| 3273 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3274 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3275 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3276 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3277 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3278 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3279 | else if (mddev->clevel[0]) |
| 3280 | return sprintf(page, "%s\n", mddev->clevel); |
| 3281 | else if (mddev->level != LEVEL_NONE) |
| 3282 | return sprintf(page, "%d\n", mddev->level); |
| 3283 | else |
| 3284 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3285 | } |
| 3286 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3287 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3288 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3289 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3290 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3291 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3292 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3293 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3294 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3295 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3296 | |
| 3297 | if (mddev->pers == NULL) { |
| 3298 | if (len == 0) |
| 3299 | return 0; |
| 3300 | if (len >= sizeof(mddev->clevel)) |
| 3301 | return -ENOSPC; |
| 3302 | strncpy(mddev->clevel, buf, len); |
| 3303 | if (mddev->clevel[len-1] == '\n') |
| 3304 | len--; |
| 3305 | mddev->clevel[len] = 0; |
| 3306 | mddev->level = LEVEL_NONE; |
| 3307 | return rv; |
| 3308 | } |
| 3309 | |
| 3310 | /* request to change the personality. Need to ensure: |
| 3311 | * - array is not engaged in resync/recovery/reshape |
| 3312 | * - old personality can be suspended |
| 3313 | * - new personality will access other array. |
| 3314 | */ |
| 3315 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3316 | if (mddev->sync_thread || |
| 3317 | mddev->reshape_position != MaxSector || |
| 3318 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3319 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3320 | |
| 3321 | if (!mddev->pers->quiesce) { |
| 3322 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3323 | mdname(mddev), mddev->pers->name); |
| 3324 | return -EINVAL; |
| 3325 | } |
| 3326 | |
| 3327 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3328 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3329 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3330 | strncpy(clevel, buf, len); |
| 3331 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3332 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3333 | clevel[len] = 0; |
| 3334 | if (strict_strtol(clevel, 10, &level)) |
| 3335 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3336 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3337 | if (request_module("md-%s", clevel) != 0) |
| 3338 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3339 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3340 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3341 | if (!pers || !try_module_get(pers->owner)) { |
| 3342 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3343 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3344 | return -EINVAL; |
| 3345 | } |
| 3346 | spin_unlock(&pers_lock); |
| 3347 | |
| 3348 | if (pers == mddev->pers) { |
| 3349 | /* Nothing to do! */ |
| 3350 | module_put(pers->owner); |
| 3351 | return rv; |
| 3352 | } |
| 3353 | if (!pers->takeover) { |
| 3354 | module_put(pers->owner); |
| 3355 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3356 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3357 | return -EINVAL; |
| 3358 | } |
| 3359 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3360 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3361 | rdev->new_raid_disk = rdev->raid_disk; |
| 3362 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3363 | /* ->takeover must set new_* and/or delta_disks |
| 3364 | * if it succeeds, and may set them when it fails. |
| 3365 | */ |
| 3366 | priv = pers->takeover(mddev); |
| 3367 | if (IS_ERR(priv)) { |
| 3368 | mddev->new_level = mddev->level; |
| 3369 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3370 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3371 | mddev->raid_disks -= mddev->delta_disks; |
| 3372 | mddev->delta_disks = 0; |
| 3373 | module_put(pers->owner); |
| 3374 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3375 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3376 | return PTR_ERR(priv); |
| 3377 | } |
| 3378 | |
| 3379 | /* Looks like we have a winner */ |
| 3380 | mddev_suspend(mddev); |
| 3381 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3382 | |
| 3383 | if (mddev->pers->sync_request == NULL && |
| 3384 | pers->sync_request != NULL) { |
| 3385 | /* need to add the md_redundancy_group */ |
| 3386 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3387 | printk(KERN_WARNING |
| 3388 | "md: cannot register extra attributes for %s\n", |
| 3389 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3390 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3391 | } |
| 3392 | if (mddev->pers->sync_request != NULL && |
| 3393 | pers->sync_request == NULL) { |
| 3394 | /* need to remove the md_redundancy_group */ |
| 3395 | if (mddev->to_remove == NULL) |
| 3396 | mddev->to_remove = &md_redundancy_group; |
| 3397 | } |
| 3398 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3399 | if (mddev->pers->sync_request == NULL && |
| 3400 | mddev->external) { |
| 3401 | /* We are converting from a no-redundancy array |
| 3402 | * to a redundancy array and metadata is managed |
| 3403 | * externally so we need to be sure that writes |
| 3404 | * won't block due to a need to transition |
| 3405 | * clean->dirty |
| 3406 | * until external management is started. |
| 3407 | */ |
| 3408 | mddev->in_sync = 0; |
| 3409 | mddev->safemode_delay = 0; |
| 3410 | mddev->safemode = 0; |
| 3411 | } |
| 3412 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3413 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3414 | if (rdev->raid_disk < 0) |
| 3415 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3416 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3417 | rdev->new_raid_disk = -1; |
| 3418 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3419 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3420 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3421 | } |
| 3422 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3423 | if (rdev->raid_disk < 0) |
| 3424 | continue; |
| 3425 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3426 | continue; |
| 3427 | rdev->raid_disk = rdev->new_raid_disk; |
| 3428 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3429 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3430 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3431 | if (sysfs_link_rdev(mddev, rdev)) |
| 3432 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3433 | " for %s after level change\n", |
| 3434 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3435 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3439 | mddev->pers = pers; |
| 3440 | mddev->private = priv; |
| 3441 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3442 | mddev->level = mddev->new_level; |
| 3443 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3444 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3445 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3446 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3447 | if (mddev->pers->sync_request == NULL) { |
| 3448 | /* this is now an array without redundancy, so |
| 3449 | * it must always be in_sync |
| 3450 | */ |
| 3451 | mddev->in_sync = 1; |
| 3452 | del_timer_sync(&mddev->safemode_timer); |
| 3453 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3454 | pers->run(mddev); |
| 3455 | mddev_resume(mddev); |
| 3456 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3457 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3458 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3459 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3460 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3461 | return rv; |
| 3462 | } |
| 3463 | |
| 3464 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3465 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3466 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3467 | |
| 3468 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3469 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3470 | { |
| 3471 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3472 | if (mddev->reshape_position != MaxSector && |
| 3473 | mddev->layout != mddev->new_layout) |
| 3474 | return sprintf(page, "%d (%d)\n", |
| 3475 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3476 | return sprintf(page, "%d\n", mddev->layout); |
| 3477 | } |
| 3478 | |
| 3479 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3480 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3481 | { |
| 3482 | char *e; |
| 3483 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3484 | |
| 3485 | if (!*buf || (*e && *e != '\n')) |
| 3486 | return -EINVAL; |
| 3487 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3488 | if (mddev->pers) { |
| 3489 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3490 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3491 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3492 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3493 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3494 | if (err) { |
| 3495 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3496 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3497 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3498 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3499 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3500 | if (mddev->reshape_position == MaxSector) |
| 3501 | mddev->layout = n; |
| 3502 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3503 | return len; |
| 3504 | } |
| 3505 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3506 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3507 | |
| 3508 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3509 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3510 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3511 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3512 | if (mddev->raid_disks == 0) |
| 3513 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3514 | if (mddev->reshape_position != MaxSector && |
| 3515 | mddev->delta_disks != 0) |
| 3516 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3517 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3518 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3519 | } |
| 3520 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3521 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3522 | |
| 3523 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3524 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3525 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3526 | char *e; |
| 3527 | int rv = 0; |
| 3528 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3529 | |
| 3530 | if (!*buf || (*e && *e != '\n')) |
| 3531 | return -EINVAL; |
| 3532 | |
| 3533 | if (mddev->pers) |
| 3534 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3535 | else if (mddev->reshape_position != MaxSector) { |
| 3536 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3537 | mddev->delta_disks = n - olddisks; |
| 3538 | mddev->raid_disks = n; |
| 3539 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3540 | mddev->raid_disks = n; |
| 3541 | return rv ? rv : len; |
| 3542 | } |
| 3543 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3544 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3545 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3546 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3547 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3548 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3549 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3550 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3551 | return sprintf(page, "%d (%d)\n", |
| 3552 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3553 | mddev->chunk_sectors << 9); |
| 3554 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3558 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3559 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3560 | char *e; |
| 3561 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3562 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3563 | if (!*buf || (*e && *e != '\n')) |
| 3564 | return -EINVAL; |
| 3565 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3566 | if (mddev->pers) { |
| 3567 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3568 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3569 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3570 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3571 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3572 | if (err) { |
| 3573 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3574 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3575 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3576 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3577 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3578 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3579 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3580 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3581 | return len; |
| 3582 | } |
| 3583 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3584 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3585 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3586 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3587 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3588 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3589 | if (mddev->recovery_cp == MaxSector) |
| 3590 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3591 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3592 | } |
| 3593 | |
| 3594 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3595 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3596 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3597 | char *e; |
| 3598 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3599 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3600 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3601 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3602 | if (cmd_match(buf, "none")) |
| 3603 | n = MaxSector; |
| 3604 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3605 | return -EINVAL; |
| 3606 | |
| 3607 | mddev->recovery_cp = n; |
| 3608 | return len; |
| 3609 | } |
| 3610 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3611 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3612 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3613 | /* |
| 3614 | * The array state can be: |
| 3615 | * |
| 3616 | * clear |
| 3617 | * No devices, no size, no level |
| 3618 | * Equivalent to STOP_ARRAY ioctl |
| 3619 | * inactive |
| 3620 | * May have some settings, but array is not active |
| 3621 | * all IO results in error |
| 3622 | * When written, doesn't tear down array, but just stops it |
| 3623 | * suspended (not supported yet) |
| 3624 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3625 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3626 | * readonly |
| 3627 | * no resync can happen. no superblocks get written. |
| 3628 | * write requests fail |
| 3629 | * read-auto |
| 3630 | * like readonly, but behaves like 'clean' on a write request. |
| 3631 | * |
| 3632 | * clean - no pending writes, but otherwise active. |
| 3633 | * When written to inactive array, starts without resync |
| 3634 | * If a write request arrives then |
| 3635 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3636 | * if not known, block and switch to write-pending |
| 3637 | * If written to an active array that has pending writes, then fails. |
| 3638 | * active |
| 3639 | * fully active: IO and resync can be happening. |
| 3640 | * When written to inactive array, starts with resync |
| 3641 | * |
| 3642 | * write-pending |
| 3643 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3644 | * |
| 3645 | * active-idle |
| 3646 | * like active, but no writes have been seen for a while (100msec). |
| 3647 | * |
| 3648 | */ |
| 3649 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3650 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3651 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3652 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3653 | "write-pending", "active-idle", NULL }; |
| 3654 | |
| 3655 | static int match_word(const char *word, char **list) |
| 3656 | { |
| 3657 | int n; |
| 3658 | for (n=0; list[n]; n++) |
| 3659 | if (cmd_match(word, list[n])) |
| 3660 | break; |
| 3661 | return n; |
| 3662 | } |
| 3663 | |
| 3664 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3665 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3666 | { |
| 3667 | enum array_state st = inactive; |
| 3668 | |
| 3669 | if (mddev->pers) |
| 3670 | switch(mddev->ro) { |
| 3671 | case 1: |
| 3672 | st = readonly; |
| 3673 | break; |
| 3674 | case 2: |
| 3675 | st = read_auto; |
| 3676 | break; |
| 3677 | case 0: |
| 3678 | if (mddev->in_sync) |
| 3679 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3680 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3681 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3682 | else if (mddev->safemode) |
| 3683 | st = active_idle; |
| 3684 | else |
| 3685 | st = active; |
| 3686 | } |
| 3687 | else { |
| 3688 | if (list_empty(&mddev->disks) && |
| 3689 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3690 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3691 | st = clear; |
| 3692 | else |
| 3693 | st = inactive; |
| 3694 | } |
| 3695 | return sprintf(page, "%s\n", array_states[st]); |
| 3696 | } |
| 3697 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3698 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3699 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3700 | static int do_md_run(struct mddev * mddev); |
| 3701 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3702 | |
| 3703 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3704 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3705 | { |
| 3706 | int err = -EINVAL; |
| 3707 | enum array_state st = match_word(buf, array_states); |
| 3708 | switch(st) { |
| 3709 | case bad_word: |
| 3710 | break; |
| 3711 | case clear: |
| 3712 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3713 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3714 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3715 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3716 | break; |
| 3717 | case inactive: |
| 3718 | /* stopping an active array */ |
| 3719 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3720 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3721 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3722 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3723 | } else |
| 3724 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3725 | break; |
| 3726 | case suspended: |
| 3727 | break; /* not supported yet */ |
| 3728 | case readonly: |
| 3729 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3730 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3731 | else { |
| 3732 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3733 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3734 | err = do_md_run(mddev); |
| 3735 | } |
| 3736 | break; |
| 3737 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3738 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3739 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3740 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3741 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3742 | err = restart_array(mddev); |
| 3743 | if (err == 0) { |
| 3744 | mddev->ro = 2; |
| 3745 | set_disk_ro(mddev->gendisk, 0); |
| 3746 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3747 | } else { |
| 3748 | mddev->ro = 2; |
| 3749 | err = do_md_run(mddev); |
| 3750 | } |
| 3751 | break; |
| 3752 | case clean: |
| 3753 | if (mddev->pers) { |
| 3754 | restart_array(mddev); |
| 3755 | spin_lock_irq(&mddev->write_lock); |
| 3756 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3757 | if (mddev->in_sync == 0) { |
| 3758 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3759 | if (mddev->safemode == 1) |
| 3760 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3761 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3762 | } |
| 3763 | err = 0; |
| 3764 | } else |
| 3765 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3766 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3767 | } else |
| 3768 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3769 | break; |
| 3770 | case active: |
| 3771 | if (mddev->pers) { |
| 3772 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3773 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3774 | wake_up(&mddev->sb_wait); |
| 3775 | err = 0; |
| 3776 | } else { |
| 3777 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3778 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3779 | err = do_md_run(mddev); |
| 3780 | } |
| 3781 | break; |
| 3782 | case write_pending: |
| 3783 | case active_idle: |
| 3784 | /* these cannot be set */ |
| 3785 | break; |
| 3786 | } |
| 3787 | if (err) |
| 3788 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3789 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3790 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3791 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3792 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3793 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3794 | static struct md_sysfs_entry md_array_state = |
| 3795 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3796 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3797 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3798 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3799 | return sprintf(page, "%d\n", |
| 3800 | atomic_read(&mddev->max_corr_read_errors)); |
| 3801 | } |
| 3802 | |
| 3803 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3804 | 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] | 3805 | { |
| 3806 | char *e; |
| 3807 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3808 | |
| 3809 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3810 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3811 | return len; |
| 3812 | } |
| 3813 | return -EINVAL; |
| 3814 | } |
| 3815 | |
| 3816 | static struct md_sysfs_entry max_corr_read_errors = |
| 3817 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3818 | max_corrected_read_errors_store); |
| 3819 | |
| 3820 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3821 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3822 | { |
| 3823 | return -EINVAL; |
| 3824 | } |
| 3825 | |
| 3826 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3827 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3828 | { |
| 3829 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3830 | /* The new device is added to the array. |
| 3831 | * If the array has a persistent superblock, we read the |
| 3832 | * superblock to initialise info and check validity. |
| 3833 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3834 | * which mainly checks size. |
| 3835 | */ |
| 3836 | char *e; |
| 3837 | int major = simple_strtoul(buf, &e, 10); |
| 3838 | int minor; |
| 3839 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3840 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3841 | int err; |
| 3842 | |
| 3843 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3844 | return -EINVAL; |
| 3845 | minor = simple_strtoul(e+1, &e, 10); |
| 3846 | if (*e && *e != '\n') |
| 3847 | return -EINVAL; |
| 3848 | dev = MKDEV(major, minor); |
| 3849 | if (major != MAJOR(dev) || |
| 3850 | minor != MINOR(dev)) |
| 3851 | return -EOVERFLOW; |
| 3852 | |
| 3853 | |
| 3854 | if (mddev->persistent) { |
| 3855 | rdev = md_import_device(dev, mddev->major_version, |
| 3856 | mddev->minor_version); |
| 3857 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3858 | struct md_rdev *rdev0 |
| 3859 | = list_entry(mddev->disks.next, |
| 3860 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3861 | err = super_types[mddev->major_version] |
| 3862 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3863 | if (err < 0) |
| 3864 | goto out; |
| 3865 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3866 | } else if (mddev->external) |
| 3867 | rdev = md_import_device(dev, -2, -1); |
| 3868 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3869 | rdev = md_import_device(dev, -1, -1); |
| 3870 | |
| 3871 | if (IS_ERR(rdev)) |
| 3872 | return PTR_ERR(rdev); |
| 3873 | err = bind_rdev_to_array(rdev, mddev); |
| 3874 | out: |
| 3875 | if (err) |
| 3876 | export_rdev(rdev); |
| 3877 | return err ? err : len; |
| 3878 | } |
| 3879 | |
| 3880 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3881 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3882 | |
| 3883 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3884 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3885 | { |
| 3886 | char *end; |
| 3887 | unsigned long chunk, end_chunk; |
| 3888 | |
| 3889 | if (!mddev->bitmap) |
| 3890 | goto out; |
| 3891 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3892 | while (*buf) { |
| 3893 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3894 | if (buf == end) break; |
| 3895 | if (*end == '-') { /* range */ |
| 3896 | buf = end + 1; |
| 3897 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3898 | if (buf == end) break; |
| 3899 | } |
| 3900 | if (*end && !isspace(*end)) break; |
| 3901 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3902 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3903 | } |
| 3904 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3905 | out: |
| 3906 | return len; |
| 3907 | } |
| 3908 | |
| 3909 | static struct md_sysfs_entry md_bitmap = |
| 3910 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3911 | |
| 3912 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3913 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3914 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3915 | return sprintf(page, "%llu\n", |
| 3916 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3917 | } |
| 3918 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3919 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3920 | |
| 3921 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3922 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3923 | { |
| 3924 | /* If array is inactive, we can reduce the component size, but |
| 3925 | * not increase it (except from 0). |
| 3926 | * If array is active, we can try an on-line resize |
| 3927 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3928 | sector_t sectors; |
| 3929 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3930 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3931 | if (err < 0) |
| 3932 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3933 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3934 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3935 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3936 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3937 | if (mddev->dev_sectors == 0 || |
| 3938 | mddev->dev_sectors > sectors) |
| 3939 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3940 | else |
| 3941 | err = -ENOSPC; |
| 3942 | } |
| 3943 | return err ? err : len; |
| 3944 | } |
| 3945 | |
| 3946 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3947 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3948 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3949 | |
| 3950 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3951 | * This is one of |
| 3952 | * 'none' for arrays with no metadata (good luck...) |
| 3953 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3954 | * or N.M for internally known formats |
| 3955 | */ |
| 3956 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3957 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3958 | { |
| 3959 | if (mddev->persistent) |
| 3960 | return sprintf(page, "%d.%d\n", |
| 3961 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3962 | else if (mddev->external) |
| 3963 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3964 | else |
| 3965 | return sprintf(page, "none\n"); |
| 3966 | } |
| 3967 | |
| 3968 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3969 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3970 | { |
| 3971 | int major, minor; |
| 3972 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3973 | /* Changing the details of 'external' metadata is |
| 3974 | * always permitted. Otherwise there must be |
| 3975 | * no devices attached to the array. |
| 3976 | */ |
| 3977 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3978 | ; |
| 3979 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3980 | return -EBUSY; |
| 3981 | |
| 3982 | if (cmd_match(buf, "none")) { |
| 3983 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3984 | mddev->external = 0; |
| 3985 | mddev->major_version = 0; |
| 3986 | mddev->minor_version = 90; |
| 3987 | return len; |
| 3988 | } |
| 3989 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3990 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3991 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3992 | namelen = sizeof(mddev->metadata_type)-1; |
| 3993 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3994 | mddev->metadata_type[namelen] = 0; |
| 3995 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3996 | mddev->metadata_type[--namelen] = 0; |
| 3997 | mddev->persistent = 0; |
| 3998 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3999 | mddev->major_version = 0; |
| 4000 | mddev->minor_version = 90; |
| 4001 | return len; |
| 4002 | } |
| 4003 | major = simple_strtoul(buf, &e, 10); |
| 4004 | if (e==buf || *e != '.') |
| 4005 | return -EINVAL; |
| 4006 | buf = e+1; |
| 4007 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4008 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4009 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4010 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4011 | return -ENOENT; |
| 4012 | mddev->major_version = major; |
| 4013 | mddev->minor_version = minor; |
| 4014 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4015 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4016 | return len; |
| 4017 | } |
| 4018 | |
| 4019 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4020 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4021 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4022 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4023 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4024 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4025 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4026 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4027 | type = "frozen"; |
| 4028 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4029 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4030 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4031 | type = "reshape"; |
| 4032 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4033 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4034 | type = "resync"; |
| 4035 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4036 | type = "check"; |
| 4037 | else |
| 4038 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4039 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4040 | type = "recover"; |
| 4041 | } |
| 4042 | return sprintf(page, "%s\n", type); |
| 4043 | } |
| 4044 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4045 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4046 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4047 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4048 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4049 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4050 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4051 | return -EINVAL; |
| 4052 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4053 | if (cmd_match(page, "frozen")) |
| 4054 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4055 | else |
| 4056 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4057 | |
| 4058 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4059 | if (mddev->sync_thread) { |
| 4060 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4061 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4062 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4063 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4064 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4065 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4066 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4067 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4068 | else if (cmd_match(page, "recover")) { |
| 4069 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4070 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4071 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4072 | int err; |
| 4073 | if (mddev->pers->start_reshape == NULL) |
| 4074 | return -EINVAL; |
| 4075 | err = mddev->pers->start_reshape(mddev); |
| 4076 | if (err) |
| 4077 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4078 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4079 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4080 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4081 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4082 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4083 | return -EINVAL; |
| 4084 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4085 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4086 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4087 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4088 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4089 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4090 | return len; |
| 4091 | } |
| 4092 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4093 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4094 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4095 | { |
| 4096 | return sprintf(page, "%llu\n", |
| 4097 | (unsigned long long) mddev->resync_mismatches); |
| 4098 | } |
| 4099 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4100 | static struct md_sysfs_entry md_scan_mode = |
| 4101 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4102 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4103 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4104 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4105 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4106 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4107 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4108 | { |
| 4109 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4110 | mddev->sync_speed_min ? "local": "system"); |
| 4111 | } |
| 4112 | |
| 4113 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4114 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4115 | { |
| 4116 | int min; |
| 4117 | char *e; |
| 4118 | if (strncmp(buf, "system", 6)==0) { |
| 4119 | mddev->sync_speed_min = 0; |
| 4120 | return len; |
| 4121 | } |
| 4122 | min = simple_strtoul(buf, &e, 10); |
| 4123 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4124 | return -EINVAL; |
| 4125 | mddev->sync_speed_min = min; |
| 4126 | return len; |
| 4127 | } |
| 4128 | |
| 4129 | static struct md_sysfs_entry md_sync_min = |
| 4130 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4131 | |
| 4132 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4133 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4134 | { |
| 4135 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4136 | mddev->sync_speed_max ? "local": "system"); |
| 4137 | } |
| 4138 | |
| 4139 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4140 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4141 | { |
| 4142 | int max; |
| 4143 | char *e; |
| 4144 | if (strncmp(buf, "system", 6)==0) { |
| 4145 | mddev->sync_speed_max = 0; |
| 4146 | return len; |
| 4147 | } |
| 4148 | max = simple_strtoul(buf, &e, 10); |
| 4149 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4150 | return -EINVAL; |
| 4151 | mddev->sync_speed_max = max; |
| 4152 | return len; |
| 4153 | } |
| 4154 | |
| 4155 | static struct md_sysfs_entry md_sync_max = |
| 4156 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4157 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4158 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4159 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4160 | { |
| 4161 | return sprintf(page, "%d\n", mddev->degraded); |
| 4162 | } |
| 4163 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4164 | |
| 4165 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4166 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4167 | { |
| 4168 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4169 | } |
| 4170 | |
| 4171 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4172 | 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] | 4173 | { |
| 4174 | long n; |
| 4175 | |
| 4176 | if (strict_strtol(buf, 10, &n)) |
| 4177 | return -EINVAL; |
| 4178 | |
| 4179 | if (n != 0 && n != 1) |
| 4180 | return -EINVAL; |
| 4181 | |
| 4182 | mddev->parallel_resync = n; |
| 4183 | |
| 4184 | if (mddev->sync_thread) |
| 4185 | wake_up(&resync_wait); |
| 4186 | |
| 4187 | return len; |
| 4188 | } |
| 4189 | |
| 4190 | /* force parallel resync, even with shared block devices */ |
| 4191 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4192 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4193 | sync_force_parallel_show, sync_force_parallel_store); |
| 4194 | |
| 4195 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4196 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4197 | { |
| 4198 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4199 | if (mddev->curr_resync == 0) |
| 4200 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4201 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4202 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4203 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4204 | db = resync - mddev->resync_mark_cnt; |
| 4205 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4208 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4209 | |
| 4210 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4211 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4212 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4213 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4214 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4215 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4216 | return sprintf(page, "none\n"); |
| 4217 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4218 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4219 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4220 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4221 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4222 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4223 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4224 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4227 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4228 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4229 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4230 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4231 | { |
| 4232 | return sprintf(page, "%llu\n", |
| 4233 | (unsigned long long)mddev->resync_min); |
| 4234 | } |
| 4235 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4236 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4237 | { |
| 4238 | unsigned long long min; |
| 4239 | if (strict_strtoull(buf, 10, &min)) |
| 4240 | return -EINVAL; |
| 4241 | if (min > mddev->resync_max) |
| 4242 | return -EINVAL; |
| 4243 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4244 | return -EBUSY; |
| 4245 | |
| 4246 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4247 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4248 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4249 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4250 | return -EINVAL; |
| 4251 | } |
| 4252 | mddev->resync_min = min; |
| 4253 | |
| 4254 | return len; |
| 4255 | } |
| 4256 | |
| 4257 | static struct md_sysfs_entry md_min_sync = |
| 4258 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4259 | |
| 4260 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4261 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4262 | { |
| 4263 | if (mddev->resync_max == MaxSector) |
| 4264 | return sprintf(page, "max\n"); |
| 4265 | else |
| 4266 | return sprintf(page, "%llu\n", |
| 4267 | (unsigned long long)mddev->resync_max); |
| 4268 | } |
| 4269 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4270 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4271 | { |
| 4272 | if (strncmp(buf, "max", 3) == 0) |
| 4273 | mddev->resync_max = MaxSector; |
| 4274 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4275 | unsigned long long max; |
| 4276 | if (strict_strtoull(buf, 10, &max)) |
| 4277 | return -EINVAL; |
| 4278 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4279 | return -EINVAL; |
| 4280 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4281 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4282 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4283 | return -EBUSY; |
| 4284 | |
| 4285 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4286 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4287 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4288 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4289 | return -EINVAL; |
| 4290 | } |
| 4291 | mddev->resync_max = max; |
| 4292 | } |
| 4293 | wake_up(&mddev->recovery_wait); |
| 4294 | return len; |
| 4295 | } |
| 4296 | |
| 4297 | static struct md_sysfs_entry md_max_sync = |
| 4298 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4299 | |
| 4300 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4301 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4302 | { |
| 4303 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4304 | } |
| 4305 | |
| 4306 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4307 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4308 | { |
| 4309 | char *e; |
| 4310 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4311 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4312 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4313 | if (mddev->pers == NULL || |
| 4314 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4315 | return -EINVAL; |
| 4316 | if (buf == e || (*e && *e != '\n')) |
| 4317 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4318 | |
| 4319 | mddev->suspend_lo = new; |
| 4320 | if (new >= old) |
| 4321 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4322 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4323 | else { |
| 4324 | /* Expanding suspended region - need to wait */ |
| 4325 | mddev->pers->quiesce(mddev, 1); |
| 4326 | mddev->pers->quiesce(mddev, 0); |
| 4327 | } |
| 4328 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4329 | } |
| 4330 | static struct md_sysfs_entry md_suspend_lo = |
| 4331 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4332 | |
| 4333 | |
| 4334 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4335 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4336 | { |
| 4337 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4338 | } |
| 4339 | |
| 4340 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4341 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4342 | { |
| 4343 | char *e; |
| 4344 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4345 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4346 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4347 | if (mddev->pers == NULL || |
| 4348 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4349 | return -EINVAL; |
| 4350 | if (buf == e || (*e && *e != '\n')) |
| 4351 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4352 | |
| 4353 | mddev->suspend_hi = new; |
| 4354 | if (new <= old) |
| 4355 | /* Shrinking suspended region */ |
| 4356 | mddev->pers->quiesce(mddev, 2); |
| 4357 | else { |
| 4358 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4359 | mddev->pers->quiesce(mddev, 1); |
| 4360 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4361 | } |
| 4362 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4363 | } |
| 4364 | static struct md_sysfs_entry md_suspend_hi = |
| 4365 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4366 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4367 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4368 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4369 | { |
| 4370 | if (mddev->reshape_position != MaxSector) |
| 4371 | return sprintf(page, "%llu\n", |
| 4372 | (unsigned long long)mddev->reshape_position); |
| 4373 | strcpy(page, "none\n"); |
| 4374 | return 5; |
| 4375 | } |
| 4376 | |
| 4377 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4378 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4379 | { |
| 4380 | char *e; |
| 4381 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4382 | if (mddev->pers) |
| 4383 | return -EBUSY; |
| 4384 | if (buf == e || (*e && *e != '\n')) |
| 4385 | return -EINVAL; |
| 4386 | mddev->reshape_position = new; |
| 4387 | mddev->delta_disks = 0; |
| 4388 | mddev->new_level = mddev->level; |
| 4389 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4390 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4391 | return len; |
| 4392 | } |
| 4393 | |
| 4394 | static struct md_sysfs_entry md_reshape_position = |
| 4395 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4396 | reshape_position_store); |
| 4397 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4398 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4399 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4400 | { |
| 4401 | if (mddev->external_size) |
| 4402 | return sprintf(page, "%llu\n", |
| 4403 | (unsigned long long)mddev->array_sectors/2); |
| 4404 | else |
| 4405 | return sprintf(page, "default\n"); |
| 4406 | } |
| 4407 | |
| 4408 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4409 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4410 | { |
| 4411 | sector_t sectors; |
| 4412 | |
| 4413 | if (strncmp(buf, "default", 7) == 0) { |
| 4414 | if (mddev->pers) |
| 4415 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4416 | else |
| 4417 | sectors = mddev->array_sectors; |
| 4418 | |
| 4419 | mddev->external_size = 0; |
| 4420 | } else { |
| 4421 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4422 | return -EINVAL; |
| 4423 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4424 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4425 | |
| 4426 | mddev->external_size = 1; |
| 4427 | } |
| 4428 | |
| 4429 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4430 | if (mddev->pers) { |
| 4431 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4432 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4433 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4434 | return len; |
| 4435 | } |
| 4436 | |
| 4437 | static struct md_sysfs_entry md_array_size = |
| 4438 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4439 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4440 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4441 | static struct attribute *md_default_attrs[] = { |
| 4442 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4443 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4444 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4445 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4446 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4447 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4448 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4449 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4450 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4451 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4452 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4453 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4454 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4455 | NULL, |
| 4456 | }; |
| 4457 | |
| 4458 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4459 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4460 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4461 | &md_sync_min.attr, |
| 4462 | &md_sync_max.attr, |
| 4463 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4464 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4465 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4466 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4467 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4468 | &md_suspend_lo.attr, |
| 4469 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4470 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4471 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4472 | NULL, |
| 4473 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4474 | static struct attribute_group md_redundancy_group = { |
| 4475 | .name = NULL, |
| 4476 | .attrs = md_redundancy_attrs, |
| 4477 | }; |
| 4478 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4479 | |
| 4480 | static ssize_t |
| 4481 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4482 | { |
| 4483 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4484 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4485 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4486 | |
| 4487 | if (!entry->show) |
| 4488 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4489 | rv = mddev_lock(mddev); |
| 4490 | if (!rv) { |
| 4491 | rv = entry->show(mddev, page); |
| 4492 | mddev_unlock(mddev); |
| 4493 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4494 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | static ssize_t |
| 4498 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4499 | const char *page, size_t length) |
| 4500 | { |
| 4501 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4502 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4503 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4504 | |
| 4505 | if (!entry->store) |
| 4506 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4507 | if (!capable(CAP_SYS_ADMIN)) |
| 4508 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4509 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4510 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4511 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4512 | if (!rv) { |
| 4513 | rv = entry->store(mddev, page, length); |
| 4514 | mddev_unlock(mddev); |
| 4515 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4516 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | static void md_free(struct kobject *ko) |
| 4520 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4521 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4522 | |
| 4523 | if (mddev->sysfs_state) |
| 4524 | sysfs_put(mddev->sysfs_state); |
| 4525 | |
| 4526 | if (mddev->gendisk) { |
| 4527 | del_gendisk(mddev->gendisk); |
| 4528 | put_disk(mddev->gendisk); |
| 4529 | } |
| 4530 | if (mddev->queue) |
| 4531 | blk_cleanup_queue(mddev->queue); |
| 4532 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4533 | kfree(mddev); |
| 4534 | } |
| 4535 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4536 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4537 | .show = md_attr_show, |
| 4538 | .store = md_attr_store, |
| 4539 | }; |
| 4540 | static struct kobj_type md_ktype = { |
| 4541 | .release = md_free, |
| 4542 | .sysfs_ops = &md_sysfs_ops, |
| 4543 | .default_attrs = md_default_attrs, |
| 4544 | }; |
| 4545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | int mdp_major = 0; |
| 4547 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4548 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4549 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4550 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4551 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4552 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4553 | kobject_del(&mddev->kobj); |
| 4554 | kobject_put(&mddev->kobj); |
| 4555 | } |
| 4556 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4557 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4559 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4560 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4562 | int partitioned; |
| 4563 | int shift; |
| 4564 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4565 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | |
| 4567 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4568 | return -ENODEV; |
| 4569 | |
| 4570 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4571 | shift = partitioned ? MdpMinorShift : 0; |
| 4572 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4574 | /* wait for any previous instance of this device to be |
| 4575 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4576 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4577 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4578 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4579 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4580 | error = -EEXIST; |
| 4581 | if (mddev->gendisk) |
| 4582 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4583 | |
| 4584 | if (name) { |
| 4585 | /* Need to ensure that 'name' is not a duplicate. |
| 4586 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4587 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4588 | spin_lock(&all_mddevs_lock); |
| 4589 | |
| 4590 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4591 | if (mddev2->gendisk && |
| 4592 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4593 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4594 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4595 | } |
| 4596 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4598 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4599 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4600 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4601 | if (!mddev->queue) |
| 4602 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4603 | mddev->queue->queuedata = mddev; |
| 4604 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4605 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | disk = alloc_disk(1 << shift); |
| 4608 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4609 | blk_cleanup_queue(mddev->queue); |
| 4610 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4611 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4613 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4615 | if (name) |
| 4616 | strcpy(disk->disk_name, name); |
| 4617 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4619 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | disk->fops = &md_fops; |
| 4622 | disk->private_data = mddev; |
| 4623 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4624 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4625 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4626 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4627 | * remove it now. |
| 4628 | */ |
| 4629 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4631 | /* As soon as we call add_disk(), another thread could get |
| 4632 | * through to md_open, so make sure it doesn't get too far |
| 4633 | */ |
| 4634 | mutex_lock(&mddev->open_mutex); |
| 4635 | add_disk(disk); |
| 4636 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4637 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4638 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4639 | if (error) { |
| 4640 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4641 | * __must_check, we must do something with the result |
| 4642 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4643 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4644 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4645 | error = 0; |
| 4646 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4647 | if (mddev->kobj.sd && |
| 4648 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4649 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4650 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4651 | abort: |
| 4652 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4653 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4654 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4655 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4656 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4657 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4658 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4662 | { |
| 4663 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | return NULL; |
| 4665 | } |
| 4666 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4667 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4668 | { |
| 4669 | /* val must be "md_*" where * is not all digits. |
| 4670 | * We allocate an array with a large free minor number, and |
| 4671 | * set the name to val. val must not already be an active name. |
| 4672 | */ |
| 4673 | int len = strlen(val); |
| 4674 | char buf[DISK_NAME_LEN]; |
| 4675 | |
| 4676 | while (len && val[len-1] == '\n') |
| 4677 | len--; |
| 4678 | if (len >= DISK_NAME_LEN) |
| 4679 | return -E2BIG; |
| 4680 | strlcpy(buf, val, len+1); |
| 4681 | if (strncmp(buf, "md_", 3) != 0) |
| 4682 | return -EINVAL; |
| 4683 | return md_alloc(0, buf); |
| 4684 | } |
| 4685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | static void md_safemode_timeout(unsigned long data) |
| 4687 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4688 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4690 | if (!atomic_read(&mddev->writes_pending)) { |
| 4691 | mddev->safemode = 1; |
| 4692 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4693 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | md_wakeup_thread(mddev->thread); |
| 4696 | } |
| 4697 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4698 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4702 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4703 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4704 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4705 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4706 | if (list_empty(&mddev->disks)) |
| 4707 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | |
| 4710 | if (mddev->pers) |
| 4711 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4712 | /* Cannot run until previous stop completes properly */ |
| 4713 | if (mddev->sysfs_active) |
| 4714 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | /* |
| 4717 | * Analyze all RAID superblock(s) |
| 4718 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4719 | if (!mddev->raid_disks) { |
| 4720 | if (!mddev->persistent) |
| 4721 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4722 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4725 | if (mddev->level != LEVEL_NONE) |
| 4726 | request_module("md-level-%d", mddev->level); |
| 4727 | else if (mddev->clevel[0]) |
| 4728 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | |
| 4730 | /* |
| 4731 | * Drop all container device buffers, from now on |
| 4732 | * the only valid external interface is through the md |
| 4733 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4735 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4736 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | continue; |
| 4738 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4739 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4740 | |
| 4741 | /* perform some consistency tests on the device. |
| 4742 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4743 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4744 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4745 | if (rdev->meta_bdev) { |
| 4746 | /* Nothing to check */; |
| 4747 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4748 | if (mddev->dev_sectors && |
| 4749 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4750 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4751 | printk("md: %s: data overlaps metadata\n", |
| 4752 | mdname(mddev)); |
| 4753 | return -EINVAL; |
| 4754 | } |
| 4755 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4756 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4757 | > rdev->data_offset) { |
| 4758 | printk("md: %s: metadata overlaps data\n", |
| 4759 | mdname(mddev)); |
| 4760 | return -EINVAL; |
| 4761 | } |
| 4762 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4763 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4766 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4767 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4768 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4771 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4772 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4774 | if (mddev->level != LEVEL_NONE) |
| 4775 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4776 | mddev->level); |
| 4777 | else |
| 4778 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4779 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | return -EINVAL; |
| 4781 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4782 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4784 | if (mddev->level != pers->level) { |
| 4785 | mddev->level = pers->level; |
| 4786 | mddev->new_level = pers->level; |
| 4787 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4788 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4790 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4791 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4792 | /* This personality cannot handle reshaping... */ |
| 4793 | mddev->pers = NULL; |
| 4794 | module_put(pers->owner); |
| 4795 | return -EINVAL; |
| 4796 | } |
| 4797 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4798 | if (pers->sync_request) { |
| 4799 | /* Warn if this is a potentially silly |
| 4800 | * configuration. |
| 4801 | */ |
| 4802 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4803 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4804 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4805 | |
| 4806 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4807 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4808 | if (rdev < rdev2 && |
| 4809 | rdev->bdev->bd_contains == |
| 4810 | rdev2->bdev->bd_contains) { |
| 4811 | printk(KERN_WARNING |
| 4812 | "%s: WARNING: %s appears to be" |
| 4813 | " on the same physical disk as" |
| 4814 | " %s.\n", |
| 4815 | mdname(mddev), |
| 4816 | bdevname(rdev->bdev,b), |
| 4817 | bdevname(rdev2->bdev,b2)); |
| 4818 | warned = 1; |
| 4819 | } |
| 4820 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4821 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4822 | if (warned) |
| 4823 | printk(KERN_WARNING |
| 4824 | "True protection against single-disk" |
| 4825 | " failure might be compromised.\n"); |
| 4826 | } |
| 4827 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4828 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4829 | /* may be over-ridden by personality */ |
| 4830 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4831 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4832 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4834 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4835 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4836 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4837 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4838 | if (err) |
| 4839 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4840 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4841 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4842 | " but 'external_size' not in effect?\n", __func__); |
| 4843 | printk(KERN_ERR |
| 4844 | "md: invalid array_size %llu > default size %llu\n", |
| 4845 | (unsigned long long)mddev->array_sectors / 2, |
| 4846 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4847 | err = -EINVAL; |
| 4848 | mddev->pers->stop(mddev); |
| 4849 | } |
| 4850 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4851 | err = bitmap_create(mddev); |
| 4852 | if (err) { |
| 4853 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4854 | mdname(mddev), err); |
| 4855 | mddev->pers->stop(mddev); |
| 4856 | } |
| 4857 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | module_put(mddev->pers->owner); |
| 4860 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4861 | bitmap_destroy(mddev); |
| 4862 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4864 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4865 | if (mddev->kobj.sd && |
| 4866 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4867 | printk(KERN_WARNING |
| 4868 | "md: cannot register extra attributes for %s\n", |
| 4869 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4870 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4871 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4872 | mddev->ro = 0; |
| 4873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4875 | atomic_set(&mddev->max_corr_read_errors, |
| 4876 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | mddev->safemode = 0; |
| 4878 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4879 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4880 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4882 | smp_wmb(); |
| 4883 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4884 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4885 | if (rdev->raid_disk >= 0) |
| 4886 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4887 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
| 4889 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4890 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4891 | if (mddev->flags) |
| 4892 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4894 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4895 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4896 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4897 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | return 0; |
| 4899 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4900 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4902 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4903 | { |
| 4904 | int err; |
| 4905 | |
| 4906 | err = md_run(mddev); |
| 4907 | if (err) |
| 4908 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4909 | err = bitmap_load(mddev); |
| 4910 | if (err) { |
| 4911 | bitmap_destroy(mddev); |
| 4912 | goto out; |
| 4913 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4914 | |
| 4915 | md_wakeup_thread(mddev->thread); |
| 4916 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4917 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4918 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4919 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4920 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4921 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4922 | out: |
| 4923 | return err; |
| 4924 | } |
| 4925 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4926 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | { |
| 4928 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4930 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4932 | return -ENXIO; |
| 4933 | if (!mddev->pers) |
| 4934 | return -EINVAL; |
| 4935 | if (!mddev->ro) |
| 4936 | return -EBUSY; |
| 4937 | mddev->safemode = 0; |
| 4938 | mddev->ro = 0; |
| 4939 | set_disk_ro(disk, 0); |
| 4940 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4941 | mdname(mddev)); |
| 4942 | /* Kick recovery or resync if necessary */ |
| 4943 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4944 | md_wakeup_thread(mddev->thread); |
| 4945 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4946 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4947 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | } |
| 4949 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4950 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4951 | * to the file ourselves */ |
| 4952 | static int deny_bitmap_write_access(struct file * file) |
| 4953 | { |
| 4954 | struct inode *inode = file->f_mapping->host; |
| 4955 | |
| 4956 | spin_lock(&inode->i_lock); |
| 4957 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4958 | spin_unlock(&inode->i_lock); |
| 4959 | return -ETXTBSY; |
| 4960 | } |
| 4961 | atomic_set(&inode->i_writecount, -1); |
| 4962 | spin_unlock(&inode->i_lock); |
| 4963 | |
| 4964 | return 0; |
| 4965 | } |
| 4966 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4967 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4968 | { |
| 4969 | struct inode *inode = file->f_mapping->host; |
| 4970 | |
| 4971 | spin_lock(&inode->i_lock); |
| 4972 | atomic_set(&inode->i_writecount, 1); |
| 4973 | spin_unlock(&inode->i_lock); |
| 4974 | } |
| 4975 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4976 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4977 | { |
| 4978 | mddev->array_sectors = 0; |
| 4979 | mddev->external_size = 0; |
| 4980 | mddev->dev_sectors = 0; |
| 4981 | mddev->raid_disks = 0; |
| 4982 | mddev->recovery_cp = 0; |
| 4983 | mddev->resync_min = 0; |
| 4984 | mddev->resync_max = MaxSector; |
| 4985 | mddev->reshape_position = MaxSector; |
| 4986 | mddev->external = 0; |
| 4987 | mddev->persistent = 0; |
| 4988 | mddev->level = LEVEL_NONE; |
| 4989 | mddev->clevel[0] = 0; |
| 4990 | mddev->flags = 0; |
| 4991 | mddev->ro = 0; |
| 4992 | mddev->metadata_type[0] = 0; |
| 4993 | mddev->chunk_sectors = 0; |
| 4994 | mddev->ctime = mddev->utime = 0; |
| 4995 | mddev->layout = 0; |
| 4996 | mddev->max_disks = 0; |
| 4997 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4998 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4999 | mddev->delta_disks = 0; |
| 5000 | mddev->new_level = LEVEL_NONE; |
| 5001 | mddev->new_layout = 0; |
| 5002 | mddev->new_chunk_sectors = 0; |
| 5003 | mddev->curr_resync = 0; |
| 5004 | mddev->resync_mismatches = 0; |
| 5005 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5006 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5007 | mddev->recovery = 0; |
| 5008 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5009 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5010 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5011 | mddev->safemode = 0; |
| 5012 | mddev->bitmap_info.offset = 0; |
| 5013 | mddev->bitmap_info.default_offset = 0; |
| 5014 | mddev->bitmap_info.chunksize = 0; |
| 5015 | mddev->bitmap_info.daemon_sleep = 0; |
| 5016 | mddev->bitmap_info.max_write_behind = 0; |
| 5017 | } |
| 5018 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5019 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5020 | { |
| 5021 | if (mddev->sync_thread) { |
| 5022 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5023 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5024 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | del_timer_sync(&mddev->safemode_timer); |
| 5028 | |
| 5029 | bitmap_flush(mddev); |
| 5030 | md_super_wait(mddev); |
| 5031 | |
| 5032 | if (!mddev->in_sync || mddev->flags) { |
| 5033 | /* mark array as shutdown cleanly */ |
| 5034 | mddev->in_sync = 1; |
| 5035 | md_update_sb(mddev, 1); |
| 5036 | } |
| 5037 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5038 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5039 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5040 | { |
| 5041 | mddev_lock(mddev); |
| 5042 | __md_stop_writes(mddev); |
| 5043 | mddev_unlock(mddev); |
| 5044 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5045 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5046 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5047 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5048 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5049 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5050 | mddev->pers->stop(mddev); |
| 5051 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5052 | mddev->to_remove = &md_redundancy_group; |
| 5053 | module_put(mddev->pers->owner); |
| 5054 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5055 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5056 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5057 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5058 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5059 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5060 | { |
| 5061 | int err = 0; |
| 5062 | mutex_lock(&mddev->open_mutex); |
| 5063 | if (atomic_read(&mddev->openers) > is_open) { |
| 5064 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5065 | err = -EBUSY; |
| 5066 | goto out; |
| 5067 | } |
| 5068 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5069 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5070 | |
| 5071 | err = -ENXIO; |
| 5072 | if (mddev->ro==1) |
| 5073 | goto out; |
| 5074 | mddev->ro = 1; |
| 5075 | set_disk_ro(mddev->gendisk, 1); |
| 5076 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5077 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5078 | err = 0; |
| 5079 | } |
| 5080 | out: |
| 5081 | mutex_unlock(&mddev->open_mutex); |
| 5082 | return err; |
| 5083 | } |
| 5084 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5085 | /* mode: |
| 5086 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5087 | * 2 - stop but do not disassemble array |
| 5088 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5089 | 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] | 5090 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5092 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5094 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5095 | if (atomic_read(&mddev->openers) > is_open || |
| 5096 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5097 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5098 | mutex_unlock(&mddev->open_mutex); |
| 5099 | return -EBUSY; |
| 5100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5102 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5103 | if (mddev->ro) |
| 5104 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5105 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5106 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5107 | md_stop(mddev); |
| 5108 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5109 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5110 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5111 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5112 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5113 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5114 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5115 | if (rdev->raid_disk >= 0) |
| 5116 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5117 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5118 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5119 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5120 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5121 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5122 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5123 | if (mddev->ro) |
| 5124 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5125 | } else |
| 5126 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | /* |
| 5128 | * Free resources if final stop |
| 5129 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5130 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5132 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5133 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5134 | if (mddev->bitmap_info.file) { |
| 5135 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5136 | fput(mddev->bitmap_info.file); |
| 5137 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5138 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5139 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | export_array(mddev); |
| 5142 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5143 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5144 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5145 | if (mddev->hold_active == UNTIL_STOP) |
| 5146 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5147 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5148 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5149 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5150 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5151 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5154 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5155 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5157 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | int err; |
| 5159 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5160 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | |
| 5163 | printk(KERN_INFO "md: running: "); |
| 5164 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5165 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | char b[BDEVNAME_SIZE]; |
| 5167 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5168 | } |
| 5169 | printk("\n"); |
| 5170 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5171 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | if (err) { |
| 5173 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5174 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | /* |
| 5179 | * lets try to run arrays based on all disks that have arrived |
| 5180 | * until now. (those are in pending_raid_disks) |
| 5181 | * |
| 5182 | * the method: pick the first pending disk, collect all disks with |
| 5183 | * the same UUID, remove all from the pending list and put them into |
| 5184 | * the 'same_array' list. Then order this list based on superblock |
| 5185 | * update time (freshest comes first), kick out 'old' disks and |
| 5186 | * compare superblocks. If everything's fine then run it. |
| 5187 | * |
| 5188 | * If "unit" is allocated, then bump its reference count |
| 5189 | */ |
| 5190 | static void autorun_devices(int part) |
| 5191 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5192 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5193 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | char b[BDEVNAME_SIZE]; |
| 5195 | |
| 5196 | printk(KERN_INFO "md: autorun ...\n"); |
| 5197 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5198 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5200 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5202 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | |
| 5204 | printk(KERN_INFO "md: considering %s ...\n", |
| 5205 | bdevname(rdev0->bdev,b)); |
| 5206 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5207 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5209 | printk(KERN_INFO "md: adding %s ...\n", |
| 5210 | bdevname(rdev->bdev,b)); |
| 5211 | list_move(&rdev->same_set, &candidates); |
| 5212 | } |
| 5213 | /* |
| 5214 | * now we have a set of devices, with all of them having |
| 5215 | * mostly sane superblocks. It's time to allocate the |
| 5216 | * mddev. |
| 5217 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5218 | if (part) { |
| 5219 | dev = MKDEV(mdp_major, |
| 5220 | rdev0->preferred_minor << MdpMinorShift); |
| 5221 | unit = MINOR(dev) >> MdpMinorShift; |
| 5222 | } else { |
| 5223 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5224 | unit = MINOR(dev); |
| 5225 | } |
| 5226 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5228 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5229 | break; |
| 5230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | |
| 5232 | md_probe(dev, NULL, NULL); |
| 5233 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5234 | if (!mddev || !mddev->gendisk) { |
| 5235 | if (mddev) |
| 5236 | mddev_put(mddev); |
| 5237 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | "md: cannot allocate memory for md drive.\n"); |
| 5239 | break; |
| 5240 | } |
| 5241 | if (mddev_lock(mddev)) |
| 5242 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5243 | mdname(mddev)); |
| 5244 | else if (mddev->raid_disks || mddev->major_version |
| 5245 | || !list_empty(&mddev->disks)) { |
| 5246 | printk(KERN_WARNING |
| 5247 | "md: %s already running, cannot run %s\n", |
| 5248 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5249 | mddev_unlock(mddev); |
| 5250 | } else { |
| 5251 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5252 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5253 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | list_del_init(&rdev->same_set); |
| 5255 | if (bind_rdev_to_array(rdev, mddev)) |
| 5256 | export_rdev(rdev); |
| 5257 | } |
| 5258 | autorun_array(mddev); |
| 5259 | mddev_unlock(mddev); |
| 5260 | } |
| 5261 | /* on success, candidates will be empty, on error |
| 5262 | * it won't... |
| 5263 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5264 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5265 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | mddev_put(mddev); |
| 5269 | } |
| 5270 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5271 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5272 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | static int get_version(void __user * arg) |
| 5275 | { |
| 5276 | mdu_version_t ver; |
| 5277 | |
| 5278 | ver.major = MD_MAJOR_VERSION; |
| 5279 | ver.minor = MD_MINOR_VERSION; |
| 5280 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5281 | |
| 5282 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5283 | return -EFAULT; |
| 5284 | |
| 5285 | return 0; |
| 5286 | } |
| 5287 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5288 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5289 | { |
| 5290 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5291 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5292 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5294 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5295 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5297 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | failed++; |
| 5299 | else { |
| 5300 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5301 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5302 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | else |
| 5304 | spare++; |
| 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | info.major_version = mddev->major_version; |
| 5309 | info.minor_version = mddev->minor_version; |
| 5310 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5311 | info.ctime = mddev->ctime; |
| 5312 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5313 | info.size = mddev->dev_sectors / 2; |
| 5314 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5315 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | info.nr_disks = nr; |
| 5317 | info.raid_disks = mddev->raid_disks; |
| 5318 | info.md_minor = mddev->md_minor; |
| 5319 | info.not_persistent= !mddev->persistent; |
| 5320 | |
| 5321 | info.utime = mddev->utime; |
| 5322 | info.state = 0; |
| 5323 | if (mddev->in_sync) |
| 5324 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5325 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5326 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5327 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | info.working_disks = working; |
| 5329 | info.failed_disks = failed; |
| 5330 | info.spare_disks = spare; |
| 5331 | |
| 5332 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5333 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | |
| 5335 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5336 | return -EFAULT; |
| 5337 | |
| 5338 | return 0; |
| 5339 | } |
| 5340 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5341 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5342 | { |
| 5343 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5344 | char *ptr, *buf = NULL; |
| 5345 | int err = -ENOMEM; |
| 5346 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5347 | if (md_allow_write(mddev)) |
| 5348 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5349 | else |
| 5350 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5351 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5352 | if (!file) |
| 5353 | goto out; |
| 5354 | |
| 5355 | /* bitmap disabled, zero the first byte and copy out */ |
| 5356 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5357 | file->pathname[0] = '\0'; |
| 5358 | goto copy_out; |
| 5359 | } |
| 5360 | |
| 5361 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5362 | if (!buf) |
| 5363 | goto out; |
| 5364 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5365 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5366 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5367 | goto out; |
| 5368 | |
| 5369 | strcpy(file->pathname, ptr); |
| 5370 | |
| 5371 | copy_out: |
| 5372 | err = 0; |
| 5373 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5374 | err = -EFAULT; |
| 5375 | out: |
| 5376 | kfree(buf); |
| 5377 | kfree(file); |
| 5378 | return err; |
| 5379 | } |
| 5380 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5381 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | { |
| 5383 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5384 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | |
| 5386 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5387 | return -EFAULT; |
| 5388 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5389 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | if (rdev) { |
| 5391 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5392 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5393 | info.raid_disk = rdev->raid_disk; |
| 5394 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5395 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5397 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5399 | info.state |= (1<<MD_DISK_SYNC); |
| 5400 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5401 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5402 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | } else { |
| 5404 | info.major = info.minor = 0; |
| 5405 | info.raid_disk = -1; |
| 5406 | info.state = (1<<MD_DISK_REMOVED); |
| 5407 | } |
| 5408 | |
| 5409 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5410 | return -EFAULT; |
| 5411 | |
| 5412 | return 0; |
| 5413 | } |
| 5414 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5415 | 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] | 5416 | { |
| 5417 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5418 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | dev_t dev = MKDEV(info->major,info->minor); |
| 5420 | |
| 5421 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5422 | return -EOVERFLOW; |
| 5423 | |
| 5424 | if (!mddev->raid_disks) { |
| 5425 | int err; |
| 5426 | /* expecting a device which has a superblock */ |
| 5427 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5428 | if (IS_ERR(rdev)) { |
| 5429 | printk(KERN_WARNING |
| 5430 | "md: md_import_device returned %ld\n", |
| 5431 | PTR_ERR(rdev)); |
| 5432 | return PTR_ERR(rdev); |
| 5433 | } |
| 5434 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5435 | struct md_rdev *rdev0 |
| 5436 | = list_entry(mddev->disks.next, |
| 5437 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5438 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5440 | if (err < 0) { |
| 5441 | printk(KERN_WARNING |
| 5442 | "md: %s has different UUID to %s\n", |
| 5443 | bdevname(rdev->bdev,b), |
| 5444 | bdevname(rdev0->bdev,b2)); |
| 5445 | export_rdev(rdev); |
| 5446 | return -EINVAL; |
| 5447 | } |
| 5448 | } |
| 5449 | err = bind_rdev_to_array(rdev, mddev); |
| 5450 | if (err) |
| 5451 | export_rdev(rdev); |
| 5452 | return err; |
| 5453 | } |
| 5454 | |
| 5455 | /* |
| 5456 | * add_new_disk can be used once the array is assembled |
| 5457 | * to add "hot spares". They must already have a superblock |
| 5458 | * written |
| 5459 | */ |
| 5460 | if (mddev->pers) { |
| 5461 | int err; |
| 5462 | if (!mddev->pers->hot_add_disk) { |
| 5463 | printk(KERN_WARNING |
| 5464 | "%s: personality does not support diskops!\n", |
| 5465 | mdname(mddev)); |
| 5466 | return -EINVAL; |
| 5467 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5468 | if (mddev->persistent) |
| 5469 | rdev = md_import_device(dev, mddev->major_version, |
| 5470 | mddev->minor_version); |
| 5471 | else |
| 5472 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | if (IS_ERR(rdev)) { |
| 5474 | printk(KERN_WARNING |
| 5475 | "md: md_import_device returned %ld\n", |
| 5476 | PTR_ERR(rdev)); |
| 5477 | return PTR_ERR(rdev); |
| 5478 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5479 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5480 | if (!mddev->persistent) { |
| 5481 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5482 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5483 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5484 | set_bit(In_sync, &rdev->flags); |
| 5485 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5486 | rdev->raid_disk = -1; |
| 5487 | } else |
| 5488 | super_types[mddev->major_version]. |
| 5489 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5490 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5491 | (!test_bit(In_sync, &rdev->flags) || |
| 5492 | rdev->raid_disk != info->raid_disk)) { |
| 5493 | /* This was a hot-add request, but events doesn't |
| 5494 | * match, so reject it. |
| 5495 | */ |
| 5496 | export_rdev(rdev); |
| 5497 | return -EINVAL; |
| 5498 | } |
| 5499 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5500 | if (test_bit(In_sync, &rdev->flags)) |
| 5501 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5502 | else |
| 5503 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5504 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5505 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5506 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5507 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5508 | else |
| 5509 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | rdev->raid_disk = -1; |
| 5512 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5513 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5514 | /* If there is hot_add_disk but no hot_remove_disk |
| 5515 | * then added disks for geometry changes, |
| 5516 | * and should be added immediately. |
| 5517 | */ |
| 5518 | super_types[mddev->major_version]. |
| 5519 | validate_super(mddev, rdev); |
| 5520 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5521 | if (err) |
| 5522 | unbind_rdev_from_array(rdev); |
| 5523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | if (err) |
| 5525 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5526 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5527 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5528 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5529 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5530 | if (mddev->degraded) |
| 5531 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5532 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5533 | if (!err) |
| 5534 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5535 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | return err; |
| 5537 | } |
| 5538 | |
| 5539 | /* otherwise, add_new_disk is only allowed |
| 5540 | * for major_version==0 superblocks |
| 5541 | */ |
| 5542 | if (mddev->major_version != 0) { |
| 5543 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5544 | mdname(mddev)); |
| 5545 | return -EINVAL; |
| 5546 | } |
| 5547 | |
| 5548 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5549 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5550 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | if (IS_ERR(rdev)) { |
| 5552 | printk(KERN_WARNING |
| 5553 | "md: error, md_import_device() returned %ld\n", |
| 5554 | PTR_ERR(rdev)); |
| 5555 | return PTR_ERR(rdev); |
| 5556 | } |
| 5557 | rdev->desc_nr = info->number; |
| 5558 | if (info->raid_disk < mddev->raid_disks) |
| 5559 | rdev->raid_disk = info->raid_disk; |
| 5560 | else |
| 5561 | rdev->raid_disk = -1; |
| 5562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5564 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5565 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5567 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5568 | set_bit(WriteMostly, &rdev->flags); |
| 5569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | if (!mddev->persistent) { |
| 5571 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5572 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5573 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5574 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5575 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5577 | err = bind_rdev_to_array(rdev, mddev); |
| 5578 | if (err) { |
| 5579 | export_rdev(rdev); |
| 5580 | return err; |
| 5581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | return 0; |
| 5585 | } |
| 5586 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5587 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | { |
| 5589 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5590 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | rdev = find_rdev(mddev, dev); |
| 5593 | if (!rdev) |
| 5594 | return -ENXIO; |
| 5595 | |
| 5596 | if (rdev->raid_disk >= 0) |
| 5597 | goto busy; |
| 5598 | |
| 5599 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5600 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5601 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | |
| 5603 | return 0; |
| 5604 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5605 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5607 | return -EBUSY; |
| 5608 | } |
| 5609 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5610 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | { |
| 5612 | char b[BDEVNAME_SIZE]; |
| 5613 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5614 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | |
| 5616 | if (!mddev->pers) |
| 5617 | return -ENODEV; |
| 5618 | |
| 5619 | if (mddev->major_version != 0) { |
| 5620 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5621 | " version-0 superblocks.\n", |
| 5622 | mdname(mddev)); |
| 5623 | return -EINVAL; |
| 5624 | } |
| 5625 | if (!mddev->pers->hot_add_disk) { |
| 5626 | printk(KERN_WARNING |
| 5627 | "%s: personality does not support diskops!\n", |
| 5628 | mdname(mddev)); |
| 5629 | return -EINVAL; |
| 5630 | } |
| 5631 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5632 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | if (IS_ERR(rdev)) { |
| 5634 | printk(KERN_WARNING |
| 5635 | "md: error, md_import_device() returned %ld\n", |
| 5636 | PTR_ERR(rdev)); |
| 5637 | return -EINVAL; |
| 5638 | } |
| 5639 | |
| 5640 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5641 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5643 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5645 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5647 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | printk(KERN_WARNING |
| 5649 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5650 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5651 | err = -EINVAL; |
| 5652 | goto abort_export; |
| 5653 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5654 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5656 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5657 | err = bind_rdev_to_array(rdev, mddev); |
| 5658 | if (err) |
| 5659 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | |
| 5661 | /* |
| 5662 | * The rest should better be atomic, we can have disk failures |
| 5663 | * noticed in interrupt contexts ... |
| 5664 | */ |
| 5665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | rdev->raid_disk = -1; |
| 5667 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5668 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | |
| 5670 | /* |
| 5671 | * Kick recovery, maybe this spare has to be added to the |
| 5672 | * array immediately. |
| 5673 | */ |
| 5674 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5675 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5676 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | return 0; |
| 5678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | abort_export: |
| 5680 | export_rdev(rdev); |
| 5681 | return err; |
| 5682 | } |
| 5683 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5684 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5685 | { |
| 5686 | int err; |
| 5687 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5688 | if (mddev->pers) { |
| 5689 | if (!mddev->pers->quiesce) |
| 5690 | return -EBUSY; |
| 5691 | if (mddev->recovery || mddev->sync_thread) |
| 5692 | return -EBUSY; |
| 5693 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5694 | } |
| 5695 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5696 | |
| 5697 | if (fd >= 0) { |
| 5698 | if (mddev->bitmap) |
| 5699 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5700 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5701 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5702 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5703 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5704 | mdname(mddev)); |
| 5705 | return -EBADF; |
| 5706 | } |
| 5707 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5708 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5709 | if (err) { |
| 5710 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5711 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5712 | fput(mddev->bitmap_info.file); |
| 5713 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5714 | return err; |
| 5715 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5716 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5717 | } else if (mddev->bitmap == NULL) |
| 5718 | return -ENOENT; /* cannot remove what isn't there */ |
| 5719 | err = 0; |
| 5720 | if (mddev->pers) { |
| 5721 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5722 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5723 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5724 | if (!err) |
| 5725 | err = bitmap_load(mddev); |
| 5726 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5727 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5728 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5729 | fd = -1; /* make sure to put the file */ |
| 5730 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5731 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5732 | } |
| 5733 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5734 | if (mddev->bitmap_info.file) { |
| 5735 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5736 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5737 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5738 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5739 | } |
| 5740 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5741 | return err; |
| 5742 | } |
| 5743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | /* |
| 5745 | * set_array_info is used two different ways |
| 5746 | * The original usage is when creating a new array. |
| 5747 | * In this usage, raid_disks is > 0 and it together with |
| 5748 | * level, size, not_persistent,layout,chunksize determine the |
| 5749 | * shape of the array. |
| 5750 | * This will always create an array with a type-0.90.0 superblock. |
| 5751 | * The newer usage is when assembling an array. |
| 5752 | * In this case raid_disks will be 0, and the major_version field is |
| 5753 | * use to determine which style super-blocks are to be found on the devices. |
| 5754 | * The minor and patch _version numbers are also kept incase the |
| 5755 | * super_block handler wishes to interpret them. |
| 5756 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5757 | 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] | 5758 | { |
| 5759 | |
| 5760 | if (info->raid_disks == 0) { |
| 5761 | /* just setting version number for superblock loading */ |
| 5762 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5763 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | super_types[info->major_version].name == NULL) { |
| 5765 | /* maybe try to auto-load a module? */ |
| 5766 | printk(KERN_INFO |
| 5767 | "md: superblock version %d not known\n", |
| 5768 | info->major_version); |
| 5769 | return -EINVAL; |
| 5770 | } |
| 5771 | mddev->major_version = info->major_version; |
| 5772 | mddev->minor_version = info->minor_version; |
| 5773 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5774 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5775 | /* ensure mddev_put doesn't delete this now that there |
| 5776 | * is some minimal configuration. |
| 5777 | */ |
| 5778 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | return 0; |
| 5780 | } |
| 5781 | mddev->major_version = MD_MAJOR_VERSION; |
| 5782 | mddev->minor_version = MD_MINOR_VERSION; |
| 5783 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5784 | mddev->ctime = get_seconds(); |
| 5785 | |
| 5786 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5787 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5788 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | mddev->raid_disks = info->raid_disks; |
| 5790 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5791 | * openned |
| 5792 | */ |
| 5793 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5794 | mddev->recovery_cp = MaxSector; |
| 5795 | else |
| 5796 | mddev->recovery_cp = 0; |
| 5797 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5798 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | |
| 5800 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5801 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5802 | |
| 5803 | mddev->max_disks = MD_SB_DISKS; |
| 5804 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5805 | if (mddev->persistent) |
| 5806 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5807 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5809 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5810 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5811 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5812 | mddev->reshape_position = MaxSector; |
| 5813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | /* |
| 5815 | * Generate a 128 bit UUID |
| 5816 | */ |
| 5817 | get_random_bytes(mddev->uuid, 16); |
| 5818 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5819 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5820 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5821 | mddev->new_layout = mddev->layout; |
| 5822 | mddev->delta_disks = 0; |
| 5823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | return 0; |
| 5825 | } |
| 5826 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5827 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5828 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5829 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5830 | |
| 5831 | if (mddev->external_size) |
| 5832 | return; |
| 5833 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5834 | mddev->array_sectors = array_sectors; |
| 5835 | } |
| 5836 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5837 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5838 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5839 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5840 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5841 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5842 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5843 | |
| 5844 | if (mddev->pers->resize == NULL) |
| 5845 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5846 | /* The "num_sectors" is the number of sectors of each device that |
| 5847 | * is used. This can only make sense for arrays with redundancy. |
| 5848 | * linear and raid0 always use whatever space is available. We can only |
| 5849 | * consider changing this number if no resync or reconstruction is |
| 5850 | * 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] | 5851 | * 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] | 5852 | * of each device. If num_sectors is zero, we find the largest size |
| 5853 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5854 | */ |
| 5855 | if (mddev->sync_thread) |
| 5856 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5857 | if (mddev->bitmap) |
| 5858 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5859 | * grow, and re-add. |
| 5860 | */ |
| 5861 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5862 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5863 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5864 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5865 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5866 | num_sectors = avail; |
| 5867 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5868 | return -ENOSPC; |
| 5869 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5870 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5871 | if (!rv) |
| 5872 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5873 | return rv; |
| 5874 | } |
| 5875 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5876 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5877 | { |
| 5878 | int rv; |
| 5879 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5880 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5881 | return -EINVAL; |
| 5882 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5883 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5884 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5885 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5886 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5887 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5888 | |
| 5889 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5890 | if (rv < 0) |
| 5891 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5892 | return rv; |
| 5893 | } |
| 5894 | |
| 5895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | /* |
| 5897 | * update_array_info is used to change the configuration of an |
| 5898 | * on-line array. |
| 5899 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5900 | * fields in the info are checked against the array. |
| 5901 | * Any differences that cannot be handled will cause an error. |
| 5902 | * Normally, only one change can be managed at a time. |
| 5903 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5904 | 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] | 5905 | { |
| 5906 | int rv = 0; |
| 5907 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5908 | int state = 0; |
| 5909 | |
| 5910 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5911 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5912 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | |
| 5914 | if (mddev->major_version != info->major_version || |
| 5915 | mddev->minor_version != info->minor_version || |
| 5916 | /* mddev->patch_version != info->patch_version || */ |
| 5917 | mddev->ctime != info->ctime || |
| 5918 | mddev->level != info->level || |
| 5919 | /* mddev->layout != info->layout || */ |
| 5920 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5921 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5922 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5923 | ((state^info->state) & 0xfffffe00) |
| 5924 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | return -EINVAL; |
| 5926 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5927 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5928 | cnt++; |
| 5929 | if (mddev->raid_disks != info->raid_disks) |
| 5930 | cnt++; |
| 5931 | if (mddev->layout != info->layout) |
| 5932 | cnt++; |
| 5933 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5934 | cnt++; |
| 5935 | if (cnt == 0) |
| 5936 | return 0; |
| 5937 | if (cnt > 1) |
| 5938 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | |
| 5940 | if (mddev->layout != info->layout) { |
| 5941 | /* Change layout |
| 5942 | * we don't need to do anything at the md level, the |
| 5943 | * personality will take care of it all. |
| 5944 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5945 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5947 | else { |
| 5948 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5949 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5950 | if (rv) |
| 5951 | mddev->new_layout = mddev->layout; |
| 5952 | return rv; |
| 5953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5955 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5956 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5958 | if (mddev->raid_disks != info->raid_disks) |
| 5959 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5961 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5962 | if (mddev->pers->quiesce == NULL) |
| 5963 | return -EINVAL; |
| 5964 | if (mddev->recovery || mddev->sync_thread) |
| 5965 | return -EBUSY; |
| 5966 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5967 | /* add the bitmap */ |
| 5968 | if (mddev->bitmap) |
| 5969 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5970 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5971 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5972 | mddev->bitmap_info.offset = |
| 5973 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5974 | mddev->pers->quiesce(mddev, 1); |
| 5975 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5976 | if (!rv) |
| 5977 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5978 | if (rv) |
| 5979 | bitmap_destroy(mddev); |
| 5980 | mddev->pers->quiesce(mddev, 0); |
| 5981 | } else { |
| 5982 | /* remove the bitmap */ |
| 5983 | if (!mddev->bitmap) |
| 5984 | return -ENOENT; |
| 5985 | if (mddev->bitmap->file) |
| 5986 | return -EINVAL; |
| 5987 | mddev->pers->quiesce(mddev, 1); |
| 5988 | bitmap_destroy(mddev); |
| 5989 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5990 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5991 | } |
| 5992 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5993 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | return rv; |
| 5995 | } |
| 5996 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5997 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5999 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | |
| 6001 | if (mddev->pers == NULL) |
| 6002 | return -ENODEV; |
| 6003 | |
| 6004 | rdev = find_rdev(mddev, dev); |
| 6005 | if (!rdev) |
| 6006 | return -ENODEV; |
| 6007 | |
| 6008 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6009 | if (!test_bit(Faulty, &rdev->flags)) |
| 6010 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | return 0; |
| 6012 | } |
| 6013 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6014 | /* |
| 6015 | * We have a problem here : there is no easy way to give a CHS |
| 6016 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6017 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6018 | * dosfs just mad... ;-) |
| 6019 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6020 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6021 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6022 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6023 | |
| 6024 | geo->heads = 2; |
| 6025 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6026 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6027 | return 0; |
| 6028 | } |
| 6029 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6030 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | unsigned int cmd, unsigned long arg) |
| 6032 | { |
| 6033 | int err = 0; |
| 6034 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6035 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6036 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | |
| 6038 | if (!capable(CAP_SYS_ADMIN)) |
| 6039 | return -EACCES; |
| 6040 | |
| 6041 | /* |
| 6042 | * Commands dealing with the RAID driver but not any |
| 6043 | * particular array: |
| 6044 | */ |
| 6045 | switch (cmd) |
| 6046 | { |
| 6047 | case RAID_VERSION: |
| 6048 | err = get_version(argp); |
| 6049 | goto done; |
| 6050 | |
| 6051 | case PRINT_RAID_DEBUG: |
| 6052 | err = 0; |
| 6053 | md_print_devices(); |
| 6054 | goto done; |
| 6055 | |
| 6056 | #ifndef MODULE |
| 6057 | case RAID_AUTORUN: |
| 6058 | err = 0; |
| 6059 | autostart_arrays(arg); |
| 6060 | goto done; |
| 6061 | #endif |
| 6062 | default:; |
| 6063 | } |
| 6064 | |
| 6065 | /* |
| 6066 | * Commands creating/starting a new array: |
| 6067 | */ |
| 6068 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6069 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
| 6071 | if (!mddev) { |
| 6072 | BUG(); |
| 6073 | goto abort; |
| 6074 | } |
| 6075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | err = mddev_lock(mddev); |
| 6077 | if (err) { |
| 6078 | printk(KERN_INFO |
| 6079 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6080 | err, cmd); |
| 6081 | goto abort; |
| 6082 | } |
| 6083 | |
| 6084 | switch (cmd) |
| 6085 | { |
| 6086 | case SET_ARRAY_INFO: |
| 6087 | { |
| 6088 | mdu_array_info_t info; |
| 6089 | if (!arg) |
| 6090 | memset(&info, 0, sizeof(info)); |
| 6091 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6092 | err = -EFAULT; |
| 6093 | goto abort_unlock; |
| 6094 | } |
| 6095 | if (mddev->pers) { |
| 6096 | err = update_array_info(mddev, &info); |
| 6097 | if (err) { |
| 6098 | printk(KERN_WARNING "md: couldn't update" |
| 6099 | " array info. %d\n", err); |
| 6100 | goto abort_unlock; |
| 6101 | } |
| 6102 | goto done_unlock; |
| 6103 | } |
| 6104 | if (!list_empty(&mddev->disks)) { |
| 6105 | printk(KERN_WARNING |
| 6106 | "md: array %s already has disks!\n", |
| 6107 | mdname(mddev)); |
| 6108 | err = -EBUSY; |
| 6109 | goto abort_unlock; |
| 6110 | } |
| 6111 | if (mddev->raid_disks) { |
| 6112 | printk(KERN_WARNING |
| 6113 | "md: array %s already initialised!\n", |
| 6114 | mdname(mddev)); |
| 6115 | err = -EBUSY; |
| 6116 | goto abort_unlock; |
| 6117 | } |
| 6118 | err = set_array_info(mddev, &info); |
| 6119 | if (err) { |
| 6120 | printk(KERN_WARNING "md: couldn't set" |
| 6121 | " array info. %d\n", err); |
| 6122 | goto abort_unlock; |
| 6123 | } |
| 6124 | } |
| 6125 | goto done_unlock; |
| 6126 | |
| 6127 | default:; |
| 6128 | } |
| 6129 | |
| 6130 | /* |
| 6131 | * Commands querying/configuring an existing array: |
| 6132 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6133 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6134 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6135 | if ((!mddev->raid_disks && !mddev->external) |
| 6136 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6137 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6138 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | err = -ENODEV; |
| 6140 | goto abort_unlock; |
| 6141 | } |
| 6142 | |
| 6143 | /* |
| 6144 | * Commands even a read-only array can execute: |
| 6145 | */ |
| 6146 | switch (cmd) |
| 6147 | { |
| 6148 | case GET_ARRAY_INFO: |
| 6149 | err = get_array_info(mddev, argp); |
| 6150 | goto done_unlock; |
| 6151 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6152 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6153 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6154 | goto done_unlock; |
| 6155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | case GET_DISK_INFO: |
| 6157 | err = get_disk_info(mddev, argp); |
| 6158 | goto done_unlock; |
| 6159 | |
| 6160 | case RESTART_ARRAY_RW: |
| 6161 | err = restart_array(mddev); |
| 6162 | goto done_unlock; |
| 6163 | |
| 6164 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6165 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | goto done_unlock; |
| 6167 | |
| 6168 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6169 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | goto done_unlock; |
| 6171 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6172 | case BLKROSET: |
| 6173 | if (get_user(ro, (int __user *)(arg))) { |
| 6174 | err = -EFAULT; |
| 6175 | goto done_unlock; |
| 6176 | } |
| 6177 | err = -EINVAL; |
| 6178 | |
| 6179 | /* if the bdev is going readonly the value of mddev->ro |
| 6180 | * does not matter, no writes are coming |
| 6181 | */ |
| 6182 | if (ro) |
| 6183 | goto done_unlock; |
| 6184 | |
| 6185 | /* are we are already prepared for writes? */ |
| 6186 | if (mddev->ro != 1) |
| 6187 | goto done_unlock; |
| 6188 | |
| 6189 | /* transitioning to readauto need only happen for |
| 6190 | * arrays that call md_write_start |
| 6191 | */ |
| 6192 | if (mddev->pers) { |
| 6193 | err = restart_array(mddev); |
| 6194 | if (err == 0) { |
| 6195 | mddev->ro = 2; |
| 6196 | set_disk_ro(mddev->gendisk, 0); |
| 6197 | } |
| 6198 | } |
| 6199 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | } |
| 6201 | |
| 6202 | /* |
| 6203 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6204 | * superblock, so we do not allow them on read-only arrays. |
| 6205 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6206 | * here and hit the 'default' below, so only disallow |
| 6207 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6209 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6210 | if (mddev->ro == 2) { |
| 6211 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6212 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6213 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6214 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6215 | } else { |
| 6216 | err = -EROFS; |
| 6217 | goto abort_unlock; |
| 6218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | switch (cmd) |
| 6222 | { |
| 6223 | case ADD_NEW_DISK: |
| 6224 | { |
| 6225 | mdu_disk_info_t info; |
| 6226 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6227 | err = -EFAULT; |
| 6228 | else |
| 6229 | err = add_new_disk(mddev, &info); |
| 6230 | goto done_unlock; |
| 6231 | } |
| 6232 | |
| 6233 | case HOT_REMOVE_DISK: |
| 6234 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6235 | goto done_unlock; |
| 6236 | |
| 6237 | case HOT_ADD_DISK: |
| 6238 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6239 | goto done_unlock; |
| 6240 | |
| 6241 | case SET_DISK_FAULTY: |
| 6242 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6243 | goto done_unlock; |
| 6244 | |
| 6245 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6246 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | goto done_unlock; |
| 6248 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6249 | case SET_BITMAP_FILE: |
| 6250 | err = set_bitmap_file(mddev, (int)arg); |
| 6251 | goto done_unlock; |
| 6252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | err = -EINVAL; |
| 6255 | goto abort_unlock; |
| 6256 | } |
| 6257 | |
| 6258 | done_unlock: |
| 6259 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6260 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6261 | err != -EINVAL) |
| 6262 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | mddev_unlock(mddev); |
| 6264 | |
| 6265 | return err; |
| 6266 | done: |
| 6267 | if (err) |
| 6268 | MD_BUG(); |
| 6269 | abort: |
| 6270 | return err; |
| 6271 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6272 | #ifdef CONFIG_COMPAT |
| 6273 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6274 | unsigned int cmd, unsigned long arg) |
| 6275 | { |
| 6276 | switch (cmd) { |
| 6277 | case HOT_REMOVE_DISK: |
| 6278 | case HOT_ADD_DISK: |
| 6279 | case SET_DISK_FAULTY: |
| 6280 | case SET_BITMAP_FILE: |
| 6281 | /* These take in integer arg, do not convert */ |
| 6282 | break; |
| 6283 | default: |
| 6284 | arg = (unsigned long)compat_ptr(arg); |
| 6285 | break; |
| 6286 | } |
| 6287 | |
| 6288 | return md_ioctl(bdev, mode, cmd, arg); |
| 6289 | } |
| 6290 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6292 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | { |
| 6294 | /* |
| 6295 | * Succeed if we can lock the mddev, which confirms that |
| 6296 | * it isn't being stopped right now. |
| 6297 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6298 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | int err; |
| 6300 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6301 | if (mddev->gendisk != bdev->bd_disk) { |
| 6302 | /* we are racing with mddev_put which is discarding this |
| 6303 | * bd_disk. |
| 6304 | */ |
| 6305 | mddev_put(mddev); |
| 6306 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6307 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6308 | /* Then retry the open from the top */ |
| 6309 | return -ERESTARTSYS; |
| 6310 | } |
| 6311 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6312 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6313 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | goto out; |
| 6315 | |
| 6316 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6317 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6318 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6319 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6320 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | out: |
| 6322 | return err; |
| 6323 | } |
| 6324 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6325 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6327 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6329 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6330 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6331 | mddev_put(mddev); |
| 6332 | |
| 6333 | return 0; |
| 6334 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6335 | |
| 6336 | static int md_media_changed(struct gendisk *disk) |
| 6337 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6338 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6339 | |
| 6340 | return mddev->changed; |
| 6341 | } |
| 6342 | |
| 6343 | static int md_revalidate(struct gendisk *disk) |
| 6344 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6345 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6346 | |
| 6347 | mddev->changed = 0; |
| 6348 | return 0; |
| 6349 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6350 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | { |
| 6352 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6353 | .open = md_open, |
| 6354 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6355 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6356 | #ifdef CONFIG_COMPAT |
| 6357 | .compat_ioctl = md_compat_ioctl, |
| 6358 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6359 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6360 | .media_changed = md_media_changed, |
| 6361 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | }; |
| 6363 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6364 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6366 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | /* |
| 6369 | * md_thread is a 'system-thread', it's priority should be very |
| 6370 | * high. We avoid resource deadlocks individually in each |
| 6371 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6372 | * the very same RT priority as kswapd, thus we will never get |
| 6373 | * into a priority inversion deadlock. |
| 6374 | * |
| 6375 | * we definitely have to have equal or higher priority than |
| 6376 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6377 | * many dirty RAID5 blocks. |
| 6378 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6380 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6381 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6383 | /* We need to wait INTERRUPTIBLE so that |
| 6384 | * we don't add to the load-average. |
| 6385 | * That means we need to be sure no signals are |
| 6386 | * pending |
| 6387 | */ |
| 6388 | if (signal_pending(current)) |
| 6389 | flush_signals(current); |
| 6390 | |
| 6391 | wait_event_interruptible_timeout |
| 6392 | (thread->wqueue, |
| 6393 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6394 | || kthread_should_stop(), |
| 6395 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6397 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6398 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6399 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | return 0; |
| 6403 | } |
| 6404 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6405 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | { |
| 6407 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6408 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6410 | wake_up(&thread->wqueue); |
| 6411 | } |
| 6412 | } |
| 6413 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6414 | 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] | 6415 | const char *name) |
| 6416 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6417 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6419 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | if (!thread) |
| 6421 | return NULL; |
| 6422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | init_waitqueue_head(&thread->wqueue); |
| 6424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | thread->run = run; |
| 6426 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6427 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6428 | thread->tsk = kthread_run(md_thread, thread, |
| 6429 | "%s_%s", |
| 6430 | mdname(thread->mddev), |
| 6431 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6432 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | kfree(thread); |
| 6434 | return NULL; |
| 6435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | return thread; |
| 6437 | } |
| 6438 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6439 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6441 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6442 | if (!thread) |
| 6443 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6444 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6445 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6446 | * non-existent thread |
| 6447 | */ |
| 6448 | spin_lock(&pers_lock); |
| 6449 | *threadp = NULL; |
| 6450 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6451 | |
| 6452 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | kfree(thread); |
| 6454 | } |
| 6455 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6456 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | { |
| 6458 | if (!mddev) { |
| 6459 | MD_BUG(); |
| 6460 | return; |
| 6461 | } |
| 6462 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6463 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6465 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6466 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | return; |
| 6468 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6469 | if (mddev->degraded) |
| 6470 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6471 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6473 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6474 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6475 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6476 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6477 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | } |
| 6479 | |
| 6480 | /* seq_file implementation /proc/mdstat */ |
| 6481 | |
| 6482 | static void status_unused(struct seq_file *seq) |
| 6483 | { |
| 6484 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6485 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | |
| 6487 | seq_printf(seq, "unused devices: "); |
| 6488 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6489 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | char b[BDEVNAME_SIZE]; |
| 6491 | i++; |
| 6492 | seq_printf(seq, "%s ", |
| 6493 | bdevname(rdev->bdev,b)); |
| 6494 | } |
| 6495 | if (!i) |
| 6496 | seq_printf(seq, "<none>"); |
| 6497 | |
| 6498 | seq_printf(seq, "\n"); |
| 6499 | } |
| 6500 | |
| 6501 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6502 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6504 | sector_t max_sectors, resync, res; |
| 6505 | unsigned long dt, db; |
| 6506 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6507 | int scale; |
| 6508 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6510 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | |
| 6512 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6513 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6515 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6516 | |
| 6517 | /* |
| 6518 | * Should not happen. |
| 6519 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6520 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | MD_BUG(); |
| 6522 | return; |
| 6523 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6524 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6525 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6526 | * u32, as those are the requirements for sector_div. |
| 6527 | * Thus 'scale' must be at least 10 |
| 6528 | */ |
| 6529 | scale = 10; |
| 6530 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6531 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6532 | scale++; |
| 6533 | } |
| 6534 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6535 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6536 | |
| 6537 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6539 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | seq_printf(seq, "["); |
| 6541 | for (i = 0; i < x; i++) |
| 6542 | seq_printf(seq, "="); |
| 6543 | seq_printf(seq, ">"); |
| 6544 | for (i = 0; i < y; i++) |
| 6545 | seq_printf(seq, "."); |
| 6546 | seq_printf(seq, "] "); |
| 6547 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6548 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6549 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6550 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6551 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6552 | "check" : |
| 6553 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6554 | "resync" : "recovery"))), |
| 6555 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6556 | (unsigned long long) resync/2, |
| 6557 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | |
| 6559 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6560 | * dt: time from mark until now |
| 6561 | * db: blocks written from mark until now |
| 6562 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6563 | * |
| 6564 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6565 | * So we divide before multiply in case it is 32bit and close |
| 6566 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6567 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6568 | * near the end of resync when the number of remaining sectors |
| 6569 | * is close to 'db'. |
| 6570 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6571 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | */ |
| 6573 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6574 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6575 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6576 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6578 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6579 | sector_div(rt, db/32+1); |
| 6580 | rt *= dt; |
| 6581 | rt >>= 5; |
| 6582 | |
| 6583 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6584 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6586 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6590 | { |
| 6591 | struct list_head *tmp; |
| 6592 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6593 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6594 | |
| 6595 | if (l >= 0x10000) |
| 6596 | return NULL; |
| 6597 | if (!l--) |
| 6598 | /* header */ |
| 6599 | return (void*)1; |
| 6600 | |
| 6601 | spin_lock(&all_mddevs_lock); |
| 6602 | list_for_each(tmp,&all_mddevs) |
| 6603 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6604 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | mddev_get(mddev); |
| 6606 | spin_unlock(&all_mddevs_lock); |
| 6607 | return mddev; |
| 6608 | } |
| 6609 | spin_unlock(&all_mddevs_lock); |
| 6610 | if (!l--) |
| 6611 | return (void*)2;/* tail */ |
| 6612 | return NULL; |
| 6613 | } |
| 6614 | |
| 6615 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6616 | { |
| 6617 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6618 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | |
| 6620 | ++*pos; |
| 6621 | if (v == (void*)2) |
| 6622 | return NULL; |
| 6623 | |
| 6624 | spin_lock(&all_mddevs_lock); |
| 6625 | if (v == (void*)1) |
| 6626 | tmp = all_mddevs.next; |
| 6627 | else |
| 6628 | tmp = mddev->all_mddevs.next; |
| 6629 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6630 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | else { |
| 6632 | next_mddev = (void*)2; |
| 6633 | *pos = 0x10000; |
| 6634 | } |
| 6635 | spin_unlock(&all_mddevs_lock); |
| 6636 | |
| 6637 | if (v != (void*)1) |
| 6638 | mddev_put(mddev); |
| 6639 | return next_mddev; |
| 6640 | |
| 6641 | } |
| 6642 | |
| 6643 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6644 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6645 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | |
| 6647 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6648 | mddev_put(mddev); |
| 6649 | } |
| 6650 | |
| 6651 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6652 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6653 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6654 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6655 | struct md_rdev *rdev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6656 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | |
| 6658 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6659 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | seq_printf(seq, "Personalities : "); |
| 6661 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6662 | list_for_each_entry(pers, &pers_list, list) |
| 6663 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | |
| 6665 | spin_unlock(&pers_lock); |
| 6666 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6667 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | return 0; |
| 6669 | } |
| 6670 | if (v == (void*)2) { |
| 6671 | status_unused(seq); |
| 6672 | return 0; |
| 6673 | } |
| 6674 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6675 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6679 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6680 | mddev->pers ? "" : "in"); |
| 6681 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6682 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6684 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6685 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6686 | seq_printf(seq, " %s", mddev->pers->name); |
| 6687 | } |
| 6688 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6689 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6690 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | char b[BDEVNAME_SIZE]; |
| 6692 | seq_printf(seq, " %s[%d]", |
| 6693 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6694 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6695 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6696 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | seq_printf(seq, "(F)"); |
| 6698 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6699 | } else if (rdev->raid_disk < 0) |
| 6700 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6701 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | if (!list_empty(&mddev->disks)) { |
| 6705 | if (mddev->pers) |
| 6706 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6707 | (unsigned long long) |
| 6708 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | else |
| 6710 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6711 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6713 | if (mddev->persistent) { |
| 6714 | if (mddev->major_version != 0 || |
| 6715 | mddev->minor_version != 90) { |
| 6716 | seq_printf(seq," super %d.%d", |
| 6717 | mddev->major_version, |
| 6718 | mddev->minor_version); |
| 6719 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6720 | } else if (mddev->external) |
| 6721 | seq_printf(seq, " super external:%s", |
| 6722 | mddev->metadata_type); |
| 6723 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6724 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | |
| 6726 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6727 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6729 | if (mddev->pers->sync_request) { |
| 6730 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6731 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6732 | seq_printf(seq, "\n "); |
| 6733 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6734 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6735 | else if (mddev->recovery_cp < MaxSector) |
| 6736 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6737 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6738 | } else |
| 6739 | seq_printf(seq, "\n "); |
| 6740 | |
| 6741 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6742 | unsigned long chunk_kb; |
| 6743 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6744 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6745 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6746 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6747 | "%lu%s chunk", |
| 6748 | bitmap->pages - bitmap->missing_pages, |
| 6749 | bitmap->pages, |
| 6750 | (bitmap->pages - bitmap->missing_pages) |
| 6751 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6752 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6753 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6754 | if (bitmap->file) { |
| 6755 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6756 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6757 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6758 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6759 | seq_printf(seq, "\n"); |
| 6760 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | seq_printf(seq, "\n"); |
| 6764 | } |
| 6765 | mddev_unlock(mddev); |
| 6766 | |
| 6767 | return 0; |
| 6768 | } |
| 6769 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6770 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | .start = md_seq_start, |
| 6772 | .next = md_seq_next, |
| 6773 | .stop = md_seq_stop, |
| 6774 | .show = md_seq_show, |
| 6775 | }; |
| 6776 | |
| 6777 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6778 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6779 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6780 | int error; |
| 6781 | |
| 6782 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6783 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6784 | return error; |
| 6785 | |
| 6786 | seq = file->private_data; |
| 6787 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | return error; |
| 6789 | } |
| 6790 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6791 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6792 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6793 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6794 | int mask; |
| 6795 | |
| 6796 | poll_wait(filp, &md_event_waiters, wait); |
| 6797 | |
| 6798 | /* always allow read */ |
| 6799 | mask = POLLIN | POLLRDNORM; |
| 6800 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6801 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6802 | mask |= POLLERR | POLLPRI; |
| 6803 | return mask; |
| 6804 | } |
| 6805 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6806 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6807 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | .open = md_seq_open, |
| 6809 | .read = seq_read, |
| 6810 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6811 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6812 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | }; |
| 6814 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6815 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6818 | list_add_tail(&p->list, &pers_list); |
| 6819 | 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] | 6820 | spin_unlock(&pers_lock); |
| 6821 | return 0; |
| 6822 | } |
| 6823 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6824 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6826 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6827 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6828 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | spin_unlock(&pers_lock); |
| 6830 | return 0; |
| 6831 | } |
| 6832 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6833 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6835 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6837 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
| 6839 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6840 | rcu_read_lock(); |
| 6841 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6843 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6844 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6845 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6846 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6847 | * as sync_io is counted when a request starts, and |
| 6848 | * disk_stats is counted when it completes. |
| 6849 | * So resync activity will cause curr_events to be smaller than |
| 6850 | * when there was no such activity. |
| 6851 | * non-sync IO will cause disk_stat to increase without |
| 6852 | * increasing sync_io so curr_events will (eventually) |
| 6853 | * be larger than it was before. Once it becomes |
| 6854 | * substantially larger, the test below will cause |
| 6855 | * the array to appear non-idle, and resync will slow |
| 6856 | * down. |
| 6857 | * If there is a lot of outstanding resync activity when |
| 6858 | * we set last_event to curr_events, then all that activity |
| 6859 | * completing might cause the array to appear non-idle |
| 6860 | * and resync will be slowed down even though there might |
| 6861 | * not have been non-resync activity. This will only |
| 6862 | * happen once though. 'last_events' will soon reflect |
| 6863 | * the state where there is little or no outstanding |
| 6864 | * resync requests, and further resync activity will |
| 6865 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6866 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6868 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | rdev->last_events = curr_events; |
| 6870 | idle = 0; |
| 6871 | } |
| 6872 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6873 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | return idle; |
| 6875 | } |
| 6876 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6877 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | { |
| 6879 | /* another "blocks" (512byte) blocks have been synced */ |
| 6880 | atomic_sub(blocks, &mddev->recovery_active); |
| 6881 | wake_up(&mddev->recovery_wait); |
| 6882 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6883 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | md_wakeup_thread(mddev->thread); |
| 6885 | // stop recovery, signal do_sync .... |
| 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6890 | /* md_write_start(mddev, bi) |
| 6891 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6892 | * in superblock) before writing, schedule a superblock update |
| 6893 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6894 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6895 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6897 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6898 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6899 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6900 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6901 | BUG_ON(mddev->ro == 1); |
| 6902 | if (mddev->ro == 2) { |
| 6903 | /* need to switch to read/write */ |
| 6904 | mddev->ro = 0; |
| 6905 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6906 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6907 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6908 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6909 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6910 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6911 | if (mddev->safemode == 1) |
| 6912 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6913 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6914 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6915 | if (mddev->in_sync) { |
| 6916 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6917 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6918 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6919 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6920 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6921 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6922 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6923 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6924 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6925 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6926 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6927 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | } |
| 6929 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6930 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | { |
| 6932 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6933 | if (mddev->safemode == 2) |
| 6934 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6935 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6936 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6937 | } |
| 6938 | } |
| 6939 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6940 | /* md_allow_write(mddev) |
| 6941 | * Calling this ensures that the array is marked 'active' so that writes |
| 6942 | * may proceed without blocking. It is important to call this before |
| 6943 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6944 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6945 | * |
| 6946 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6947 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6948 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6949 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6950 | { |
| 6951 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6952 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6953 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6954 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6955 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6956 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6957 | |
| 6958 | spin_lock_irq(&mddev->write_lock); |
| 6959 | if (mddev->in_sync) { |
| 6960 | mddev->in_sync = 0; |
| 6961 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6962 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6963 | if (mddev->safemode_delay && |
| 6964 | mddev->safemode == 0) |
| 6965 | mddev->safemode = 1; |
| 6966 | spin_unlock_irq(&mddev->write_lock); |
| 6967 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6968 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6969 | } else |
| 6970 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6971 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6972 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6973 | return -EAGAIN; |
| 6974 | else |
| 6975 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6976 | } |
| 6977 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | #define SYNC_MARKS 10 |
| 6980 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6981 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6983 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | unsigned int currspeed = 0, |
| 6985 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6986 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | unsigned long mark[SYNC_MARKS]; |
| 6988 | sector_t mark_cnt[SYNC_MARKS]; |
| 6989 | int last_mark,m; |
| 6990 | struct list_head *tmp; |
| 6991 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6992 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6993 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6994 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6995 | |
| 6996 | /* just incase thread restarts... */ |
| 6997 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6998 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6999 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7000 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7002 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7003 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7004 | desc = "data-check"; |
| 7005 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7006 | desc = "requested-resync"; |
| 7007 | else |
| 7008 | desc = "resync"; |
| 7009 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7010 | desc = "reshape"; |
| 7011 | else |
| 7012 | desc = "recovery"; |
| 7013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | /* we overload curr_resync somewhat here. |
| 7015 | * 0 == not engaged in resync at all |
| 7016 | * 2 == checking that there is no conflict with another sync |
| 7017 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7018 | * commense |
| 7019 | * other == active in resync - this many blocks |
| 7020 | * |
| 7021 | * Before starting a resync we must have set curr_resync to |
| 7022 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7023 | * less than ours. When we find one that is the same or higher |
| 7024 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7025 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7026 | * This will mean we have to start checking from the beginning again. |
| 7027 | * |
| 7028 | */ |
| 7029 | |
| 7030 | do { |
| 7031 | mddev->curr_resync = 2; |
| 7032 | |
| 7033 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7034 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7035 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7036 | |
| 7037 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7039 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | if (mddev2 == mddev) |
| 7041 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7042 | if (!mddev->parallel_resync |
| 7043 | && mddev2->curr_resync |
| 7044 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | DEFINE_WAIT(wq); |
| 7046 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7047 | /* arbitrarily yield */ |
| 7048 | mddev->curr_resync = 1; |
| 7049 | wake_up(&resync_wait); |
| 7050 | } |
| 7051 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7052 | /* no need to wait here, we can wait the next |
| 7053 | * time 'round when curr_resync == 2 |
| 7054 | */ |
| 7055 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7056 | /* We need to wait 'interruptible' so as not to |
| 7057 | * contribute to the load average, and not to |
| 7058 | * be caught by 'softlockup' |
| 7059 | */ |
| 7060 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7061 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7062 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7063 | printk(KERN_INFO "md: delaying %s of %s" |
| 7064 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7065 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7066 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7068 | if (signal_pending(current)) |
| 7069 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | schedule(); |
| 7071 | finish_wait(&resync_wait, &wq); |
| 7072 | goto try_again; |
| 7073 | } |
| 7074 | finish_wait(&resync_wait, &wq); |
| 7075 | } |
| 7076 | } |
| 7077 | } while (mddev->curr_resync < 2); |
| 7078 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7079 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7080 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7082 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7083 | */ |
| 7084 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7085 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7086 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7087 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7088 | j = mddev->resync_min; |
| 7089 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7090 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7091 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7092 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7093 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7094 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7096 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7097 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7098 | rcu_read_lock(); |
| 7099 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7100 | if (rdev->raid_disk >= 0 && |
| 7101 | !test_bit(Faulty, &rdev->flags) && |
| 7102 | !test_bit(In_sync, &rdev->flags) && |
| 7103 | rdev->recovery_offset < j) |
| 7104 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7105 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7108 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7109 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7110 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7111 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7112 | "(but not more than %d KB/sec) for %s.\n", |
| 7113 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7115 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7116 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7117 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7119 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7120 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | } |
| 7122 | last_mark = 0; |
| 7123 | mddev->resync_mark = mark[last_mark]; |
| 7124 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7125 | |
| 7126 | /* |
| 7127 | * Tune reconstruction: |
| 7128 | */ |
| 7129 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7130 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7131 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | |
| 7133 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | last_check = 0; |
| 7135 | |
| 7136 | if (j>2) { |
| 7137 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7138 | "md: resuming %s of %s from checkpoint.\n", |
| 7139 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | mddev->curr_resync = j; |
| 7141 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7142 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | |
| 7144 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7145 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7147 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7148 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7149 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7150 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7151 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7152 | > (max_sectors >> 4)) || |
| 7153 | (j - mddev->curr_resync_completed)*2 |
| 7154 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7155 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7156 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7157 | wait_event(mddev->recovery_wait, |
| 7158 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7159 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7160 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7161 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7162 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7163 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7164 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7165 | /* As this condition is controlled by user-space, |
| 7166 | * we can block indefinitely, so use '_interruptible' |
| 7167 | * to avoid triggering warnings. |
| 7168 | */ |
| 7169 | flush_signals(current); /* just in case */ |
| 7170 | wait_event_interruptible(mddev->recovery_wait, |
| 7171 | mddev->resync_max > j |
| 7172 | || kthread_should_stop()); |
| 7173 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7174 | |
| 7175 | if (kthread_should_stop()) |
| 7176 | goto interrupted; |
| 7177 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7178 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7179 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7180 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7181 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | goto out; |
| 7183 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7184 | |
| 7185 | if (!skipped) { /* actual IO requested */ |
| 7186 | io_sectors += sectors; |
| 7187 | atomic_add(sectors, &mddev->recovery_active); |
| 7188 | } |
| 7189 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7190 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7191 | break; |
| 7192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | j += sectors; |
| 7194 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7195 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7196 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7197 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7198 | * visible in /proc/mdstat |
| 7199 | */ |
| 7200 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7201 | |
| 7202 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | continue; |
| 7204 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7205 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | repeat: |
| 7207 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7208 | /* step marks */ |
| 7209 | int next = (last_mark+1) % SYNC_MARKS; |
| 7210 | |
| 7211 | mddev->resync_mark = mark[next]; |
| 7212 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7213 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7214 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | last_mark = next; |
| 7216 | } |
| 7217 | |
| 7218 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7219 | if (kthread_should_stop()) |
| 7220 | goto interrupted; |
| 7221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | |
| 7223 | /* |
| 7224 | * this loop exits only if either when we are slower than |
| 7225 | * the 'hard' speed limit, or the system was IO-idle for |
| 7226 | * a jiffy. |
| 7227 | * the system might be non-idle CPU-wise, but we only care |
| 7228 | * about not overloading the IO subsystem. (things like an |
| 7229 | * e2fsck being done on the RAID array should execute fast) |
| 7230 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | cond_resched(); |
| 7232 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7233 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7234 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7236 | if (currspeed > speed_min(mddev)) { |
| 7237 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7238 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7239 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | goto repeat; |
| 7241 | } |
| 7242 | } |
| 7243 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7244 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | /* |
| 7246 | * this also signals 'finished resyncing' to md_stop |
| 7247 | */ |
| 7248 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7250 | |
| 7251 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7252 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7254 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7255 | mddev->curr_resync > 2) { |
| 7256 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7257 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7258 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7259 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7260 | "md: checkpointing %s of %s.\n", |
| 7261 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7262 | mddev->recovery_cp = mddev->curr_resync; |
| 7263 | } |
| 7264 | } else |
| 7265 | mddev->recovery_cp = MaxSector; |
| 7266 | } else { |
| 7267 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7268 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7269 | rcu_read_lock(); |
| 7270 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7271 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7272 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7273 | !test_bit(Faulty, &rdev->flags) && |
| 7274 | !test_bit(In_sync, &rdev->flags) && |
| 7275 | rdev->recovery_offset < mddev->curr_resync) |
| 7276 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7277 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7280 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7283 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7284 | /* We completed so min/max setting can be forgotten if used. */ |
| 7285 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7286 | mddev->resync_min = 0; |
| 7287 | mddev->resync_max = MaxSector; |
| 7288 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7289 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | mddev->curr_resync = 0; |
| 7291 | wake_up(&resync_wait); |
| 7292 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7293 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7294 | return; |
| 7295 | |
| 7296 | interrupted: |
| 7297 | /* |
| 7298 | * got a signal, exit. |
| 7299 | */ |
| 7300 | printk(KERN_INFO |
| 7301 | "md: md_do_sync() got signal ... exiting\n"); |
| 7302 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7303 | goto out; |
| 7304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7306 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7308 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7309 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7310 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7311 | int spares = 0; |
| 7312 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7313 | mddev->curr_resync_completed = 0; |
| 7314 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7315 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7316 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7317 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7318 | (test_bit(Faulty, &rdev->flags) || |
| 7319 | ! test_bit(In_sync, &rdev->flags)) && |
| 7320 | atomic_read(&rdev->nr_pending)==0) { |
| 7321 | if (mddev->pers->hot_remove_disk( |
| 7322 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7323 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7324 | rdev->raid_disk = -1; |
| 7325 | } |
| 7326 | } |
| 7327 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7328 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7329 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7330 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7331 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7332 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7333 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7334 | if (rdev->raid_disk < 0 |
| 7335 | && !test_bit(Faulty, &rdev->flags)) { |
| 7336 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7337 | if (mddev->pers-> |
| 7338 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7339 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7340 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7341 | spares++; |
| 7342 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7343 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7344 | } else |
| 7345 | break; |
| 7346 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7347 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7348 | } |
| 7349 | return spares; |
| 7350 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7351 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7352 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7353 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7354 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7355 | |
| 7356 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7357 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7358 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7359 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7360 | /* success...*/ |
| 7361 | /* activate any spares */ |
| 7362 | if (mddev->pers->spare_active(mddev)) |
| 7363 | sysfs_notify(&mddev->kobj, NULL, |
| 7364 | "degraded"); |
| 7365 | } |
| 7366 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7367 | mddev->pers->finish_reshape) |
| 7368 | mddev->pers->finish_reshape(mddev); |
| 7369 | md_update_sb(mddev, 1); |
| 7370 | |
| 7371 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7372 | * information must be scrapped |
| 7373 | */ |
| 7374 | if (!mddev->degraded) |
| 7375 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7376 | rdev->saved_raid_disk = -1; |
| 7377 | |
| 7378 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7379 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7380 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7381 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7382 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7383 | /* flag recovery needed just to double check */ |
| 7384 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7385 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7386 | md_new_event(mddev); |
Jonathan Brassow | 768e587 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7387 | if (mddev->event_work.func) |
| 7388 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7389 | } |
| 7390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | /* |
| 7392 | * This routine is regularly called by all per-raid-array threads to |
| 7393 | * deal with generic issues like resync and super-block update. |
| 7394 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7395 | * need this as they never do any recovery or update the superblock. |
| 7396 | * |
| 7397 | * It does not do any resync itself, but rather "forks" off other threads |
| 7398 | * to do that as needed. |
| 7399 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7400 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7401 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | * and wakeups up this thread which will reap the thread and finish up. |
| 7403 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7404 | * |
| 7405 | * The overall approach is: |
| 7406 | * 1/ if the superblock needs updating, update it. |
| 7407 | * 2/ If a recovery thread is running, don't do anything else. |
| 7408 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7409 | * 4/ If there are any faulty devices, remove them. |
| 7410 | * 5/ If array is degraded, try to add spares devices |
| 7411 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7412 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7413 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7415 | if (mddev->suspended) |
| 7416 | return; |
| 7417 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7418 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7419 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7421 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7422 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7423 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7424 | mdname(mddev)); |
| 7425 | mddev->safemode = 2; |
| 7426 | } |
| 7427 | flush_signals(current); |
| 7428 | } |
| 7429 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7430 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7431 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7433 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7434 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7435 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7436 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7437 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7438 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | )) |
| 7440 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7441 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7442 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7443 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7444 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7445 | if (mddev->ro) { |
| 7446 | /* Only thing we do on a ro array is remove |
| 7447 | * failed devices. |
| 7448 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7449 | struct md_rdev *rdev; |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7450 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7451 | if (rdev->raid_disk >= 0 && |
| 7452 | !test_bit(Blocked, &rdev->flags) && |
| 7453 | test_bit(Faulty, &rdev->flags) && |
| 7454 | atomic_read(&rdev->nr_pending)==0) { |
| 7455 | if (mddev->pers->hot_remove_disk( |
| 7456 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7457 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7458 | rdev->raid_disk = -1; |
| 7459 | } |
| 7460 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7461 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7462 | goto unlock; |
| 7463 | } |
| 7464 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7465 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7466 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7467 | spin_lock_irq(&mddev->write_lock); |
| 7468 | if (mddev->safemode && |
| 7469 | !atomic_read(&mddev->writes_pending) && |
| 7470 | !mddev->in_sync && |
| 7471 | mddev->recovery_cp == MaxSector) { |
| 7472 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7473 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7474 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7475 | } |
| 7476 | if (mddev->safemode == 1) |
| 7477 | mddev->safemode = 0; |
| 7478 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7479 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7480 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7481 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7482 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7483 | if (mddev->flags) |
| 7484 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7486 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7487 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7488 | /* resync/recovery still happening */ |
| 7489 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7490 | goto unlock; |
| 7491 | } |
| 7492 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7493 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | goto unlock; |
| 7495 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7496 | /* Set RUNNING before clearing NEEDED to avoid |
| 7497 | * any transients in the value of "sync_action". |
| 7498 | */ |
| 7499 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7500 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7501 | /* Clear some bits that don't mean anything, but |
| 7502 | * might be left set |
| 7503 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7504 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7505 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7507 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7508 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | /* no recovery is running. |
| 7510 | * remove any failed drives, then |
| 7511 | * add spares if possible. |
| 7512 | * Spare are also removed and re-added, to allow |
| 7513 | * the personality to fail the re-add. |
| 7514 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7516 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7517 | if (mddev->pers->check_reshape == NULL || |
| 7518 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7519 | /* Cannot proceed */ |
| 7520 | goto unlock; |
| 7521 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7522 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7523 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7524 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7525 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7526 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7527 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7528 | } else if (mddev->recovery_cp < MaxSector) { |
| 7529 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7530 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7531 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7532 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7533 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7535 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7536 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7537 | /* We are adding a device or devices to an array |
| 7538 | * which has the bitmap stored on all devices. |
| 7539 | * So make sure all bitmap pages get written |
| 7540 | */ |
| 7541 | bitmap_write_all(mddev->bitmap); |
| 7542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7543 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7544 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7545 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7546 | if (!mddev->sync_thread) { |
| 7547 | printk(KERN_ERR "%s: could not start resync" |
| 7548 | " thread...\n", |
| 7549 | mdname(mddev)); |
| 7550 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7551 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7552 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7553 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7554 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7555 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7556 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7558 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7559 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7560 | } |
| 7561 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7562 | if (!mddev->sync_thread) { |
| 7563 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7564 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7565 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7566 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7567 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7569 | mddev_unlock(mddev); |
| 7570 | } |
| 7571 | } |
| 7572 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7573 | 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] | 7574 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7575 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7576 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7577 | !test_bit(Blocked, &rdev->flags) && |
| 7578 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7579 | msecs_to_jiffies(5000)); |
| 7580 | rdev_dec_pending(rdev, mddev); |
| 7581 | } |
| 7582 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7583 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7584 | |
| 7585 | /* Bad block management. |
| 7586 | * We can record which blocks on each device are 'bad' and so just |
| 7587 | * fail those blocks, or that stripe, rather than the whole device. |
| 7588 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7589 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7590 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7591 | * A 'shift' can be set so that larger blocks are tracked and |
| 7592 | * consequently larger devices can be covered. |
| 7593 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7594 | * |
| 7595 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7596 | * might need to retry if it is very unlucky. |
| 7597 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7598 | * so we use the write_seqlock_irq variant. |
| 7599 | * |
| 7600 | * When looking for a bad block we specify a range and want to |
| 7601 | * know if any block in the range is bad. So we binary-search |
| 7602 | * to the last range that starts at-or-before the given endpoint, |
| 7603 | * (or "before the sector after the target range") |
| 7604 | * then see if it ends after the given start. |
| 7605 | * We return |
| 7606 | * 0 if there are no known bad blocks in the range |
| 7607 | * 1 if there are known bad block which are all acknowledged |
| 7608 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7609 | * plus the start/length of the first bad section we overlap. |
| 7610 | */ |
| 7611 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7612 | sector_t *first_bad, int *bad_sectors) |
| 7613 | { |
| 7614 | int hi; |
| 7615 | int lo = 0; |
| 7616 | u64 *p = bb->page; |
| 7617 | int rv = 0; |
| 7618 | sector_t target = s + sectors; |
| 7619 | unsigned seq; |
| 7620 | |
| 7621 | if (bb->shift > 0) { |
| 7622 | /* round the start down, and the end up */ |
| 7623 | s >>= bb->shift; |
| 7624 | target += (1<<bb->shift) - 1; |
| 7625 | target >>= bb->shift; |
| 7626 | sectors = target - s; |
| 7627 | } |
| 7628 | /* 'target' is now the first block after the bad range */ |
| 7629 | |
| 7630 | retry: |
| 7631 | seq = read_seqbegin(&bb->lock); |
| 7632 | |
| 7633 | hi = bb->count; |
| 7634 | |
| 7635 | /* Binary search between lo and hi for 'target' |
| 7636 | * i.e. for the last range that starts before 'target' |
| 7637 | */ |
| 7638 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7639 | * are known not to be the last range before target. |
| 7640 | * VARIANT: hi-lo is the number of possible |
| 7641 | * ranges, and decreases until it reaches 1 |
| 7642 | */ |
| 7643 | while (hi - lo > 1) { |
| 7644 | int mid = (lo + hi) / 2; |
| 7645 | sector_t a = BB_OFFSET(p[mid]); |
| 7646 | if (a < target) |
| 7647 | /* This could still be the one, earlier ranges |
| 7648 | * could not. */ |
| 7649 | lo = mid; |
| 7650 | else |
| 7651 | /* This and later ranges are definitely out. */ |
| 7652 | hi = mid; |
| 7653 | } |
| 7654 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7655 | if (hi > lo) { |
| 7656 | /* need to check all range that end after 's' to see if |
| 7657 | * any are unacknowledged. |
| 7658 | */ |
| 7659 | while (lo >= 0 && |
| 7660 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7661 | if (BB_OFFSET(p[lo]) < target) { |
| 7662 | /* starts before the end, and finishes after |
| 7663 | * the start, so they must overlap |
| 7664 | */ |
| 7665 | if (rv != -1 && BB_ACK(p[lo])) |
| 7666 | rv = 1; |
| 7667 | else |
| 7668 | rv = -1; |
| 7669 | *first_bad = BB_OFFSET(p[lo]); |
| 7670 | *bad_sectors = BB_LEN(p[lo]); |
| 7671 | } |
| 7672 | lo--; |
| 7673 | } |
| 7674 | } |
| 7675 | |
| 7676 | if (read_seqretry(&bb->lock, seq)) |
| 7677 | goto retry; |
| 7678 | |
| 7679 | return rv; |
| 7680 | } |
| 7681 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7682 | |
| 7683 | /* |
| 7684 | * Add a range of bad blocks to the table. |
| 7685 | * This might extend the table, or might contract it |
| 7686 | * if two adjacent ranges can be merged. |
| 7687 | * We binary-search to find the 'insertion' point, then |
| 7688 | * decide how best to handle it. |
| 7689 | */ |
| 7690 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7691 | int acknowledged) |
| 7692 | { |
| 7693 | u64 *p; |
| 7694 | int lo, hi; |
| 7695 | int rv = 1; |
| 7696 | |
| 7697 | if (bb->shift < 0) |
| 7698 | /* badblocks are disabled */ |
| 7699 | return 0; |
| 7700 | |
| 7701 | if (bb->shift) { |
| 7702 | /* round the start down, and the end up */ |
| 7703 | sector_t next = s + sectors; |
| 7704 | s >>= bb->shift; |
| 7705 | next += (1<<bb->shift) - 1; |
| 7706 | next >>= bb->shift; |
| 7707 | sectors = next - s; |
| 7708 | } |
| 7709 | |
| 7710 | write_seqlock_irq(&bb->lock); |
| 7711 | |
| 7712 | p = bb->page; |
| 7713 | lo = 0; |
| 7714 | hi = bb->count; |
| 7715 | /* Find the last range that starts at-or-before 's' */ |
| 7716 | while (hi - lo > 1) { |
| 7717 | int mid = (lo + hi) / 2; |
| 7718 | sector_t a = BB_OFFSET(p[mid]); |
| 7719 | if (a <= s) |
| 7720 | lo = mid; |
| 7721 | else |
| 7722 | hi = mid; |
| 7723 | } |
| 7724 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7725 | hi = lo; |
| 7726 | |
| 7727 | if (hi > lo) { |
| 7728 | /* we found a range that might merge with the start |
| 7729 | * of our new range |
| 7730 | */ |
| 7731 | sector_t a = BB_OFFSET(p[lo]); |
| 7732 | sector_t e = a + BB_LEN(p[lo]); |
| 7733 | int ack = BB_ACK(p[lo]); |
| 7734 | if (e >= s) { |
| 7735 | /* Yes, we can merge with a previous range */ |
| 7736 | if (s == a && s + sectors >= e) |
| 7737 | /* new range covers old */ |
| 7738 | ack = acknowledged; |
| 7739 | else |
| 7740 | ack = ack && acknowledged; |
| 7741 | |
| 7742 | if (e < s + sectors) |
| 7743 | e = s + sectors; |
| 7744 | if (e - a <= BB_MAX_LEN) { |
| 7745 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7746 | s = e; |
| 7747 | } else { |
| 7748 | /* does not all fit in one range, |
| 7749 | * make p[lo] maximal |
| 7750 | */ |
| 7751 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7752 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7753 | s = a + BB_MAX_LEN; |
| 7754 | } |
| 7755 | sectors = e - s; |
| 7756 | } |
| 7757 | } |
| 7758 | if (sectors && hi < bb->count) { |
| 7759 | /* 'hi' points to the first range that starts after 's'. |
| 7760 | * Maybe we can merge with the start of that range */ |
| 7761 | sector_t a = BB_OFFSET(p[hi]); |
| 7762 | sector_t e = a + BB_LEN(p[hi]); |
| 7763 | int ack = BB_ACK(p[hi]); |
| 7764 | if (a <= s + sectors) { |
| 7765 | /* merging is possible */ |
| 7766 | if (e <= s + sectors) { |
| 7767 | /* full overlap */ |
| 7768 | e = s + sectors; |
| 7769 | ack = acknowledged; |
| 7770 | } else |
| 7771 | ack = ack && acknowledged; |
| 7772 | |
| 7773 | a = s; |
| 7774 | if (e - a <= BB_MAX_LEN) { |
| 7775 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7776 | s = e; |
| 7777 | } else { |
| 7778 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7779 | s = a + BB_MAX_LEN; |
| 7780 | } |
| 7781 | sectors = e - s; |
| 7782 | lo = hi; |
| 7783 | hi++; |
| 7784 | } |
| 7785 | } |
| 7786 | if (sectors == 0 && hi < bb->count) { |
| 7787 | /* we might be able to combine lo and hi */ |
| 7788 | /* Note: 's' is at the end of 'lo' */ |
| 7789 | sector_t a = BB_OFFSET(p[hi]); |
| 7790 | int lolen = BB_LEN(p[lo]); |
| 7791 | int hilen = BB_LEN(p[hi]); |
| 7792 | int newlen = lolen + hilen - (s - a); |
| 7793 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7794 | /* yes, we can combine them */ |
| 7795 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7796 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7797 | memmove(p + hi, p + hi + 1, |
| 7798 | (bb->count - hi - 1) * 8); |
| 7799 | bb->count--; |
| 7800 | } |
| 7801 | } |
| 7802 | while (sectors) { |
| 7803 | /* didn't merge (it all). |
| 7804 | * Need to add a range just before 'hi' */ |
| 7805 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7806 | /* No room for more */ |
| 7807 | rv = 0; |
| 7808 | break; |
| 7809 | } else { |
| 7810 | int this_sectors = sectors; |
| 7811 | memmove(p + hi + 1, p + hi, |
| 7812 | (bb->count - hi) * 8); |
| 7813 | bb->count++; |
| 7814 | |
| 7815 | if (this_sectors > BB_MAX_LEN) |
| 7816 | this_sectors = BB_MAX_LEN; |
| 7817 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7818 | sectors -= this_sectors; |
| 7819 | s += this_sectors; |
| 7820 | } |
| 7821 | } |
| 7822 | |
| 7823 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7824 | if (!acknowledged) |
| 7825 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7826 | write_sequnlock_irq(&bb->lock); |
| 7827 | |
| 7828 | return rv; |
| 7829 | } |
| 7830 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7831 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7832 | int acknowledged) |
| 7833 | { |
| 7834 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7835 | s + rdev->data_offset, sectors, acknowledged); |
| 7836 | if (rv) { |
| 7837 | /* Make sure they get written out promptly */ |
| 7838 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7839 | md_wakeup_thread(rdev->mddev->thread); |
| 7840 | } |
| 7841 | return rv; |
| 7842 | } |
| 7843 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7844 | |
| 7845 | /* |
| 7846 | * Remove a range of bad blocks from the table. |
| 7847 | * This may involve extending the table if we spilt a region, |
| 7848 | * but it must not fail. So if the table becomes full, we just |
| 7849 | * drop the remove request. |
| 7850 | */ |
| 7851 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7852 | { |
| 7853 | u64 *p; |
| 7854 | int lo, hi; |
| 7855 | sector_t target = s + sectors; |
| 7856 | int rv = 0; |
| 7857 | |
| 7858 | if (bb->shift > 0) { |
| 7859 | /* When clearing we round the start up and the end down. |
| 7860 | * This should not matter as the shift should align with |
| 7861 | * the block size and no rounding should ever be needed. |
| 7862 | * However it is better the think a block is bad when it |
| 7863 | * isn't than to think a block is not bad when it is. |
| 7864 | */ |
| 7865 | s += (1<<bb->shift) - 1; |
| 7866 | s >>= bb->shift; |
| 7867 | target >>= bb->shift; |
| 7868 | sectors = target - s; |
| 7869 | } |
| 7870 | |
| 7871 | write_seqlock_irq(&bb->lock); |
| 7872 | |
| 7873 | p = bb->page; |
| 7874 | lo = 0; |
| 7875 | hi = bb->count; |
| 7876 | /* Find the last range that starts before 'target' */ |
| 7877 | while (hi - lo > 1) { |
| 7878 | int mid = (lo + hi) / 2; |
| 7879 | sector_t a = BB_OFFSET(p[mid]); |
| 7880 | if (a < target) |
| 7881 | lo = mid; |
| 7882 | else |
| 7883 | hi = mid; |
| 7884 | } |
| 7885 | if (hi > lo) { |
| 7886 | /* p[lo] is the last range that could overlap the |
| 7887 | * current range. Earlier ranges could also overlap, |
| 7888 | * but only this one can overlap the end of the range. |
| 7889 | */ |
| 7890 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7891 | /* Partial overlap, leave the tail of this range */ |
| 7892 | int ack = BB_ACK(p[lo]); |
| 7893 | sector_t a = BB_OFFSET(p[lo]); |
| 7894 | sector_t end = a + BB_LEN(p[lo]); |
| 7895 | |
| 7896 | if (a < s) { |
| 7897 | /* we need to split this range */ |
| 7898 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7899 | rv = 0; |
| 7900 | goto out; |
| 7901 | } |
| 7902 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7903 | bb->count++; |
| 7904 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7905 | lo++; |
| 7906 | } |
| 7907 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7908 | /* there is no longer an overlap */ |
| 7909 | hi = lo; |
| 7910 | lo--; |
| 7911 | } |
| 7912 | while (lo >= 0 && |
| 7913 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7914 | /* This range does overlap */ |
| 7915 | if (BB_OFFSET(p[lo]) < s) { |
| 7916 | /* Keep the early parts of this range. */ |
| 7917 | int ack = BB_ACK(p[lo]); |
| 7918 | sector_t start = BB_OFFSET(p[lo]); |
| 7919 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7920 | /* now low doesn't overlap, so.. */ |
| 7921 | break; |
| 7922 | } |
| 7923 | lo--; |
| 7924 | } |
| 7925 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7926 | * anything between needs to be discarded |
| 7927 | */ |
| 7928 | if (hi - lo > 1) { |
| 7929 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7930 | bb->count -= (hi - lo - 1); |
| 7931 | } |
| 7932 | } |
| 7933 | |
| 7934 | bb->changed = 1; |
| 7935 | out: |
| 7936 | write_sequnlock_irq(&bb->lock); |
| 7937 | return rv; |
| 7938 | } |
| 7939 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7940 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7941 | { |
| 7942 | return md_clear_badblocks(&rdev->badblocks, |
| 7943 | s + rdev->data_offset, |
| 7944 | sectors); |
| 7945 | } |
| 7946 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 7947 | |
| 7948 | /* |
| 7949 | * Acknowledge all bad blocks in a list. |
| 7950 | * This only succeeds if ->changed is clear. It is used by |
| 7951 | * in-kernel metadata updates |
| 7952 | */ |
| 7953 | void md_ack_all_badblocks(struct badblocks *bb) |
| 7954 | { |
| 7955 | if (bb->page == NULL || bb->changed) |
| 7956 | /* no point even trying */ |
| 7957 | return; |
| 7958 | write_seqlock_irq(&bb->lock); |
| 7959 | |
| 7960 | if (bb->changed == 0) { |
| 7961 | u64 *p = bb->page; |
| 7962 | int i; |
| 7963 | for (i = 0; i < bb->count ; i++) { |
| 7964 | if (!BB_ACK(p[i])) { |
| 7965 | sector_t start = BB_OFFSET(p[i]); |
| 7966 | int len = BB_LEN(p[i]); |
| 7967 | p[i] = BB_MAKE(start, len, 1); |
| 7968 | } |
| 7969 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7970 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7971 | } |
| 7972 | write_sequnlock_irq(&bb->lock); |
| 7973 | } |
| 7974 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 7975 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7976 | /* sysfs access to bad-blocks list. |
| 7977 | * We present two files. |
| 7978 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 7979 | * are recorded as bad. The list is truncated to fit within |
| 7980 | * the one-page limit of sysfs. |
| 7981 | * Writing "sector length" to this file adds an acknowledged |
| 7982 | * bad block list. |
| 7983 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 7984 | * been acknowledged. Writing to this file adds bad blocks |
| 7985 | * without acknowledging them. This is largely for testing. |
| 7986 | */ |
| 7987 | |
| 7988 | static ssize_t |
| 7989 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 7990 | { |
| 7991 | size_t len; |
| 7992 | int i; |
| 7993 | u64 *p = bb->page; |
| 7994 | unsigned seq; |
| 7995 | |
| 7996 | if (bb->shift < 0) |
| 7997 | return 0; |
| 7998 | |
| 7999 | retry: |
| 8000 | seq = read_seqbegin(&bb->lock); |
| 8001 | |
| 8002 | len = 0; |
| 8003 | i = 0; |
| 8004 | |
| 8005 | while (len < PAGE_SIZE && i < bb->count) { |
| 8006 | sector_t s = BB_OFFSET(p[i]); |
| 8007 | unsigned int length = BB_LEN(p[i]); |
| 8008 | int ack = BB_ACK(p[i]); |
| 8009 | i++; |
| 8010 | |
| 8011 | if (unack && ack) |
| 8012 | continue; |
| 8013 | |
| 8014 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8015 | (unsigned long long)s << bb->shift, |
| 8016 | length << bb->shift); |
| 8017 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8018 | if (unack && len == 0) |
| 8019 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8020 | |
| 8021 | if (read_seqretry(&bb->lock, seq)) |
| 8022 | goto retry; |
| 8023 | |
| 8024 | return len; |
| 8025 | } |
| 8026 | |
| 8027 | #define DO_DEBUG 1 |
| 8028 | |
| 8029 | static ssize_t |
| 8030 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8031 | { |
| 8032 | unsigned long long sector; |
| 8033 | int length; |
| 8034 | char newline; |
| 8035 | #ifdef DO_DEBUG |
| 8036 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8037 | * Normally only a successful write may clear a badblock |
| 8038 | */ |
| 8039 | int clear = 0; |
| 8040 | if (page[0] == '-') { |
| 8041 | clear = 1; |
| 8042 | page++; |
| 8043 | } |
| 8044 | #endif /* DO_DEBUG */ |
| 8045 | |
| 8046 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8047 | case 3: |
| 8048 | if (newline != '\n') |
| 8049 | return -EINVAL; |
| 8050 | case 2: |
| 8051 | if (length <= 0) |
| 8052 | return -EINVAL; |
| 8053 | break; |
| 8054 | default: |
| 8055 | return -EINVAL; |
| 8056 | } |
| 8057 | |
| 8058 | #ifdef DO_DEBUG |
| 8059 | if (clear) { |
| 8060 | md_clear_badblocks(bb, sector, length); |
| 8061 | return len; |
| 8062 | } |
| 8063 | #endif /* DO_DEBUG */ |
| 8064 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8065 | return len; |
| 8066 | else |
| 8067 | return -ENOSPC; |
| 8068 | } |
| 8069 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8070 | static int md_notify_reboot(struct notifier_block *this, |
| 8071 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8072 | { |
| 8073 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8074 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8075 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8076 | |
| 8077 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 8078 | |
| 8079 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 8080 | |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8081 | for_each_mddev(mddev, tmp) { |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8082 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8083 | /* Force a switch to readonly even array |
| 8084 | * appears to still be in use. Hence |
| 8085 | * the '100'. |
| 8086 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 8087 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8088 | mddev_unlock(mddev); |
| 8089 | } |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8090 | need_delay = 1; |
| 8091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8092 | /* |
| 8093 | * certain more exotic SCSI devices are known to be |
| 8094 | * volatile wrt too early system reboots. While the |
| 8095 | * right place to handle this issue is the given |
| 8096 | * driver, we do want to have a safe RAID driver ... |
| 8097 | */ |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8098 | if (need_delay) |
| 8099 | mdelay(1000*1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8100 | } |
| 8101 | return NOTIFY_DONE; |
| 8102 | } |
| 8103 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8104 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8105 | .notifier_call = md_notify_reboot, |
| 8106 | .next = NULL, |
| 8107 | .priority = INT_MAX, /* before any real devices */ |
| 8108 | }; |
| 8109 | |
| 8110 | static void md_geninit(void) |
| 8111 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8112 | 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] | 8113 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8114 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8115 | } |
| 8116 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8117 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8118 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8119 | int ret = -ENOMEM; |
| 8120 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8121 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8122 | if (!md_wq) |
| 8123 | goto err_wq; |
| 8124 | |
| 8125 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8126 | if (!md_misc_wq) |
| 8127 | goto err_misc_wq; |
| 8128 | |
| 8129 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8130 | goto err_md; |
| 8131 | |
| 8132 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8133 | goto err_mdp; |
| 8134 | mdp_major = ret; |
| 8135 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8136 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8137 | md_probe, NULL, NULL); |
| 8138 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8139 | md_probe, NULL, NULL); |
| 8140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8141 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8142 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8143 | |
| 8144 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8145 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8146 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8147 | err_mdp: |
| 8148 | unregister_blkdev(MD_MAJOR, "md"); |
| 8149 | err_md: |
| 8150 | destroy_workqueue(md_misc_wq); |
| 8151 | err_misc_wq: |
| 8152 | destroy_workqueue(md_wq); |
| 8153 | err_wq: |
| 8154 | return ret; |
| 8155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8156 | |
| 8157 | #ifndef MODULE |
| 8158 | |
| 8159 | /* |
| 8160 | * Searches all registered partitions for autorun RAID arrays |
| 8161 | * at boot time. |
| 8162 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8163 | |
| 8164 | static LIST_HEAD(all_detected_devices); |
| 8165 | struct detected_devices_node { |
| 8166 | struct list_head list; |
| 8167 | dev_t dev; |
| 8168 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8169 | |
| 8170 | void md_autodetect_dev(dev_t dev) |
| 8171 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8172 | struct detected_devices_node *node_detected_dev; |
| 8173 | |
| 8174 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8175 | if (node_detected_dev) { |
| 8176 | node_detected_dev->dev = dev; |
| 8177 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8178 | } else { |
| 8179 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8180 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8182 | } |
| 8183 | |
| 8184 | |
| 8185 | static void autostart_arrays(int part) |
| 8186 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8187 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8188 | struct detected_devices_node *node_detected_dev; |
| 8189 | dev_t dev; |
| 8190 | int i_scanned, i_passed; |
| 8191 | |
| 8192 | i_scanned = 0; |
| 8193 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8194 | |
| 8195 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8196 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8197 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8198 | i_scanned++; |
| 8199 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8200 | struct detected_devices_node, list); |
| 8201 | list_del(&node_detected_dev->list); |
| 8202 | dev = node_detected_dev->dev; |
| 8203 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8204 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8205 | if (IS_ERR(rdev)) |
| 8206 | continue; |
| 8207 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8208 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8209 | MD_BUG(); |
| 8210 | continue; |
| 8211 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8212 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8213 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8214 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8215 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8216 | |
| 8217 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8218 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8219 | |
| 8220 | autorun_devices(part); |
| 8221 | } |
| 8222 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8223 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8224 | |
| 8225 | static __exit void md_exit(void) |
| 8226 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8227 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8228 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8229 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8230 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8231 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8232 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8233 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8234 | unregister_blkdev(mdp_major, "mdp"); |
| 8235 | unregister_reboot_notifier(&md_notifier); |
| 8236 | unregister_sysctl_table(raid_table_header); |
| 8237 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8238 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8240 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8241 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8242 | destroy_workqueue(md_misc_wq); |
| 8243 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | } |
| 8245 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8246 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8247 | module_exit(md_exit) |
| 8248 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8249 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8250 | { |
| 8251 | return sprintf(buffer, "%d", start_readonly); |
| 8252 | } |
| 8253 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8254 | { |
| 8255 | char *e; |
| 8256 | int num = simple_strtoul(val, &e, 10); |
| 8257 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8258 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8259 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8260 | } |
| 8261 | return -EINVAL; |
| 8262 | } |
| 8263 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8264 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8265 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8266 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8267 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8269 | EXPORT_SYMBOL(register_md_personality); |
| 8270 | EXPORT_SYMBOL(unregister_md_personality); |
| 8271 | EXPORT_SYMBOL(md_error); |
| 8272 | EXPORT_SYMBOL(md_done_sync); |
| 8273 | EXPORT_SYMBOL(md_write_start); |
| 8274 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | EXPORT_SYMBOL(md_register_thread); |
| 8276 | EXPORT_SYMBOL(md_unregister_thread); |
| 8277 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8278 | EXPORT_SYMBOL(md_check_recovery); |
| 8279 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8280 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8281 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8282 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |