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> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 47 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 48 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 49 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 50 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 51 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/raid/md_p.h> |
| 53 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 55 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 56 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 70 | static void md_print_devices(void); |
| 71 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 73 | static struct workqueue_struct *md_wq; |
| 74 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 76 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 79 | * Default number of read corrections we'll attempt on an rdev |
| 80 | * before ejecting it from the array. We divide the read error |
| 81 | * count by 2 for every hour elapsed between read errors. |
| 82 | */ |
| 83 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 84 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 86 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 87 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 88 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 90 | * speed limit - in case reconstruction slows down your system despite |
| 91 | * idle IO detection. |
| 92 | * |
| 93 | * 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] | 94 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
| 96 | |
| 97 | static int sysctl_speed_limit_min = 1000; |
| 98 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 99 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 100 | { |
| 101 | return mddev->sync_speed_min ? |
| 102 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 103 | } |
| 104 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 105 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 106 | { |
| 107 | return mddev->sync_speed_max ? |
| 108 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | static struct ctl_table_header *raid_table_header; |
| 112 | |
| 113 | static ctl_table raid_table[] = { |
| 114 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .procname = "speed_limit_min", |
| 116 | .data = &sysctl_speed_limit_min, |
| 117 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 118 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 119 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }, |
| 121 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .procname = "speed_limit_max", |
| 123 | .data = &sysctl_speed_limit_max, |
| 124 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 125 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 126 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 128 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static ctl_table raid_dir_table[] = { |
| 132 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .procname = "raid", |
| 134 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 135 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .child = raid_table, |
| 137 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 138 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | static ctl_table raid_root_table[] = { |
| 142 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | .procname = "dev", |
| 144 | .maxlen = 0, |
| 145 | .mode = 0555, |
| 146 | .child = raid_dir_table, |
| 147 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 148 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 151 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 153 | static int start_readonly; |
| 154 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 155 | /* bio_clone_mddev |
| 156 | * like bio_clone, but with a local bio set |
| 157 | */ |
| 158 | |
| 159 | static void mddev_bio_destructor(struct bio *bio) |
| 160 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 161 | struct mddev *mddev, **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 162 | |
| 163 | mddevp = (void*)bio; |
| 164 | mddev = mddevp[-1]; |
| 165 | |
| 166 | bio_free(bio, mddev->bio_set); |
| 167 | } |
| 168 | |
| 169 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 170 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 171 | { |
| 172 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 173 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 174 | |
| 175 | if (!mddev || !mddev->bio_set) |
| 176 | return bio_alloc(gfp_mask, nr_iovecs); |
| 177 | |
| 178 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 179 | mddev->bio_set); |
| 180 | if (!b) |
| 181 | return NULL; |
| 182 | mddevp = (void*)b; |
| 183 | mddevp[-1] = mddev; |
| 184 | b->bi_destructor = mddev_bio_destructor; |
| 185 | return b; |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 188 | |
| 189 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 190 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 191 | { |
| 192 | struct bio *b; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 193 | struct mddev **mddevp; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 194 | |
| 195 | if (!mddev || !mddev->bio_set) |
| 196 | return bio_clone(bio, gfp_mask); |
| 197 | |
| 198 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 199 | mddev->bio_set); |
| 200 | if (!b) |
| 201 | return NULL; |
| 202 | mddevp = (void*)b; |
| 203 | mddevp[-1] = mddev; |
| 204 | b->bi_destructor = mddev_bio_destructor; |
| 205 | __bio_clone(b, bio); |
| 206 | if (bio_integrity(bio)) { |
| 207 | int ret; |
| 208 | |
| 209 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 210 | |
| 211 | if (ret < 0) { |
| 212 | bio_put(b); |
| 213 | return NULL; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return b; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 220 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 221 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 222 | { |
| 223 | /* 'bio' is a cloned bio which we need to trim to match |
| 224 | * the given offset and size. |
| 225 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 226 | */ |
| 227 | int i; |
| 228 | struct bio_vec *bvec; |
| 229 | int sofar = 0; |
| 230 | |
| 231 | size <<= 9; |
| 232 | if (offset == 0 && size == bio->bi_size) |
| 233 | return; |
| 234 | |
| 235 | bio->bi_sector += offset; |
| 236 | bio->bi_size = size; |
| 237 | offset <<= 9; |
| 238 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 239 | |
| 240 | while (bio->bi_idx < bio->bi_vcnt && |
| 241 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 242 | /* remove this whole bio_vec */ |
| 243 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 244 | bio->bi_idx++; |
| 245 | } |
| 246 | if (bio->bi_idx < bio->bi_vcnt) { |
| 247 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 248 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 249 | } |
| 250 | /* avoid any complications with bi_idx being non-zero*/ |
| 251 | if (bio->bi_idx) { |
| 252 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 253 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 254 | bio->bi_vcnt -= bio->bi_idx; |
| 255 | bio->bi_idx = 0; |
| 256 | } |
| 257 | /* Make sure vcnt and last bv are not too big */ |
| 258 | bio_for_each_segment(bvec, bio, i) { |
| 259 | if (sofar + bvec->bv_len > size) |
| 260 | bvec->bv_len = size - sofar; |
| 261 | if (bvec->bv_len == 0) { |
| 262 | bio->bi_vcnt = i; |
| 263 | break; |
| 264 | } |
| 265 | sofar += bvec->bv_len; |
| 266 | } |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 271 | * We have a system wide 'event count' that is incremented |
| 272 | * on any 'interesting' event, and readers of /proc/mdstat |
| 273 | * can use 'poll' or 'select' to find out when the event |
| 274 | * count increases. |
| 275 | * |
| 276 | * Events are: |
| 277 | * start array, stop array, error, add device, remove device, |
| 278 | * start build, activate spare |
| 279 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 280 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 281 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 282 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 283 | { |
| 284 | atomic_inc(&md_event_count); |
| 285 | wake_up(&md_event_waiters); |
| 286 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 287 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 288 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 289 | /* Alternate version that can be called from interrupts |
| 290 | * when calling sysfs_notify isn't needed. |
| 291 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 292 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 293 | { |
| 294 | atomic_inc(&md_event_count); |
| 295 | wake_up(&md_event_waiters); |
| 296 | } |
| 297 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 298 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * Enables to iterate over all existing md arrays |
| 300 | * all_mddevs_lock protects this list. |
| 301 | */ |
| 302 | static LIST_HEAD(all_mddevs); |
| 303 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 304 | |
| 305 | |
| 306 | /* |
| 307 | * iterates through all used mddevs in the system. |
| 308 | * We take care to grab the all_mddevs_lock whenever navigating |
| 309 | * the list, and to always hold a refcount when unlocked. |
| 310 | * Any code which breaks out of this loop while own |
| 311 | * a reference to the current mddev and must mddev_put it. |
| 312 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 313 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | \ |
| 315 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 316 | _tmp = all_mddevs.next; \ |
| 317 | _mddev = NULL;}); \ |
| 318 | ({ if (_tmp != &all_mddevs) \ |
| 319 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 321 | if (_mddev) mddev_put(_mddev); \ |
| 322 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 323 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 325 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | ) |
| 327 | |
| 328 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 329 | /* Rather than calling directly into the personality make_request function, |
| 330 | * IO requests come here first so that we can check if the device is |
| 331 | * being suspended pending a reconfiguration. |
| 332 | * We hold a refcount over the call to ->make_request. By the time that |
| 333 | * call has finished, the bio has been linked into some internal structure |
| 334 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 335 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 336 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 338 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 339 | struct mddev *mddev = q->queuedata; |
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); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 346 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 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); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 371 | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 382 | /* mddev_suspend makes sure no new requests are submitted |
| 383 | * to the device, and that any requests that have been submitted |
| 384 | * are completely handled. |
| 385 | * Once ->stop is called and completes, the module will be completely |
| 386 | * unused. |
| 387 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 388 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 389 | { |
| 390 | BUG_ON(mddev->suspended); |
| 391 | mddev->suspended = 1; |
| 392 | synchronize_rcu(); |
| 393 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 394 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 395 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 397 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 398 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 399 | { |
| 400 | mddev->suspended = 0; |
| 401 | wake_up(&mddev->sb_wait); |
| 402 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 403 | |
| 404 | md_wakeup_thread(mddev->thread); |
| 405 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 406 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 408 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 409 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 410 | { |
| 411 | return mddev->suspended; |
| 412 | } |
| 413 | EXPORT_SYMBOL(mddev_congested); |
| 414 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 415 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | */ |
| 418 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 419 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 421 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 422 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | |
| 424 | rdev_dec_pending(rdev, mddev); |
| 425 | |
| 426 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 427 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 428 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 429 | } |
| 430 | bio_put(bio); |
| 431 | } |
| 432 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 433 | static void md_submit_flush_data(struct work_struct *ws); |
| 434 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 435 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 436 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 437 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 438 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 440 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 441 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 442 | rcu_read_lock(); |
| 443 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 444 | if (rdev->raid_disk >= 0 && |
| 445 | !test_bit(Faulty, &rdev->flags)) { |
| 446 | /* Take two references, one is dropped |
| 447 | * when request finishes, one after |
| 448 | * we reclaim rcu_read_lock |
| 449 | */ |
| 450 | struct bio *bi; |
| 451 | atomic_inc(&rdev->nr_pending); |
| 452 | atomic_inc(&rdev->nr_pending); |
| 453 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 454 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 455 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 456 | bi->bi_private = rdev; |
| 457 | bi->bi_bdev = rdev->bdev; |
| 458 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 459 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 460 | rcu_read_lock(); |
| 461 | rdev_dec_pending(rdev, mddev); |
| 462 | } |
| 463 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 464 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 465 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 466 | } |
| 467 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 468 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 469 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 470 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 471 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 472 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 473 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | /* an empty barrier - all done */ |
| 475 | bio_endio(bio, 0); |
| 476 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 477 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 478 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 479 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 480 | |
| 481 | mddev->flush_bio = NULL; |
| 482 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 483 | } |
| 484 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 485 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 486 | { |
| 487 | spin_lock_irq(&mddev->write_lock); |
| 488 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 489 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 490 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 491 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 492 | spin_unlock_irq(&mddev->write_lock); |
| 493 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 494 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 495 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 496 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 497 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 498 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 499 | /* Support for plugging. |
| 500 | * This mirrors the plugging support in request_queue, but does not |
| 501 | * require having a whole queue or request structures. |
| 502 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 503 | * plugged on. This links tot the private plug_handle structure in the |
| 504 | * personality data where we keep a count of the number of outstanding |
| 505 | * plugs so other code can see if a plug is active. |
| 506 | */ |
| 507 | struct md_plug_cb { |
| 508 | struct blk_plug_cb cb; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 509 | struct mddev *mddev; |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 513 | { |
| 514 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 515 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 516 | md_wakeup_thread(mdcb->mddev->thread); |
| 517 | kfree(mdcb); |
| 518 | } |
| 519 | |
| 520 | /* Check that an unplug wakeup will come shortly. |
| 521 | * If not, wakeup the md thread immediately |
| 522 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 523 | int mddev_check_plugged(struct mddev *mddev) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 524 | { |
| 525 | struct blk_plug *plug = current->plug; |
| 526 | struct md_plug_cb *mdcb; |
| 527 | |
| 528 | if (!plug) |
| 529 | return 0; |
| 530 | |
| 531 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 532 | if (mdcb->cb.callback == plugger_unplug && |
| 533 | mdcb->mddev == mddev) { |
| 534 | /* Already on the list, move to top */ |
| 535 | if (mdcb != list_first_entry(&plug->cb_list, |
| 536 | struct md_plug_cb, |
| 537 | cb.list)) |
| 538 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 539 | return 1; |
| 540 | } |
| 541 | } |
| 542 | /* Not currently on the callback list */ |
| 543 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 544 | if (!mdcb) |
| 545 | return 0; |
| 546 | |
| 547 | mdcb->mddev = mddev; |
| 548 | mdcb->cb.callback = plugger_unplug; |
| 549 | atomic_inc(&mddev->plug_cnt); |
| 550 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 551 | return 1; |
| 552 | } |
| 553 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 554 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 555 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
| 557 | atomic_inc(&mddev->active); |
| 558 | return mddev; |
| 559 | } |
| 560 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 561 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 562 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 563 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 565 | struct bio_set *bs = NULL; |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 568 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 569 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 570 | mddev->ctime == 0 && !mddev->hold_active) { |
| 571 | /* Array is not configured at all, and not held active, |
| 572 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 574 | bs = mddev->bio_set; |
| 575 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 576 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 577 | /* We did a probe so need to clean up. Call |
| 578 | * queue_work inside the spinlock so that |
| 579 | * flush_workqueue() after mddev_find will |
| 580 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 581 | */ |
| 582 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 583 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 584 | } else |
| 585 | kfree(mddev); |
| 586 | } |
| 587 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 588 | if (bs) |
| 589 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 592 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 593 | { |
| 594 | mutex_init(&mddev->open_mutex); |
| 595 | mutex_init(&mddev->reconfig_mutex); |
| 596 | mutex_init(&mddev->bitmap_info.mutex); |
| 597 | INIT_LIST_HEAD(&mddev->disks); |
| 598 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 599 | init_timer(&mddev->safemode_timer); |
| 600 | atomic_set(&mddev->active, 1); |
| 601 | atomic_set(&mddev->openers, 0); |
| 602 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 603 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 604 | spin_lock_init(&mddev->write_lock); |
| 605 | atomic_set(&mddev->flush_pending, 0); |
| 606 | init_waitqueue_head(&mddev->sb_wait); |
| 607 | init_waitqueue_head(&mddev->recovery_wait); |
| 608 | mddev->reshape_position = MaxSector; |
| 609 | mddev->resync_min = 0; |
| 610 | mddev->resync_max = MaxSector; |
| 611 | mddev->level = LEVEL_NONE; |
| 612 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 613 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 614 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 615 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 617 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 619 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 620 | unit &= ~((1<<MdpMinorShift)-1); |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | retry: |
| 623 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 625 | if (unit) { |
| 626 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 627 | if (mddev->unit == unit) { |
| 628 | mddev_get(mddev); |
| 629 | spin_unlock(&all_mddevs_lock); |
| 630 | kfree(new); |
| 631 | return mddev; |
| 632 | } |
| 633 | |
| 634 | if (new) { |
| 635 | list_add(&new->all_mddevs, &all_mddevs); |
| 636 | spin_unlock(&all_mddevs_lock); |
| 637 | new->hold_active = UNTIL_IOCTL; |
| 638 | return new; |
| 639 | } |
| 640 | } else if (new) { |
| 641 | /* find an unused unit number */ |
| 642 | static int next_minor = 512; |
| 643 | int start = next_minor; |
| 644 | int is_free = 0; |
| 645 | int dev = 0; |
| 646 | while (!is_free) { |
| 647 | dev = MKDEV(MD_MAJOR, next_minor); |
| 648 | next_minor++; |
| 649 | if (next_minor > MINORMASK) |
| 650 | next_minor = 0; |
| 651 | if (next_minor == start) { |
| 652 | /* Oh dear, all in use. */ |
| 653 | spin_unlock(&all_mddevs_lock); |
| 654 | kfree(new); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | is_free = 1; |
| 659 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 660 | if (mddev->unit == dev) { |
| 661 | is_free = 0; |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | new->unit = dev; |
| 666 | new->md_minor = MINOR(dev); |
| 667 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | list_add(&new->all_mddevs, &all_mddevs); |
| 669 | spin_unlock(&all_mddevs_lock); |
| 670 | return new; |
| 671 | } |
| 672 | spin_unlock(&all_mddevs_lock); |
| 673 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 674 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (!new) |
| 676 | return NULL; |
| 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | new->unit = unit; |
| 679 | if (MAJOR(unit) == MD_MAJOR) |
| 680 | new->md_minor = MINOR(unit); |
| 681 | else |
| 682 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 683 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 684 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | goto retry; |
| 687 | } |
| 688 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 689 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 691 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 694 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 695 | { |
| 696 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 697 | } |
| 698 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 699 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 701 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 704 | static struct attribute_group md_redundancy_group; |
| 705 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 706 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 708 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 709 | /* These cannot be removed under reconfig_mutex as |
| 710 | * an access to the files will try to take reconfig_mutex |
| 711 | * while holding the file unremovable, which leads to |
| 712 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 713 | * So hold set sysfs_active while the remove in happeing, |
| 714 | * and anything else which might set ->to_remove or my |
| 715 | * otherwise change the sysfs namespace will fail with |
| 716 | * -EBUSY if sysfs_active is still set. |
| 717 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 718 | * test it under the same mutex to ensure its correct value |
| 719 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 720 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 721 | struct attribute_group *to_remove = mddev->to_remove; |
| 722 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 723 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 724 | mutex_unlock(&mddev->reconfig_mutex); |
| 725 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 726 | if (mddev->kobj.sd) { |
| 727 | if (to_remove != &md_redundancy_group) |
| 728 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 729 | if (mddev->pers == NULL || |
| 730 | mddev->pers->sync_request == NULL) { |
| 731 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 732 | if (mddev->sysfs_action) |
| 733 | sysfs_put(mddev->sysfs_action); |
| 734 | mddev->sysfs_action = NULL; |
| 735 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 736 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 737 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 738 | } else |
| 739 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 741 | /* As we've dropped the mutex we need a spinlock to |
| 742 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 743 | */ |
| 744 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 745 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 746 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 749 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 751 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 753 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | if (rdev->desc_nr == nr) |
| 755 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | return NULL; |
| 758 | } |
| 759 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 760 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 762 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 764 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | if (rdev->bdev->bd_dev == dev) |
| 766 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return NULL; |
| 769 | } |
| 770 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 771 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 772 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 773 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 774 | list_for_each_entry(pers, &pers_list, list) { |
| 775 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 776 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 777 | if (strcmp(pers->name, clevel)==0) |
| 778 | return pers; |
| 779 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 780 | return NULL; |
| 781 | } |
| 782 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 783 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 784 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 786 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 787 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | } |
| 789 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 790 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | if (rdev->sb_page) |
| 793 | MD_BUG(); |
| 794 | |
| 795 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 796 | if (!rdev->sb_page) { |
| 797 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 798 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 804 | static void free_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | { |
| 806 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 807 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | rdev->sb_loaded = 0; |
| 809 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 810 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 811 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 813 | if (rdev->bb_page) { |
| 814 | put_page(rdev->bb_page); |
| 815 | rdev->bb_page = NULL; |
| 816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 820 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 821 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 822 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 823 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 824 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 825 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 826 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 827 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 828 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 829 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 830 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 831 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 832 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 833 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 834 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 835 | } |
| 836 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 837 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 838 | sector_t sector, int size, struct page *page) |
| 839 | { |
| 840 | /* write first size bytes of page to sector of rdev |
| 841 | * Increment mddev->pending_writes before returning |
| 842 | * and decrement it on completion, waking up sb_wait |
| 843 | * if zero is reached. |
| 844 | * If an error occurred, call md_error |
| 845 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 846 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 847 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 848 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 849 | bio->bi_sector = sector; |
| 850 | bio_add_page(bio, page, size, 0); |
| 851 | bio->bi_private = rdev; |
| 852 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 853 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 854 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 855 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 856 | } |
| 857 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 858 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 859 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 860 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 861 | DEFINE_WAIT(wq); |
| 862 | for(;;) { |
| 863 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 864 | if (atomic_read(&mddev->pending_writes)==0) |
| 865 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 866 | schedule(); |
| 867 | } |
| 868 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 869 | } |
| 870 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 871 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 876 | 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] | 877 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 879 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | struct completion event; |
| 881 | int ret; |
| 882 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 883 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 885 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 886 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 887 | if (metadata_op) |
| 888 | bio->bi_sector = sector + rdev->sb_start; |
| 889 | else |
| 890 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | bio_add_page(bio, page, size, 0); |
| 892 | init_completion(&event); |
| 893 | bio->bi_private = &event; |
| 894 | bio->bi_end_io = bi_complete; |
| 895 | submit_bio(rw, bio); |
| 896 | wait_for_completion(&event); |
| 897 | |
| 898 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 899 | bio_put(bio); |
| 900 | return ret; |
| 901 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 902 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 904 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
| 906 | char b[BDEVNAME_SIZE]; |
| 907 | if (!rdev->sb_page) { |
| 908 | MD_BUG(); |
| 909 | return -EINVAL; |
| 910 | } |
| 911 | if (rdev->sb_loaded) |
| 912 | return 0; |
| 913 | |
| 914 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 915 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | goto fail; |
| 917 | rdev->sb_loaded = 1; |
| 918 | return 0; |
| 919 | |
| 920 | fail: |
| 921 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 922 | bdevname(rdev->bdev,b)); |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | |
| 926 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 927 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 928 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 929 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 930 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 931 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 935 | { |
| 936 | int ret; |
| 937 | mdp_super_t *tmp1, *tmp2; |
| 938 | |
| 939 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 940 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 941 | |
| 942 | if (!tmp1 || !tmp2) { |
| 943 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 944 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | goto abort; |
| 946 | } |
| 947 | |
| 948 | *tmp1 = *sb1; |
| 949 | *tmp2 = *sb2; |
| 950 | |
| 951 | /* |
| 952 | * nr_disks is not constant |
| 953 | */ |
| 954 | tmp1->nr_disks = 0; |
| 955 | tmp2->nr_disks = 0; |
| 956 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 957 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 959 | kfree(tmp1); |
| 960 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return ret; |
| 962 | } |
| 963 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 964 | |
| 965 | static u32 md_csum_fold(u32 csum) |
| 966 | { |
| 967 | csum = (csum & 0xffff) + (csum >> 16); |
| 968 | return (csum & 0xffff) + (csum >> 16); |
| 969 | } |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 972 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 973 | u64 newcsum = 0; |
| 974 | u32 *sb32 = (u32*)sb; |
| 975 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | unsigned int disk_csum, csum; |
| 977 | |
| 978 | disk_csum = sb->sb_csum; |
| 979 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 980 | |
| 981 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 982 | newcsum += sb32[i]; |
| 983 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 984 | |
| 985 | |
| 986 | #ifdef CONFIG_ALPHA |
| 987 | /* This used to use csum_partial, which was wrong for several |
| 988 | * reasons including that different results are returned on |
| 989 | * different architectures. It isn't critical that we get exactly |
| 990 | * the same return value as before (we always csum_fold before |
| 991 | * testing, and that removes any differences). However as we |
| 992 | * know that csum_partial always returned a 16bit value on |
| 993 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 994 | */ |
| 995 | sb->sb_csum = md_csum_fold(disk_csum); |
| 996 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 998 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return csum; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | /* |
| 1004 | * Handle superblock details. |
| 1005 | * We want to be able to handle multiple superblock formats |
| 1006 | * so we have a common interface to them all, and an array of |
| 1007 | * different handlers. |
| 1008 | * We rely on user-space to write the initial superblock, and support |
| 1009 | * reading and updating of superblocks. |
| 1010 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1011 | * 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] | 1012 | * loads and validates a superblock on dev. |
| 1013 | * if refdev != NULL, compare superblocks on both devices |
| 1014 | * Return: |
| 1015 | * 0 - dev has a superblock that is compatible with refdev |
| 1016 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1017 | * so dev should be used as the refdev in future |
| 1018 | * -EINVAL superblock incompatible or invalid |
| 1019 | * -othererror e.g. -EIO |
| 1020 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1021 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | * Verify that dev is acceptable into mddev. |
| 1023 | * The first time, mddev->raid_disks will be 0, and data from |
| 1024 | * dev should be merged in. Subsequent calls check that dev |
| 1025 | * is new enough. Return 0 or -EINVAL |
| 1026 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1027 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * Update the superblock for rdev with data in mddev |
| 1029 | * This does not write to disc. |
| 1030 | * |
| 1031 | */ |
| 1032 | |
| 1033 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1034 | char *name; |
| 1035 | struct module *owner; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1036 | int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1037 | int minor_version); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1038 | int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev); |
| 1039 | void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1040 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1041 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | }; |
| 1043 | |
| 1044 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1045 | * Check that the given mddev has no bitmap. |
| 1046 | * |
| 1047 | * This function is called from the run method of all personalities that do not |
| 1048 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1049 | * has a bitmap. Otherwise, it returns 0. |
| 1050 | * |
| 1051 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1052 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1053 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1054 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1055 | return 0; |
| 1056 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1057 | mdname(mddev), mddev->pers->name); |
| 1058 | return 1; |
| 1059 | } |
| 1060 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1061 | |
| 1062 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | * load_super for 0.90.0 |
| 1064 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1065 | 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] | 1066 | { |
| 1067 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1068 | mdp_super_t *sb; |
| 1069 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1072 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | * it's at the end of the disk. |
| 1074 | * |
| 1075 | * It also happens to be a multiple of 4Kb. |
| 1076 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1077 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1079 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | if (ret) return ret; |
| 1081 | |
| 1082 | ret = -EINVAL; |
| 1083 | |
| 1084 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1085 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1088 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1089 | b); |
| 1090 | goto abort; |
| 1091 | } |
| 1092 | |
| 1093 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1094 | sb->minor_version < 90 || |
| 1095 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1097 | sb->major_version, sb->minor_version, |
| 1098 | b); |
| 1099 | goto abort; |
| 1100 | } |
| 1101 | |
| 1102 | if (sb->raid_disks <= 0) |
| 1103 | goto abort; |
| 1104 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1105 | 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] | 1106 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1107 | b); |
| 1108 | goto abort; |
| 1109 | } |
| 1110 | |
| 1111 | rdev->preferred_minor = sb->md_minor; |
| 1112 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1113 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1114 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (sb->level == LEVEL_MULTIPATH) |
| 1117 | rdev->desc_nr = -1; |
| 1118 | else |
| 1119 | rdev->desc_nr = sb->this_disk.number; |
| 1120 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1121 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1123 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1125 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | if (!uuid_equal(refsb, sb)) { |
| 1127 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1128 | b, bdevname(refdev->bdev,b2)); |
| 1129 | goto abort; |
| 1130 | } |
| 1131 | if (!sb_equal(refsb, sb)) { |
| 1132 | printk(KERN_WARNING "md: %s has same UUID" |
| 1133 | " but different superblock to %s\n", |
| 1134 | b, bdevname(refdev->bdev, b2)); |
| 1135 | goto abort; |
| 1136 | } |
| 1137 | ev1 = md_event(sb); |
| 1138 | ev2 = md_event(refsb); |
| 1139 | if (ev1 > ev2) |
| 1140 | ret = 1; |
| 1141 | else |
| 1142 | ret = 0; |
| 1143 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1144 | rdev->sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1145 | /* Limit to 4TB as metadata cannot record more than that */ |
| 1146 | if (rdev->sectors >= (2ULL << 32)) |
| 1147 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1149 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1150 | /* "this cannot possibly happen" ... */ |
| 1151 | ret = -EINVAL; |
| 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | abort: |
| 1154 | return ret; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * validate_super for 0.90.0 |
| 1159 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1160 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { |
| 1162 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1163 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1164 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1166 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1167 | clear_bit(Faulty, &rdev->flags); |
| 1168 | clear_bit(In_sync, &rdev->flags); |
| 1169 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | if (mddev->raid_disks == 0) { |
| 1172 | mddev->major_version = 0; |
| 1173 | mddev->minor_version = sb->minor_version; |
| 1174 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1175 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1176 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | mddev->ctime = sb->ctime; |
| 1178 | mddev->utime = sb->utime; |
| 1179 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1180 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | mddev->layout = sb->layout; |
| 1182 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1183 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1184 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1185 | mddev->bitmap_info.offset = 0; |
| 1186 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1188 | if (mddev->minor_version >= 91) { |
| 1189 | mddev->reshape_position = sb->reshape_position; |
| 1190 | mddev->delta_disks = sb->delta_disks; |
| 1191 | mddev->new_level = sb->new_level; |
| 1192 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1193 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1194 | } else { |
| 1195 | mddev->reshape_position = MaxSector; |
| 1196 | mddev->delta_disks = 0; |
| 1197 | mddev->new_level = mddev->level; |
| 1198 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1199 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1203 | mddev->recovery_cp = MaxSector; |
| 1204 | else { |
| 1205 | if (sb->events_hi == sb->cp_events_hi && |
| 1206 | sb->events_lo == sb->cp_events_lo) { |
| 1207 | mddev->recovery_cp = sb->recovery_cp; |
| 1208 | } else |
| 1209 | mddev->recovery_cp = 0; |
| 1210 | } |
| 1211 | |
| 1212 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1213 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1214 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1215 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1216 | |
| 1217 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1220 | mddev->bitmap_info.file == NULL) |
| 1221 | mddev->bitmap_info.offset = |
| 1222 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1223 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1224 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1225 | /* Insist on good event counter while assembling, except |
| 1226 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1228 | if (sb->disks[rdev->desc_nr].state & ( |
| 1229 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1230 | if (ev1 < mddev->events) |
| 1231 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1232 | } else if (mddev->bitmap) { |
| 1233 | /* if adding to array with a bitmap, then we can accept an |
| 1234 | * older device ... but not too old. |
| 1235 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1236 | if (ev1 < mddev->bitmap->events_cleared) |
| 1237 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1238 | } else { |
| 1239 | if (ev1 < mddev->events) |
| 1240 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1241 | return 0; |
| 1242 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | desc = sb->disks + rdev->desc_nr; |
| 1246 | |
| 1247 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1248 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1249 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1250 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1251 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1253 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1254 | /* active but not in sync implies recovery up to |
| 1255 | * reshape position. We don't know exactly where |
| 1256 | * that is, so set to zero for now */ |
| 1257 | if (mddev->minor_version >= 91) { |
| 1258 | rdev->recovery_offset = 0; |
| 1259 | rdev->raid_disk = desc->raid_disk; |
| 1260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1262 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1263 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1264 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1265 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | /* |
| 1270 | * sync_super for 0.90.0 |
| 1271 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1272 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | { |
| 1274 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1275 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | /* make rdev->sb match mddev data.. |
| 1280 | * |
| 1281 | * 1/ zero out disks |
| 1282 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1283 | * 3/ any empty disks < next_spare become removed |
| 1284 | * |
| 1285 | * disks[0] gets initialised to REMOVED because |
| 1286 | * we cannot be sure from other fields if it has |
| 1287 | * been initialised or not. |
| 1288 | */ |
| 1289 | int i; |
| 1290 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1291 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1292 | rdev->sb_size = MD_SB_BYTES; |
| 1293 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1294 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
| 1296 | memset(sb, 0, sizeof(*sb)); |
| 1297 | |
| 1298 | sb->md_magic = MD_SB_MAGIC; |
| 1299 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | sb->patch_version = mddev->patch_version; |
| 1301 | sb->gvalid_words = 0; /* ignored */ |
| 1302 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1303 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1304 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1305 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1306 | |
| 1307 | sb->ctime = mddev->ctime; |
| 1308 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1309 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | sb->raid_disks = mddev->raid_disks; |
| 1311 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1312 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | sb->utime = mddev->utime; |
| 1314 | sb->state = 0; |
| 1315 | sb->events_hi = (mddev->events>>32); |
| 1316 | sb->events_lo = (u32)mddev->events; |
| 1317 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1318 | if (mddev->reshape_position == MaxSector) |
| 1319 | sb->minor_version = 90; |
| 1320 | else { |
| 1321 | sb->minor_version = 91; |
| 1322 | sb->reshape_position = mddev->reshape_position; |
| 1323 | sb->new_level = mddev->new_level; |
| 1324 | sb->delta_disks = mddev->delta_disks; |
| 1325 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1326 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1327 | } |
| 1328 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | if (mddev->in_sync) |
| 1330 | { |
| 1331 | sb->recovery_cp = mddev->recovery_cp; |
| 1332 | sb->cp_events_hi = (mddev->events>>32); |
| 1333 | sb->cp_events_lo = (u32)mddev->events; |
| 1334 | if (mddev->recovery_cp == MaxSector) |
| 1335 | sb->state = (1<< MD_SB_CLEAN); |
| 1336 | } else |
| 1337 | sb->recovery_cp = 0; |
| 1338 | |
| 1339 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1340 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1342 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1343 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1346 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1348 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1349 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1350 | |
| 1351 | if (rdev2->raid_disk >= 0 && |
| 1352 | sb->minor_version >= 91) |
| 1353 | /* we have nowhere to store the recovery_offset, |
| 1354 | * but if it is not below the reshape_position, |
| 1355 | * we can piggy-back on that. |
| 1356 | */ |
| 1357 | is_active = 1; |
| 1358 | if (rdev2->raid_disk < 0 || |
| 1359 | test_bit(Faulty, &rdev2->flags)) |
| 1360 | is_active = 0; |
| 1361 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1362 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1364 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1365 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | d = &sb->disks[rdev2->desc_nr]; |
| 1367 | nr_disks++; |
| 1368 | d->number = rdev2->desc_nr; |
| 1369 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1370 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1371 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | d->raid_disk = rdev2->raid_disk; |
| 1373 | else |
| 1374 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1375 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1377 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1379 | if (test_bit(In_sync, &rdev2->flags)) |
| 1380 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | active++; |
| 1382 | working++; |
| 1383 | } else { |
| 1384 | d->state = 0; |
| 1385 | spare++; |
| 1386 | working++; |
| 1387 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1388 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1389 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1392 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1393 | mdp_disk_t *d = &sb->disks[i]; |
| 1394 | if (d->state == 0 && d->number == 0) { |
| 1395 | d->number = i; |
| 1396 | d->raid_disk = i; |
| 1397 | d->state = (1<<MD_DISK_REMOVED); |
| 1398 | d->state |= (1<<MD_DISK_FAULTY); |
| 1399 | failed++; |
| 1400 | } |
| 1401 | } |
| 1402 | sb->nr_disks = nr_disks; |
| 1403 | sb->active_disks = active; |
| 1404 | sb->working_disks = working; |
| 1405 | sb->failed_disks = failed; |
| 1406 | sb->spare_disks = spare; |
| 1407 | |
| 1408 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1409 | sb->sb_csum = calc_sb_csum(sb); |
| 1410 | } |
| 1411 | |
| 1412 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1413 | * rdev_size_change for 0.90.0 |
| 1414 | */ |
| 1415 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1416 | 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] | 1417 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1418 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1419 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1420 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1421 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1422 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1423 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1424 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1425 | /* Limit to 4TB as metadata cannot record more than that. |
| 1426 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1427 | */ |
| 1428 | if (num_sectors >= (2ULL << 32)) |
| 1429 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1430 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1431 | rdev->sb_page); |
| 1432 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1433 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | * version 1 superblock |
| 1439 | */ |
| 1440 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1441 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1443 | __le32 disk_csum; |
| 1444 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | unsigned long long newcsum; |
| 1446 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1447 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | int i; |
| 1449 | |
| 1450 | disk_csum = sb->sb_csum; |
| 1451 | sb->sb_csum = 0; |
| 1452 | newcsum = 0; |
| 1453 | for (i=0; size>=4; size -= 4 ) |
| 1454 | newcsum += le32_to_cpu(*isuper++); |
| 1455 | |
| 1456 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1457 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1460 | sb->sb_csum = disk_csum; |
| 1461 | return cpu_to_le32(csum); |
| 1462 | } |
| 1463 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1464 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1465 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1466 | 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] | 1467 | { |
| 1468 | struct mdp_superblock_1 *sb; |
| 1469 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1470 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1472 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | |
| 1474 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1475 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | * It is always aligned to a 4K boundary and |
| 1477 | * depeding on minor_version, it can be: |
| 1478 | * 0: At least 8K, but less than 12K, from end of device |
| 1479 | * 1: At start of device |
| 1480 | * 2: 4K from start of device. |
| 1481 | */ |
| 1482 | switch(minor_version) { |
| 1483 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1484 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1485 | sb_start -= 8*2; |
| 1486 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | break; |
| 1488 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1489 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | break; |
| 1491 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1492 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | break; |
| 1494 | default: |
| 1495 | return -EINVAL; |
| 1496 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1497 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1499 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1500 | * and it is safe to read 4k, so we do that |
| 1501 | */ |
| 1502 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (ret) return ret; |
| 1504 | |
| 1505 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1506 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1509 | sb->major_version != cpu_to_le32(1) || |
| 1510 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1511 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1512 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | return -EINVAL; |
| 1514 | |
| 1515 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1516 | printk("md: invalid superblock checksum on %s\n", |
| 1517 | bdevname(rdev->bdev,b)); |
| 1518 | return -EINVAL; |
| 1519 | } |
| 1520 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1521 | printk("md: data_size too small on %s\n", |
| 1522 | bdevname(rdev->bdev,b)); |
| 1523 | return -EINVAL; |
| 1524 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | rdev->preferred_minor = 0xffff; |
| 1527 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1528 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1530 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1531 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1532 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1533 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1534 | |
| 1535 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1536 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1537 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1538 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1539 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1540 | rdev->desc_nr = -1; |
| 1541 | else |
| 1542 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1543 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1544 | if (!rdev->bb_page) { |
| 1545 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1546 | if (!rdev->bb_page) |
| 1547 | return -ENOMEM; |
| 1548 | } |
| 1549 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1550 | rdev->badblocks.count == 0) { |
| 1551 | /* need to load the bad block list. |
| 1552 | * Currently we limit it to one page. |
| 1553 | */ |
| 1554 | s32 offset; |
| 1555 | sector_t bb_sector; |
| 1556 | u64 *bbp; |
| 1557 | int i; |
| 1558 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1559 | if (sectors > (PAGE_SIZE / 512)) |
| 1560 | return -EINVAL; |
| 1561 | offset = le32_to_cpu(sb->bblog_offset); |
| 1562 | if (offset == 0) |
| 1563 | return -EINVAL; |
| 1564 | bb_sector = (long long)offset; |
| 1565 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1566 | rdev->bb_page, READ, true)) |
| 1567 | return -EIO; |
| 1568 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1569 | rdev->badblocks.shift = sb->bblog_shift; |
| 1570 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1571 | u64 bb = le64_to_cpu(*bbp); |
| 1572 | int count = bb & (0x3ff); |
| 1573 | u64 sector = bb >> 10; |
| 1574 | sector <<= sb->bblog_shift; |
| 1575 | count <<= sb->bblog_shift; |
| 1576 | if (bb + 1 == 0) |
| 1577 | break; |
| 1578 | if (md_set_badblocks(&rdev->badblocks, |
| 1579 | sector, count, 1) == 0) |
| 1580 | return -EINVAL; |
| 1581 | } |
| 1582 | } else if (sb->bblog_offset == 0) |
| 1583 | rdev->badblocks.shift = -1; |
| 1584 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1585 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1586 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1587 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1589 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1592 | sb->level != refsb->level || |
| 1593 | sb->layout != refsb->layout || |
| 1594 | sb->chunksize != refsb->chunksize) { |
| 1595 | printk(KERN_WARNING "md: %s has strangely different" |
| 1596 | " superblock to %s\n", |
| 1597 | bdevname(rdev->bdev,b), |
| 1598 | bdevname(refdev->bdev,b2)); |
| 1599 | return -EINVAL; |
| 1600 | } |
| 1601 | ev1 = le64_to_cpu(sb->events); |
| 1602 | ev2 = le64_to_cpu(refsb->events); |
| 1603 | |
| 1604 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1605 | ret = 1; |
| 1606 | else |
| 1607 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1609 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1610 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1611 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1613 | rdev->sectors = rdev->sb_start; |
| 1614 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1616 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1617 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1618 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1619 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1622 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1624 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1625 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1627 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1628 | clear_bit(Faulty, &rdev->flags); |
| 1629 | clear_bit(In_sync, &rdev->flags); |
| 1630 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | if (mddev->raid_disks == 0) { |
| 1633 | mddev->major_version = 1; |
| 1634 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1635 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1636 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1638 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1639 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1640 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | mddev->layout = le32_to_cpu(sb->layout); |
| 1642 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1643 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1644 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1645 | mddev->bitmap_info.offset = 0; |
| 1646 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
| 1648 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1649 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1650 | |
| 1651 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1652 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1653 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1654 | mddev->bitmap_info.file == NULL ) |
| 1655 | mddev->bitmap_info.offset = |
| 1656 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1657 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1658 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1659 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1660 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1661 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1662 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1663 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1664 | } else { |
| 1665 | mddev->reshape_position = MaxSector; |
| 1666 | mddev->delta_disks = 0; |
| 1667 | mddev->new_level = mddev->level; |
| 1668 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1669 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1670 | } |
| 1671 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1672 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1673 | /* Insist of good event counter while assembling, except for |
| 1674 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1676 | if (rdev->desc_nr >= 0 && |
| 1677 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1678 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1679 | if (ev1 < mddev->events) |
| 1680 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1681 | } else if (mddev->bitmap) { |
| 1682 | /* If adding to array with a bitmap, then we can accept an |
| 1683 | * older device, but not too old. |
| 1684 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1685 | if (ev1 < mddev->bitmap->events_cleared) |
| 1686 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1687 | } else { |
| 1688 | if (ev1 < mddev->events) |
| 1689 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1690 | return 0; |
| 1691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1693 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1694 | if (rdev->desc_nr < 0 || |
| 1695 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1696 | role = 0xffff; |
| 1697 | rdev->desc_nr = -1; |
| 1698 | } else |
| 1699 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | switch(role) { |
| 1701 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | break; |
| 1703 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1704 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | break; |
| 1706 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1707 | if ((le32_to_cpu(sb->feature_map) & |
| 1708 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1709 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1710 | else |
| 1711 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | rdev->raid_disk = role; |
| 1713 | break; |
| 1714 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1715 | if (sb->devflags & WriteMostly1) |
| 1716 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1717 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1718 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | return 0; |
| 1721 | } |
| 1722 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1723 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | { |
| 1725 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1726 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | int max_dev, i; |
| 1728 | /* make rdev->sb match mddev and rdev data. */ |
| 1729 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1730 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
| 1732 | sb->feature_map = 0; |
| 1733 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1734 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1737 | |
| 1738 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1739 | sb->events = cpu_to_le64(mddev->events); |
| 1740 | if (mddev->in_sync) |
| 1741 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1742 | else |
| 1743 | sb->resync_offset = cpu_to_le64(0); |
| 1744 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1745 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1746 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1747 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1748 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1749 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1750 | sb->level = cpu_to_le32(mddev->level); |
| 1751 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1752 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1753 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1754 | sb->devflags |= WriteMostly1; |
| 1755 | else |
| 1756 | sb->devflags &= ~WriteMostly1; |
| 1757 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1758 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1759 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1760 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1761 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1762 | |
| 1763 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1764 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1765 | sb->feature_map |= |
| 1766 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1767 | sb->recovery_offset = |
| 1768 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1771 | if (mddev->reshape_position != MaxSector) { |
| 1772 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1773 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1774 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1775 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1776 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1777 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1778 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1779 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1780 | if (rdev->badblocks.count == 0) |
| 1781 | /* Nothing to do for bad blocks*/ ; |
| 1782 | else if (sb->bblog_offset == 0) |
| 1783 | /* Cannot record bad blocks on this device */ |
| 1784 | md_error(mddev, rdev); |
| 1785 | else { |
| 1786 | struct badblocks *bb = &rdev->badblocks; |
| 1787 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1788 | u64 *p = bb->page; |
| 1789 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1790 | if (bb->changed) { |
| 1791 | unsigned seq; |
| 1792 | |
| 1793 | retry: |
| 1794 | seq = read_seqbegin(&bb->lock); |
| 1795 | |
| 1796 | memset(bbp, 0xff, PAGE_SIZE); |
| 1797 | |
| 1798 | for (i = 0 ; i < bb->count ; i++) { |
| 1799 | u64 internal_bb = *p++; |
| 1800 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1801 | | BB_LEN(internal_bb)); |
| 1802 | *bbp++ = cpu_to_le64(store_bb); |
| 1803 | } |
| 1804 | if (read_seqretry(&bb->lock, seq)) |
| 1805 | goto retry; |
| 1806 | |
| 1807 | bb->sector = (rdev->sb_start + |
| 1808 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1809 | bb->size = le16_to_cpu(sb->bblog_size); |
| 1810 | bb->changed = 0; |
| 1811 | } |
| 1812 | } |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1815 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | if (rdev2->desc_nr+1 > max_dev) |
| 1817 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1818 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1819 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1820 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1821 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1822 | rdev->sb_size = max_dev * 2 + 256; |
| 1823 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1824 | if (rdev->sb_size & bmask) |
| 1825 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1826 | } else |
| 1827 | max_dev = le32_to_cpu(sb->max_dev); |
| 1828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | for (i=0; i<max_dev;i++) |
| 1830 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1831 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1832 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1834 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1836 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1838 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1839 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | else |
| 1841 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1842 | } |
| 1843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1845 | } |
| 1846 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1847 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1848 | 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] | 1849 | { |
| 1850 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1851 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1852 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1853 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1854 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1855 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1856 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1857 | max_sectors -= rdev->data_offset; |
| 1858 | if (!num_sectors || num_sectors > max_sectors) |
| 1859 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1860 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1861 | /* minor version 0 with bitmap we can't move */ |
| 1862 | return 0; |
| 1863 | } else { |
| 1864 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1865 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1866 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1867 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1868 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1869 | if (!num_sectors || num_sectors > max_sectors) |
| 1870 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1871 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1872 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1873 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1874 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1875 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1876 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1877 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1878 | rdev->sb_page); |
| 1879 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1880 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1881 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1883 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | [0] = { |
| 1885 | .name = "0.90.0", |
| 1886 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1887 | .load_super = super_90_load, |
| 1888 | .validate_super = super_90_validate, |
| 1889 | .sync_super = super_90_sync, |
| 1890 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | }, |
| 1892 | [1] = { |
| 1893 | .name = "md-1", |
| 1894 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1895 | .load_super = super_1_load, |
| 1896 | .validate_super = super_1_validate, |
| 1897 | .sync_super = super_1_sync, |
| 1898 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | }, |
| 1900 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1902 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1903 | { |
| 1904 | if (mddev->sync_super) { |
| 1905 | mddev->sync_super(mddev, rdev); |
| 1906 | return; |
| 1907 | } |
| 1908 | |
| 1909 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1910 | |
| 1911 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1912 | } |
| 1913 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1914 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1916 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1918 | rcu_read_lock(); |
| 1919 | rdev_for_each_rcu(rdev, mddev1) |
| 1920 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1921 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1922 | rdev2->bdev->bd_contains) { |
| 1923 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1924 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1925 | } |
| 1926 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | static LIST_HEAD(pending_raid_disks); |
| 1931 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1932 | /* |
| 1933 | * Try to register data integrity profile for an mddev |
| 1934 | * |
| 1935 | * This is called when an array is started and after a disk has been kicked |
| 1936 | * from the array. It only succeeds if all working and active component devices |
| 1937 | * are integrity capable with matching profiles. |
| 1938 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1939 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1940 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1941 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1942 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1943 | if (list_empty(&mddev->disks)) |
| 1944 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1945 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1946 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1947 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1948 | /* skip spares and non-functional disks */ |
| 1949 | if (test_bit(Faulty, &rdev->flags)) |
| 1950 | continue; |
| 1951 | if (rdev->raid_disk < 0) |
| 1952 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1953 | if (!reference) { |
| 1954 | /* Use the first rdev as the reference */ |
| 1955 | reference = rdev; |
| 1956 | continue; |
| 1957 | } |
| 1958 | /* does this rdev's profile match the reference profile? */ |
| 1959 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1960 | rdev->bdev->bd_disk) < 0) |
| 1961 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1962 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1963 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1964 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1965 | /* |
| 1966 | * All component devices are integrity capable and have matching |
| 1967 | * profiles, register the common profile for the md device. |
| 1968 | */ |
| 1969 | if (blk_integrity_register(mddev->gendisk, |
| 1970 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1971 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1972 | mdname(mddev)); |
| 1973 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1974 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1975 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1976 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1977 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1978 | mdname(mddev)); |
| 1979 | return -EINVAL; |
| 1980 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1981 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1982 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1983 | EXPORT_SYMBOL(md_integrity_register); |
| 1984 | |
| 1985 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1986 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1987 | { |
| 1988 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1989 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1990 | |
| 1991 | if (!bi_mddev) /* nothing to do */ |
| 1992 | return; |
| 1993 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1994 | return; |
| 1995 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1996 | rdev->bdev->bd_disk) >= 0) |
| 1997 | return; |
| 1998 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1999 | blk_integrity_unregister(mddev->gendisk); |
| 2000 | } |
| 2001 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2002 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2003 | 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] | 2004 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2005 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2006 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2007 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2008 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
| 2010 | if (rdev->mddev) { |
| 2011 | MD_BUG(); |
| 2012 | return -EINVAL; |
| 2013 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2014 | |
| 2015 | /* prevent duplicates */ |
| 2016 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2017 | return -EEXIST; |
| 2018 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2019 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2020 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2021 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2022 | if (mddev->pers) { |
| 2023 | /* Cannot change size, so fail |
| 2024 | * If mddev->level <= 0, then we don't care |
| 2025 | * about aligning sizes (e.g. linear) |
| 2026 | */ |
| 2027 | if (mddev->level > 0) |
| 2028 | return -ENOSPC; |
| 2029 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2030 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
| 2033 | /* Verify rdev->desc_nr is unique. |
| 2034 | * If it is -1, assign a free number, else |
| 2035 | * check number is not in use |
| 2036 | */ |
| 2037 | if (rdev->desc_nr < 0) { |
| 2038 | int choice = 0; |
| 2039 | if (mddev->pers) choice = mddev->raid_disks; |
| 2040 | while (find_rdev_nr(mddev, choice)) |
| 2041 | choice++; |
| 2042 | rdev->desc_nr = choice; |
| 2043 | } else { |
| 2044 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2045 | return -EBUSY; |
| 2046 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2047 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2048 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2049 | mdname(mddev), mddev->max_disks); |
| 2050 | return -EBUSY; |
| 2051 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2052 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2053 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2054 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2057 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2058 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2059 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2060 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2061 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2062 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2063 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2064 | /* failure here is OK */; |
| 2065 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2066 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2067 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2068 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2069 | |
| 2070 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2071 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2074 | |
| 2075 | fail: |
| 2076 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2077 | b, mdname(mddev)); |
| 2078 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2081 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2082 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2083 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2084 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2085 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2086 | } |
| 2087 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2088 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
| 2090 | char b[BDEVNAME_SIZE]; |
| 2091 | if (!rdev->mddev) { |
| 2092 | MD_BUG(); |
| 2093 | return; |
| 2094 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2095 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2096 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2098 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2099 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2100 | sysfs_put(rdev->sysfs_state); |
| 2101 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2102 | kfree(rdev->badblocks.page); |
| 2103 | rdev->badblocks.count = 0; |
| 2104 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2105 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2106 | * writing to 'remove' to "dev/state". We also need |
| 2107 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2108 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2109 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2110 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2111 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2112 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * prevent the device from being mounted, repartitioned or |
| 2117 | * otherwise reused by a RAID array (or any other kernel |
| 2118 | * subsystem), by bd_claiming the device. |
| 2119 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2120 | 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] | 2121 | { |
| 2122 | int err = 0; |
| 2123 | struct block_device *bdev; |
| 2124 | char b[BDEVNAME_SIZE]; |
| 2125 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2126 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2127 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | if (IS_ERR(bdev)) { |
| 2129 | printk(KERN_ERR "md: could not open %s.\n", |
| 2130 | __bdevname(dev, b)); |
| 2131 | return PTR_ERR(bdev); |
| 2132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | rdev->bdev = bdev; |
| 2134 | return err; |
| 2135 | } |
| 2136 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2137 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | { |
| 2139 | struct block_device *bdev = rdev->bdev; |
| 2140 | rdev->bdev = NULL; |
| 2141 | if (!bdev) |
| 2142 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2143 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | void md_autodetect_dev(dev_t dev); |
| 2147 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2148 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | { |
| 2150 | char b[BDEVNAME_SIZE]; |
| 2151 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2152 | bdevname(rdev->bdev,b)); |
| 2153 | if (rdev->mddev) |
| 2154 | MD_BUG(); |
| 2155 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2157 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2158 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | #endif |
| 2160 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2161 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2164 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | { |
| 2166 | unbind_rdev_from_array(rdev); |
| 2167 | export_rdev(rdev); |
| 2168 | } |
| 2169 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2170 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2172 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2174 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | if (!rdev->mddev) { |
| 2176 | MD_BUG(); |
| 2177 | continue; |
| 2178 | } |
| 2179 | kick_rdev_from_array(rdev); |
| 2180 | } |
| 2181 | if (!list_empty(&mddev->disks)) |
| 2182 | MD_BUG(); |
| 2183 | mddev->raid_disks = 0; |
| 2184 | mddev->major_version = 0; |
| 2185 | } |
| 2186 | |
| 2187 | static void print_desc(mdp_disk_t *desc) |
| 2188 | { |
| 2189 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2190 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2191 | } |
| 2192 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2193 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | { |
| 2195 | int i; |
| 2196 | |
| 2197 | printk(KERN_INFO |
| 2198 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2199 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2200 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2201 | sb->ctime); |
| 2202 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2203 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2204 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2205 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2206 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2207 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2208 | sb->failed_disks, sb->spare_disks, |
| 2209 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2210 | |
| 2211 | printk(KERN_INFO); |
| 2212 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2213 | mdp_disk_t *desc; |
| 2214 | |
| 2215 | desc = sb->disks + i; |
| 2216 | if (desc->number || desc->major || desc->minor || |
| 2217 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2218 | printk(" D %2d: ", i); |
| 2219 | print_desc(desc); |
| 2220 | } |
| 2221 | } |
| 2222 | printk(KERN_INFO "md: THIS: "); |
| 2223 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2226 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2227 | { |
| 2228 | __u8 *uuid; |
| 2229 | |
| 2230 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2231 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2232 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2233 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2234 | le32_to_cpu(sb->major_version), |
| 2235 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2236 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2237 | sb->set_name, |
| 2238 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2239 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2240 | |
| 2241 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2242 | printk(KERN_INFO |
| 2243 | "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] | 2244 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2245 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2246 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2247 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2248 | le32_to_cpu(sb->level), |
| 2249 | (unsigned long long)le64_to_cpu(sb->size), |
| 2250 | le32_to_cpu(sb->raid_disks), |
| 2251 | le32_to_cpu(sb->layout), |
| 2252 | le32_to_cpu(sb->chunksize), |
| 2253 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2254 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2255 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2256 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2257 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2258 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2259 | sb->devflags, |
| 2260 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2261 | (unsigned long long)le64_to_cpu(sb->events), |
| 2262 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2263 | le32_to_cpu(sb->sb_csum), |
| 2264 | le32_to_cpu(sb->max_dev) |
| 2265 | ); |
| 2266 | } |
| 2267 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2268 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | { |
| 2270 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2271 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2272 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2273 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2274 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2276 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2277 | switch (major_version) { |
| 2278 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2279 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2280 | break; |
| 2281 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2282 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2283 | break; |
| 2284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | } else |
| 2286 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2287 | } |
| 2288 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2289 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2291 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2292 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2293 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | char b[BDEVNAME_SIZE]; |
| 2295 | |
| 2296 | printk("\n"); |
| 2297 | printk("md: **********************************\n"); |
| 2298 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2299 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2300 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2302 | if (mddev->bitmap) |
| 2303 | bitmap_print_sb(mddev->bitmap); |
| 2304 | else |
| 2305 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2306 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2308 | printk("\n"); |
| 2309 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2310 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2311 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | } |
| 2313 | printk("md: **********************************\n"); |
| 2314 | printk("\n"); |
| 2315 | } |
| 2316 | |
| 2317 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2318 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2320 | /* Update each superblock (in-memory image), but |
| 2321 | * if we are allowed to, skip spares which already |
| 2322 | * have the right event counter, or have one earlier |
| 2323 | * (which would mean they aren't being marked as dirty |
| 2324 | * with the rest of the array) |
| 2325 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2326 | struct md_rdev *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2327 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2328 | if (rdev->sb_events == mddev->events || |
| 2329 | (nospares && |
| 2330 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2331 | rdev->sb_events+1 == mddev->events)) { |
| 2332 | /* Don't update this superblock */ |
| 2333 | rdev->sb_loaded = 2; |
| 2334 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2335 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2336 | rdev->sb_loaded = 1; |
| 2337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2341 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2343 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2344 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2345 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2346 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2349 | /* First make sure individual recovery_offsets are correct */ |
| 2350 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2351 | if (rdev->raid_disk >= 0 && |
| 2352 | mddev->delta_disks >= 0 && |
| 2353 | !test_bit(In_sync, &rdev->flags) && |
| 2354 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2355 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2356 | |
| 2357 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2358 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2359 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2360 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2361 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2362 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2363 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2364 | if (rdev->badblocks.changed) { |
| 2365 | md_ack_all_badblocks(&rdev->badblocks); |
| 2366 | md_error(mddev, rdev); |
| 2367 | } |
| 2368 | clear_bit(Blocked, &rdev->flags); |
| 2369 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2370 | wake_up(&rdev->blocked_wait); |
| 2371 | } |
| 2372 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2373 | wake_up(&mddev->sb_wait); |
| 2374 | return; |
| 2375 | } |
| 2376 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2377 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2378 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2379 | mddev->utime = get_seconds(); |
| 2380 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2381 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2382 | force_change = 1; |
| 2383 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2384 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2385 | * though if events isn't the right even/odd, we will have to do |
| 2386 | * spares after all |
| 2387 | */ |
| 2388 | nospares = 1; |
| 2389 | if (force_change) |
| 2390 | nospares = 0; |
| 2391 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2392 | /* If the array is degraded, then skipping spares is both |
| 2393 | * dangerous and fairly pointless. |
| 2394 | * Dangerous because a device that was removed from the array |
| 2395 | * might have a event_count that still looks up-to-date, |
| 2396 | * so it can be re-added without a resync. |
| 2397 | * Pointless because if there are any spares to skip, |
| 2398 | * then a recovery will happen and soon that array won't |
| 2399 | * be degraded any more and the spare can go back to sleep then. |
| 2400 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2401 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2402 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2403 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2404 | |
| 2405 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2406 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2407 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2408 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2409 | && mddev->can_decrease_events |
| 2410 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2412 | mddev->can_decrease_events = 0; |
| 2413 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2414 | /* otherwise we have to go forward and ... */ |
| 2415 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2416 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
| 2419 | if (!mddev->events) { |
| 2420 | /* |
| 2421 | * oops, this 64-bit counter should never wrap. |
| 2422 | * Either we are in around ~1 trillion A.C., assuming |
| 2423 | * 1 reboot per second, or we have a bug: |
| 2424 | */ |
| 2425 | MD_BUG(); |
| 2426 | mddev->events --; |
| 2427 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2428 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2429 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2430 | if (rdev->badblocks.changed) |
| 2431 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2432 | if (test_bit(Faulty, &rdev->flags)) |
| 2433 | set_bit(FaultRecorded, &rdev->flags); |
| 2434 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2435 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2436 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2437 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2439 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2440 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2442 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2443 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2445 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2446 | if (rdev->sb_loaded != 1) |
| 2447 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2449 | if (!test_bit(Faulty, &rdev->flags) && |
| 2450 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2451 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2452 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2453 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2454 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2455 | bdevname(rdev->bdev, b), |
| 2456 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2457 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2458 | if (rdev->badblocks.size) { |
| 2459 | md_super_write(mddev, rdev, |
| 2460 | rdev->badblocks.sector, |
| 2461 | rdev->badblocks.size << 9, |
| 2462 | rdev->bb_page); |
| 2463 | rdev->badblocks.size = 0; |
| 2464 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2465 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2466 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2467 | pr_debug("md: %s (skipping faulty)\n", |
| 2468 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2469 | else |
| 2470 | pr_debug("(skipping incremental s/r "); |
| 2471 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2472 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | /* only need to write one superblock... */ |
| 2474 | break; |
| 2475 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2476 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2477 | /* 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] | 2478 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2479 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2480 | if (mddev->in_sync != sync_req || |
| 2481 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2482 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2483 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2484 | goto repeat; |
| 2485 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2486 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2487 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2488 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2489 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2490 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2491 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2492 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2493 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2494 | clear_bit(Blocked, &rdev->flags); |
| 2495 | |
| 2496 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2497 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2498 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2499 | wake_up(&rdev->blocked_wait); |
| 2500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2503 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2504 | * We want to accept with case. For this we use cmd_match. |
| 2505 | */ |
| 2506 | static int cmd_match(const char *cmd, const char *str) |
| 2507 | { |
| 2508 | /* See if cmd, written into a sysfs file, matches |
| 2509 | * str. They must either be the same, or cmd can |
| 2510 | * have a trailing newline |
| 2511 | */ |
| 2512 | while (*cmd && *str && *cmd == *str) { |
| 2513 | cmd++; |
| 2514 | str++; |
| 2515 | } |
| 2516 | if (*cmd == '\n') |
| 2517 | cmd++; |
| 2518 | if (*str || *cmd) |
| 2519 | return 0; |
| 2520 | return 1; |
| 2521 | } |
| 2522 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2523 | struct rdev_sysfs_entry { |
| 2524 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2525 | ssize_t (*show)(struct md_rdev *, char *); |
| 2526 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2527 | }; |
| 2528 | |
| 2529 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2530 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2531 | { |
| 2532 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2533 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2534 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2535 | if (test_bit(Faulty, &rdev->flags) || |
| 2536 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2537 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2538 | sep = ","; |
| 2539 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2540 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2541 | len += sprintf(page+len, "%sin_sync",sep); |
| 2542 | sep = ","; |
| 2543 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2544 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2545 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2546 | sep = ","; |
| 2547 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2548 | if (test_bit(Blocked, &rdev->flags) || |
| 2549 | rdev->badblocks.unacked_exist) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2550 | len += sprintf(page+len, "%sblocked", sep); |
| 2551 | sep = ","; |
| 2552 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2553 | if (!test_bit(Faulty, &rdev->flags) && |
| 2554 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2555 | len += sprintf(page+len, "%sspare", sep); |
| 2556 | sep = ","; |
| 2557 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2558 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2559 | len += sprintf(page+len, "%swrite_error", sep); |
| 2560 | sep = ","; |
| 2561 | } |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2562 | return len+sprintf(page+len, "\n"); |
| 2563 | } |
| 2564 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2565 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2566 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2567 | { |
| 2568 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2569 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2570 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2571 | * writemostly - sets write_mostly |
| 2572 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2573 | * blocked - sets the Blocked flags |
| 2574 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2575 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2576 | * write_error - sets WriteErrorSeen |
| 2577 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2578 | */ |
| 2579 | int err = -EINVAL; |
| 2580 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2581 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2582 | if (test_bit(Faulty, &rdev->flags)) |
| 2583 | err = 0; |
| 2584 | else |
| 2585 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2586 | } else if (cmd_match(buf, "remove")) { |
| 2587 | if (rdev->raid_disk >= 0) |
| 2588 | err = -EBUSY; |
| 2589 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2590 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2591 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2592 | if (mddev->pers) |
| 2593 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2594 | md_new_event(mddev); |
| 2595 | err = 0; |
| 2596 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2597 | } else if (cmd_match(buf, "writemostly")) { |
| 2598 | set_bit(WriteMostly, &rdev->flags); |
| 2599 | err = 0; |
| 2600 | } else if (cmd_match(buf, "-writemostly")) { |
| 2601 | clear_bit(WriteMostly, &rdev->flags); |
| 2602 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2603 | } else if (cmd_match(buf, "blocked")) { |
| 2604 | set_bit(Blocked, &rdev->flags); |
| 2605 | err = 0; |
| 2606 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2607 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2608 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2609 | /* metadata handler doesn't understand badblocks, |
| 2610 | * so we need to fail the device |
| 2611 | */ |
| 2612 | md_error(rdev->mddev, rdev); |
| 2613 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2614 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2615 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2616 | wake_up(&rdev->blocked_wait); |
| 2617 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2618 | md_wakeup_thread(rdev->mddev->thread); |
| 2619 | |
| 2620 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2621 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2622 | set_bit(In_sync, &rdev->flags); |
| 2623 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2624 | } else if (cmd_match(buf, "write_error")) { |
| 2625 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2626 | err = 0; |
| 2627 | } else if (cmd_match(buf, "-write_error")) { |
| 2628 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2629 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2630 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2631 | if (!err) |
| 2632 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2633 | return err ? err : len; |
| 2634 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2635 | static struct rdev_sysfs_entry rdev_state = |
| 2636 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2637 | |
| 2638 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2639 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2640 | { |
| 2641 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2642 | } |
| 2643 | |
| 2644 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2645 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2646 | { |
| 2647 | char *e; |
| 2648 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2649 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2650 | atomic_set(&rdev->corrected_errors, n); |
| 2651 | return len; |
| 2652 | } |
| 2653 | return -EINVAL; |
| 2654 | } |
| 2655 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2656 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2657 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2658 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2659 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2660 | { |
| 2661 | if (rdev->raid_disk < 0) |
| 2662 | return sprintf(page, "none\n"); |
| 2663 | else |
| 2664 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2665 | } |
| 2666 | |
| 2667 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2668 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2669 | { |
| 2670 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2671 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2672 | int slot = simple_strtoul(buf, &e, 10); |
| 2673 | if (strncmp(buf, "none", 4)==0) |
| 2674 | slot = -1; |
| 2675 | else if (e==buf || (*e && *e!= '\n')) |
| 2676 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2677 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2678 | /* Setting 'slot' on an active array requires also |
| 2679 | * updating the 'rd%d' link, and communicating |
| 2680 | * with the personality with ->hot_*_disk. |
| 2681 | * For now we only support removing |
| 2682 | * failed/spare devices. This normally happens automatically, |
| 2683 | * but not when the metadata is externally managed. |
| 2684 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2685 | if (rdev->raid_disk == -1) |
| 2686 | return -EEXIST; |
| 2687 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2688 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2689 | return -EINVAL; |
| 2690 | err = rdev->mddev->pers-> |
| 2691 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2692 | if (err) |
| 2693 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2694 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2695 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2696 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2697 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2698 | } else if (rdev->mddev->pers) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2699 | struct md_rdev *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2700 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2701 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2702 | */ |
| 2703 | |
| 2704 | if (rdev->raid_disk != -1) |
| 2705 | return -EBUSY; |
| 2706 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2707 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2708 | return -EBUSY; |
| 2709 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2710 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2711 | return -EINVAL; |
| 2712 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2713 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2714 | if (rdev2->raid_disk == slot) |
| 2715 | return -EEXIST; |
| 2716 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2717 | if (slot >= rdev->mddev->raid_disks && |
| 2718 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2719 | return -ENOSPC; |
| 2720 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2721 | rdev->raid_disk = slot; |
| 2722 | if (test_bit(In_sync, &rdev->flags)) |
| 2723 | rdev->saved_raid_disk = slot; |
| 2724 | else |
| 2725 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2726 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2727 | err = rdev->mddev->pers-> |
| 2728 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2729 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2730 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2731 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2732 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2733 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2734 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2735 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2736 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2737 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2738 | if (slot >= rdev->mddev->raid_disks && |
| 2739 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2740 | return -ENOSPC; |
| 2741 | rdev->raid_disk = slot; |
| 2742 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2743 | clear_bit(Faulty, &rdev->flags); |
| 2744 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2745 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2746 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2747 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2748 | return len; |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2753 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2754 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2755 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2756 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2757 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2758 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2762 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2763 | { |
| 2764 | char *e; |
| 2765 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2766 | if (e==buf || (*e && *e != '\n')) |
| 2767 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2768 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2769 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2770 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2771 | /* Must set offset before size, so overlap checks |
| 2772 | * can be sane */ |
| 2773 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2774 | rdev->data_offset = offset; |
| 2775 | return len; |
| 2776 | } |
| 2777 | |
| 2778 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2779 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2780 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2781 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2782 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2783 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2784 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2785 | } |
| 2786 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2787 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2788 | { |
| 2789 | /* check if two start/length pairs overlap */ |
| 2790 | if (s1+l1 <= s2) |
| 2791 | return 0; |
| 2792 | if (s2+l2 <= s1) |
| 2793 | return 0; |
| 2794 | return 1; |
| 2795 | } |
| 2796 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2797 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2798 | { |
| 2799 | unsigned long long blocks; |
| 2800 | sector_t new; |
| 2801 | |
| 2802 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2803 | return -EINVAL; |
| 2804 | |
| 2805 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2806 | return -EINVAL; /* sector conversion overflow */ |
| 2807 | |
| 2808 | new = blocks * 2; |
| 2809 | if (new != blocks * 2) |
| 2810 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2811 | |
| 2812 | *sectors = new; |
| 2813 | return 0; |
| 2814 | } |
| 2815 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2816 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2817 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2818 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2819 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2820 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2821 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2822 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2823 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2824 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2825 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2826 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2827 | sectors = super_types[my_mddev->major_version]. |
| 2828 | rdev_size_change(rdev, sectors); |
| 2829 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2830 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2831 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2832 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2833 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2834 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2835 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2836 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2837 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2838 | rdev->sectors = sectors; |
| 2839 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2840 | /* need to check that all other rdevs with the same ->bdev |
| 2841 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2842 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2843 | * we have to change it back, we will have the lock again. |
| 2844 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2845 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2846 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2847 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2848 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2849 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2850 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2851 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2852 | |
| 2853 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2854 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2855 | if (rdev->bdev == rdev2->bdev && |
| 2856 | rdev != rdev2 && |
| 2857 | overlaps(rdev->data_offset, rdev->sectors, |
| 2858 | rdev2->data_offset, |
| 2859 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2860 | overlap = 1; |
| 2861 | break; |
| 2862 | } |
| 2863 | mddev_unlock(mddev); |
| 2864 | if (overlap) { |
| 2865 | mddev_put(mddev); |
| 2866 | break; |
| 2867 | } |
| 2868 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2869 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2870 | if (overlap) { |
| 2871 | /* Someone else could have slipped in a size |
| 2872 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2873 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2874 | * safe, and trust userspace not to race with |
| 2875 | * itself |
| 2876 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2877 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2878 | return -EBUSY; |
| 2879 | } |
| 2880 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2881 | return len; |
| 2882 | } |
| 2883 | |
| 2884 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2885 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2886 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2887 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2888 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2889 | { |
| 2890 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2891 | |
| 2892 | if (test_bit(In_sync, &rdev->flags) || |
| 2893 | recovery_start == MaxSector) |
| 2894 | return sprintf(page, "none\n"); |
| 2895 | |
| 2896 | return sprintf(page, "%llu\n", recovery_start); |
| 2897 | } |
| 2898 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2899 | 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] | 2900 | { |
| 2901 | unsigned long long recovery_start; |
| 2902 | |
| 2903 | if (cmd_match(buf, "none")) |
| 2904 | recovery_start = MaxSector; |
| 2905 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2906 | return -EINVAL; |
| 2907 | |
| 2908 | if (rdev->mddev->pers && |
| 2909 | rdev->raid_disk >= 0) |
| 2910 | return -EBUSY; |
| 2911 | |
| 2912 | rdev->recovery_offset = recovery_start; |
| 2913 | if (recovery_start == MaxSector) |
| 2914 | set_bit(In_sync, &rdev->flags); |
| 2915 | else |
| 2916 | clear_bit(In_sync, &rdev->flags); |
| 2917 | return len; |
| 2918 | } |
| 2919 | |
| 2920 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2921 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2922 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2923 | |
| 2924 | static ssize_t |
| 2925 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2926 | static ssize_t |
| 2927 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2928 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2929 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2930 | { |
| 2931 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2932 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2933 | 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] | 2934 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2935 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2936 | /* Maybe that ack was all we needed */ |
| 2937 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2938 | wake_up(&rdev->blocked_wait); |
| 2939 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2940 | } |
| 2941 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2942 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2943 | |
| 2944 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2945 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2946 | { |
| 2947 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2948 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2949 | 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] | 2950 | { |
| 2951 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2952 | } |
| 2953 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2954 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2955 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2956 | static struct attribute *rdev_default_attrs[] = { |
| 2957 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2958 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2959 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2960 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2961 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2962 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2963 | &rdev_bad_blocks.attr, |
| 2964 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2965 | NULL, |
| 2966 | }; |
| 2967 | static ssize_t |
| 2968 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2969 | { |
| 2970 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2971 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2972 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2973 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2974 | |
| 2975 | if (!entry->show) |
| 2976 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2977 | |
| 2978 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2979 | if (!rv) { |
| 2980 | if (rdev->mddev == NULL) |
| 2981 | rv = -EBUSY; |
| 2982 | else |
| 2983 | rv = entry->show(rdev, page); |
| 2984 | mddev_unlock(mddev); |
| 2985 | } |
| 2986 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | static ssize_t |
| 2990 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2991 | const char *page, size_t length) |
| 2992 | { |
| 2993 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2994 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2995 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2996 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2997 | |
| 2998 | if (!entry->store) |
| 2999 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3000 | if (!capable(CAP_SYS_ADMIN)) |
| 3001 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3002 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3003 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3004 | if (rdev->mddev == NULL) |
| 3005 | rv = -EBUSY; |
| 3006 | else |
| 3007 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3008 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3009 | } |
| 3010 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | static void rdev_free(struct kobject *ko) |
| 3014 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3015 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3016 | kfree(rdev); |
| 3017 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3018 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3019 | .show = rdev_attr_show, |
| 3020 | .store = rdev_attr_store, |
| 3021 | }; |
| 3022 | static struct kobj_type rdev_ktype = { |
| 3023 | .release = rdev_free, |
| 3024 | .sysfs_ops = &rdev_sysfs_ops, |
| 3025 | .default_attrs = rdev_default_attrs, |
| 3026 | }; |
| 3027 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3028 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3029 | { |
| 3030 | rdev->desc_nr = -1; |
| 3031 | rdev->saved_raid_disk = -1; |
| 3032 | rdev->raid_disk = -1; |
| 3033 | rdev->flags = 0; |
| 3034 | rdev->data_offset = 0; |
| 3035 | rdev->sb_events = 0; |
| 3036 | rdev->last_read_error.tv_sec = 0; |
| 3037 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3038 | rdev->sb_loaded = 0; |
| 3039 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3040 | atomic_set(&rdev->nr_pending, 0); |
| 3041 | atomic_set(&rdev->read_errors, 0); |
| 3042 | atomic_set(&rdev->corrected_errors, 0); |
| 3043 | |
| 3044 | INIT_LIST_HEAD(&rdev->same_set); |
| 3045 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3046 | |
| 3047 | /* Add space to store bad block list. |
| 3048 | * This reserves the space even on arrays where it cannot |
| 3049 | * be used - I wonder if that matters |
| 3050 | */ |
| 3051 | rdev->badblocks.count = 0; |
| 3052 | rdev->badblocks.shift = 0; |
| 3053 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3054 | seqlock_init(&rdev->badblocks.lock); |
| 3055 | if (rdev->badblocks.page == NULL) |
| 3056 | return -ENOMEM; |
| 3057 | |
| 3058 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3059 | } |
| 3060 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | /* |
| 3062 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3063 | * |
| 3064 | * mark the device faulty if: |
| 3065 | * |
| 3066 | * - the device is nonexistent (zero size) |
| 3067 | * - the device has no valid superblock |
| 3068 | * |
| 3069 | * a faulty rdev _never_ has rdev->sb set. |
| 3070 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3071 | 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] | 3072 | { |
| 3073 | char b[BDEVNAME_SIZE]; |
| 3074 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3075 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | sector_t size; |
| 3077 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3078 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | if (!rdev) { |
| 3080 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3081 | return ERR_PTR(-ENOMEM); |
| 3082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3084 | err = md_rdev_init(rdev); |
| 3085 | if (err) |
| 3086 | goto abort_free; |
| 3087 | err = alloc_disk_sb(rdev); |
| 3088 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | goto abort_free; |
| 3090 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3091 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | if (err) |
| 3093 | goto abort_free; |
| 3094 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3095 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3096 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3097 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | if (!size) { |
| 3099 | printk(KERN_WARNING |
| 3100 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3101 | bdevname(rdev->bdev,b)); |
| 3102 | err = -EINVAL; |
| 3103 | goto abort_free; |
| 3104 | } |
| 3105 | |
| 3106 | if (super_format >= 0) { |
| 3107 | err = super_types[super_format]. |
| 3108 | load_super(rdev, NULL, super_minor); |
| 3109 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3110 | printk(KERN_WARNING |
| 3111 | "md: %s does not have a valid v%d.%d " |
| 3112 | "superblock, not importing!\n", |
| 3113 | bdevname(rdev->bdev,b), |
| 3114 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | goto abort_free; |
| 3116 | } |
| 3117 | if (err < 0) { |
| 3118 | printk(KERN_WARNING |
| 3119 | "md: could not read %s's sb, not importing!\n", |
| 3120 | bdevname(rdev->bdev,b)); |
| 3121 | goto abort_free; |
| 3122 | } |
| 3123 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3124 | if (super_format == -1) |
| 3125 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3126 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | return rdev; |
| 3129 | |
| 3130 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3131 | if (rdev->bdev) |
| 3132 | unlock_rdev(rdev); |
| 3133 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3134 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | kfree(rdev); |
| 3136 | return ERR_PTR(err); |
| 3137 | } |
| 3138 | |
| 3139 | /* |
| 3140 | * Check a full RAID array for plausibility |
| 3141 | */ |
| 3142 | |
| 3143 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3144 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | { |
| 3146 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3147 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | char b[BDEVNAME_SIZE]; |
| 3149 | |
| 3150 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3151 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | switch (super_types[mddev->major_version]. |
| 3153 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3154 | case 1: |
| 3155 | freshest = rdev; |
| 3156 | break; |
| 3157 | case 0: |
| 3158 | break; |
| 3159 | default: |
| 3160 | printk( KERN_ERR \ |
| 3161 | "md: fatal superblock inconsistency in %s" |
| 3162 | " -- removing from array\n", |
| 3163 | bdevname(rdev->bdev,b)); |
| 3164 | kick_rdev_from_array(rdev); |
| 3165 | } |
| 3166 | |
| 3167 | |
| 3168 | super_types[mddev->major_version]. |
| 3169 | validate_super(mddev, freshest); |
| 3170 | |
| 3171 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3172 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3173 | if (mddev->max_disks && |
| 3174 | (rdev->desc_nr >= mddev->max_disks || |
| 3175 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3176 | printk(KERN_WARNING |
| 3177 | "md: %s: %s: only %d devices permitted\n", |
| 3178 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3179 | mddev->max_disks); |
| 3180 | kick_rdev_from_array(rdev); |
| 3181 | continue; |
| 3182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | if (rdev != freshest) |
| 3184 | if (super_types[mddev->major_version]. |
| 3185 | validate_super(mddev, rdev)) { |
| 3186 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3187 | " from array!\n", |
| 3188 | bdevname(rdev->bdev,b)); |
| 3189 | kick_rdev_from_array(rdev); |
| 3190 | continue; |
| 3191 | } |
| 3192 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3193 | rdev->desc_nr = i++; |
| 3194 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3195 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3196 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3197 | rdev->raid_disk = -1; |
| 3198 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | } |
| 3200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3203 | /* Read a fixed-point number. |
| 3204 | * Numbers in sysfs attributes should be in "standard" units where |
| 3205 | * possible, so time should be in seconds. |
| 3206 | * However we internally use a a much smaller unit such as |
| 3207 | * milliseconds or jiffies. |
| 3208 | * This function takes a decimal number with a possible fractional |
| 3209 | * component, and produces an integer which is the result of |
| 3210 | * multiplying that number by 10^'scale'. |
| 3211 | * all without any floating-point arithmetic. |
| 3212 | */ |
| 3213 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3214 | { |
| 3215 | unsigned long result = 0; |
| 3216 | long decimals = -1; |
| 3217 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3218 | if (*cp == '.') |
| 3219 | decimals = 0; |
| 3220 | else if (decimals < scale) { |
| 3221 | unsigned int value; |
| 3222 | value = *cp - '0'; |
| 3223 | result = result * 10 + value; |
| 3224 | if (decimals >= 0) |
| 3225 | decimals++; |
| 3226 | } |
| 3227 | cp++; |
| 3228 | } |
| 3229 | if (*cp == '\n') |
| 3230 | cp++; |
| 3231 | if (*cp) |
| 3232 | return -EINVAL; |
| 3233 | if (decimals < 0) |
| 3234 | decimals = 0; |
| 3235 | while (decimals < scale) { |
| 3236 | result *= 10; |
| 3237 | decimals ++; |
| 3238 | } |
| 3239 | *res = result; |
| 3240 | return 0; |
| 3241 | } |
| 3242 | |
| 3243 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3244 | static void md_safemode_timeout(unsigned long data); |
| 3245 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3246 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3247 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3248 | { |
| 3249 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3250 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3251 | } |
| 3252 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3253 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3254 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3255 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3256 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3257 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3258 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3259 | if (msec == 0) |
| 3260 | mddev->safemode_delay = 0; |
| 3261 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3262 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3263 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3264 | if (mddev->safemode_delay == 0) |
| 3265 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3266 | if (mddev->safemode_delay < old_delay) |
| 3267 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3268 | } |
| 3269 | return len; |
| 3270 | } |
| 3271 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3272 | __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] | 3273 | |
| 3274 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3275 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3276 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3277 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3278 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3279 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3280 | else if (mddev->clevel[0]) |
| 3281 | return sprintf(page, "%s\n", mddev->clevel); |
| 3282 | else if (mddev->level != LEVEL_NONE) |
| 3283 | return sprintf(page, "%d\n", mddev->level); |
| 3284 | else |
| 3285 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3286 | } |
| 3287 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3288 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3289 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3290 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3291 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3292 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3293 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3294 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3295 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3296 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3297 | |
| 3298 | if (mddev->pers == NULL) { |
| 3299 | if (len == 0) |
| 3300 | return 0; |
| 3301 | if (len >= sizeof(mddev->clevel)) |
| 3302 | return -ENOSPC; |
| 3303 | strncpy(mddev->clevel, buf, len); |
| 3304 | if (mddev->clevel[len-1] == '\n') |
| 3305 | len--; |
| 3306 | mddev->clevel[len] = 0; |
| 3307 | mddev->level = LEVEL_NONE; |
| 3308 | return rv; |
| 3309 | } |
| 3310 | |
| 3311 | /* request to change the personality. Need to ensure: |
| 3312 | * - array is not engaged in resync/recovery/reshape |
| 3313 | * - old personality can be suspended |
| 3314 | * - new personality will access other array. |
| 3315 | */ |
| 3316 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3317 | if (mddev->sync_thread || |
| 3318 | mddev->reshape_position != MaxSector || |
| 3319 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3320 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3321 | |
| 3322 | if (!mddev->pers->quiesce) { |
| 3323 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3324 | mdname(mddev), mddev->pers->name); |
| 3325 | return -EINVAL; |
| 3326 | } |
| 3327 | |
| 3328 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3329 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3330 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3331 | strncpy(clevel, buf, len); |
| 3332 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3333 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3334 | clevel[len] = 0; |
| 3335 | if (strict_strtol(clevel, 10, &level)) |
| 3336 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3337 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3338 | if (request_module("md-%s", clevel) != 0) |
| 3339 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3340 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3341 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3342 | if (!pers || !try_module_get(pers->owner)) { |
| 3343 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3344 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3345 | return -EINVAL; |
| 3346 | } |
| 3347 | spin_unlock(&pers_lock); |
| 3348 | |
| 3349 | if (pers == mddev->pers) { |
| 3350 | /* Nothing to do! */ |
| 3351 | module_put(pers->owner); |
| 3352 | return rv; |
| 3353 | } |
| 3354 | if (!pers->takeover) { |
| 3355 | module_put(pers->owner); |
| 3356 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3357 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3358 | return -EINVAL; |
| 3359 | } |
| 3360 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3361 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3362 | rdev->new_raid_disk = rdev->raid_disk; |
| 3363 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3364 | /* ->takeover must set new_* and/or delta_disks |
| 3365 | * if it succeeds, and may set them when it fails. |
| 3366 | */ |
| 3367 | priv = pers->takeover(mddev); |
| 3368 | if (IS_ERR(priv)) { |
| 3369 | mddev->new_level = mddev->level; |
| 3370 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3371 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3372 | mddev->raid_disks -= mddev->delta_disks; |
| 3373 | mddev->delta_disks = 0; |
| 3374 | module_put(pers->owner); |
| 3375 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3376 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3377 | return PTR_ERR(priv); |
| 3378 | } |
| 3379 | |
| 3380 | /* Looks like we have a winner */ |
| 3381 | mddev_suspend(mddev); |
| 3382 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3383 | |
| 3384 | if (mddev->pers->sync_request == NULL && |
| 3385 | pers->sync_request != NULL) { |
| 3386 | /* need to add the md_redundancy_group */ |
| 3387 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3388 | printk(KERN_WARNING |
| 3389 | "md: cannot register extra attributes for %s\n", |
| 3390 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3391 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3392 | } |
| 3393 | if (mddev->pers->sync_request != NULL && |
| 3394 | pers->sync_request == NULL) { |
| 3395 | /* need to remove the md_redundancy_group */ |
| 3396 | if (mddev->to_remove == NULL) |
| 3397 | mddev->to_remove = &md_redundancy_group; |
| 3398 | } |
| 3399 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3400 | if (mddev->pers->sync_request == NULL && |
| 3401 | mddev->external) { |
| 3402 | /* We are converting from a no-redundancy array |
| 3403 | * to a redundancy array and metadata is managed |
| 3404 | * externally so we need to be sure that writes |
| 3405 | * won't block due to a need to transition |
| 3406 | * clean->dirty |
| 3407 | * until external management is started. |
| 3408 | */ |
| 3409 | mddev->in_sync = 0; |
| 3410 | mddev->safemode_delay = 0; |
| 3411 | mddev->safemode = 0; |
| 3412 | } |
| 3413 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3414 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3415 | if (rdev->raid_disk < 0) |
| 3416 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3417 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3418 | rdev->new_raid_disk = -1; |
| 3419 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3420 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3421 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3422 | } |
| 3423 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3424 | if (rdev->raid_disk < 0) |
| 3425 | continue; |
| 3426 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3427 | continue; |
| 3428 | rdev->raid_disk = rdev->new_raid_disk; |
| 3429 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3430 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3431 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3432 | if (sysfs_link_rdev(mddev, rdev)) |
| 3433 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3434 | " for %s after level change\n", |
| 3435 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3436 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3440 | mddev->pers = pers; |
| 3441 | mddev->private = priv; |
| 3442 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3443 | mddev->level = mddev->new_level; |
| 3444 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3445 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3446 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3447 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3448 | if (mddev->pers->sync_request == NULL) { |
| 3449 | /* this is now an array without redundancy, so |
| 3450 | * it must always be in_sync |
| 3451 | */ |
| 3452 | mddev->in_sync = 1; |
| 3453 | del_timer_sync(&mddev->safemode_timer); |
| 3454 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3455 | pers->run(mddev); |
| 3456 | mddev_resume(mddev); |
| 3457 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3458 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3459 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3460 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3461 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3462 | return rv; |
| 3463 | } |
| 3464 | |
| 3465 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3466 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3467 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3468 | |
| 3469 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3470 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3471 | { |
| 3472 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3473 | if (mddev->reshape_position != MaxSector && |
| 3474 | mddev->layout != mddev->new_layout) |
| 3475 | return sprintf(page, "%d (%d)\n", |
| 3476 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3477 | return sprintf(page, "%d\n", mddev->layout); |
| 3478 | } |
| 3479 | |
| 3480 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3481 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3482 | { |
| 3483 | char *e; |
| 3484 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3485 | |
| 3486 | if (!*buf || (*e && *e != '\n')) |
| 3487 | return -EINVAL; |
| 3488 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3489 | if (mddev->pers) { |
| 3490 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3491 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3492 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3493 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3494 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3495 | if (err) { |
| 3496 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3497 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3498 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3499 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3500 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3501 | if (mddev->reshape_position == MaxSector) |
| 3502 | mddev->layout = n; |
| 3503 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3504 | return len; |
| 3505 | } |
| 3506 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3507 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3508 | |
| 3509 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3510 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3511 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3512 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3513 | if (mddev->raid_disks == 0) |
| 3514 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3515 | if (mddev->reshape_position != MaxSector && |
| 3516 | mddev->delta_disks != 0) |
| 3517 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3518 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3519 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3520 | } |
| 3521 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3522 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3523 | |
| 3524 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3525 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3526 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3527 | char *e; |
| 3528 | int rv = 0; |
| 3529 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3530 | |
| 3531 | if (!*buf || (*e && *e != '\n')) |
| 3532 | return -EINVAL; |
| 3533 | |
| 3534 | if (mddev->pers) |
| 3535 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3536 | else if (mddev->reshape_position != MaxSector) { |
| 3537 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3538 | mddev->delta_disks = n - olddisks; |
| 3539 | mddev->raid_disks = n; |
| 3540 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3541 | mddev->raid_disks = n; |
| 3542 | return rv ? rv : len; |
| 3543 | } |
| 3544 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3545 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3546 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3547 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3548 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3549 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3550 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3551 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3552 | return sprintf(page, "%d (%d)\n", |
| 3553 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3554 | mddev->chunk_sectors << 9); |
| 3555 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3559 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3560 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3561 | char *e; |
| 3562 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3563 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3564 | if (!*buf || (*e && *e != '\n')) |
| 3565 | return -EINVAL; |
| 3566 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3567 | if (mddev->pers) { |
| 3568 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3569 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3570 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3571 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3572 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3573 | if (err) { |
| 3574 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3575 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3576 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3577 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3578 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3579 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3580 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3581 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3582 | return len; |
| 3583 | } |
| 3584 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3585 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3586 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3587 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3588 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3589 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3590 | if (mddev->recovery_cp == MaxSector) |
| 3591 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3592 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3593 | } |
| 3594 | |
| 3595 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3596 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3597 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3598 | char *e; |
| 3599 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3600 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3601 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3602 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3603 | if (cmd_match(buf, "none")) |
| 3604 | n = MaxSector; |
| 3605 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3606 | return -EINVAL; |
| 3607 | |
| 3608 | mddev->recovery_cp = n; |
| 3609 | return len; |
| 3610 | } |
| 3611 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3612 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3613 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3614 | /* |
| 3615 | * The array state can be: |
| 3616 | * |
| 3617 | * clear |
| 3618 | * No devices, no size, no level |
| 3619 | * Equivalent to STOP_ARRAY ioctl |
| 3620 | * inactive |
| 3621 | * May have some settings, but array is not active |
| 3622 | * all IO results in error |
| 3623 | * When written, doesn't tear down array, but just stops it |
| 3624 | * suspended (not supported yet) |
| 3625 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3626 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3627 | * readonly |
| 3628 | * no resync can happen. no superblocks get written. |
| 3629 | * write requests fail |
| 3630 | * read-auto |
| 3631 | * like readonly, but behaves like 'clean' on a write request. |
| 3632 | * |
| 3633 | * clean - no pending writes, but otherwise active. |
| 3634 | * When written to inactive array, starts without resync |
| 3635 | * If a write request arrives then |
| 3636 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3637 | * if not known, block and switch to write-pending |
| 3638 | * If written to an active array that has pending writes, then fails. |
| 3639 | * active |
| 3640 | * fully active: IO and resync can be happening. |
| 3641 | * When written to inactive array, starts with resync |
| 3642 | * |
| 3643 | * write-pending |
| 3644 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3645 | * |
| 3646 | * active-idle |
| 3647 | * like active, but no writes have been seen for a while (100msec). |
| 3648 | * |
| 3649 | */ |
| 3650 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3651 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3652 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3653 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3654 | "write-pending", "active-idle", NULL }; |
| 3655 | |
| 3656 | static int match_word(const char *word, char **list) |
| 3657 | { |
| 3658 | int n; |
| 3659 | for (n=0; list[n]; n++) |
| 3660 | if (cmd_match(word, list[n])) |
| 3661 | break; |
| 3662 | return n; |
| 3663 | } |
| 3664 | |
| 3665 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3666 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3667 | { |
| 3668 | enum array_state st = inactive; |
| 3669 | |
| 3670 | if (mddev->pers) |
| 3671 | switch(mddev->ro) { |
| 3672 | case 1: |
| 3673 | st = readonly; |
| 3674 | break; |
| 3675 | case 2: |
| 3676 | st = read_auto; |
| 3677 | break; |
| 3678 | case 0: |
| 3679 | if (mddev->in_sync) |
| 3680 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3681 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3682 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3683 | else if (mddev->safemode) |
| 3684 | st = active_idle; |
| 3685 | else |
| 3686 | st = active; |
| 3687 | } |
| 3688 | else { |
| 3689 | if (list_empty(&mddev->disks) && |
| 3690 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3691 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3692 | st = clear; |
| 3693 | else |
| 3694 | st = inactive; |
| 3695 | } |
| 3696 | return sprintf(page, "%s\n", array_states[st]); |
| 3697 | } |
| 3698 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3699 | static int do_md_stop(struct mddev * mddev, int ro, int is_open); |
| 3700 | static int md_set_readonly(struct mddev * mddev, int is_open); |
| 3701 | static int do_md_run(struct mddev * mddev); |
| 3702 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3703 | |
| 3704 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3705 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3706 | { |
| 3707 | int err = -EINVAL; |
| 3708 | enum array_state st = match_word(buf, array_states); |
| 3709 | switch(st) { |
| 3710 | case bad_word: |
| 3711 | break; |
| 3712 | case clear: |
| 3713 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3714 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3715 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3716 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3717 | break; |
| 3718 | case inactive: |
| 3719 | /* stopping an active array */ |
| 3720 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3721 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3722 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3723 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3724 | } else |
| 3725 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3726 | break; |
| 3727 | case suspended: |
| 3728 | break; /* not supported yet */ |
| 3729 | case readonly: |
| 3730 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3731 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3732 | else { |
| 3733 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3734 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3735 | err = do_md_run(mddev); |
| 3736 | } |
| 3737 | break; |
| 3738 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3739 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3740 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3741 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3742 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3743 | err = restart_array(mddev); |
| 3744 | if (err == 0) { |
| 3745 | mddev->ro = 2; |
| 3746 | set_disk_ro(mddev->gendisk, 0); |
| 3747 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3748 | } else { |
| 3749 | mddev->ro = 2; |
| 3750 | err = do_md_run(mddev); |
| 3751 | } |
| 3752 | break; |
| 3753 | case clean: |
| 3754 | if (mddev->pers) { |
| 3755 | restart_array(mddev); |
| 3756 | spin_lock_irq(&mddev->write_lock); |
| 3757 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3758 | if (mddev->in_sync == 0) { |
| 3759 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3760 | if (mddev->safemode == 1) |
| 3761 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3762 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3763 | } |
| 3764 | err = 0; |
| 3765 | } else |
| 3766 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3767 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3768 | } else |
| 3769 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3770 | break; |
| 3771 | case active: |
| 3772 | if (mddev->pers) { |
| 3773 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3774 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | wake_up(&mddev->sb_wait); |
| 3776 | err = 0; |
| 3777 | } else { |
| 3778 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3779 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3780 | err = do_md_run(mddev); |
| 3781 | } |
| 3782 | break; |
| 3783 | case write_pending: |
| 3784 | case active_idle: |
| 3785 | /* these cannot be set */ |
| 3786 | break; |
| 3787 | } |
| 3788 | if (err) |
| 3789 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3790 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame^] | 3791 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3792 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3793 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3794 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3795 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3796 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3797 | static struct md_sysfs_entry md_array_state = |
| 3798 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3799 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3800 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3801 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3802 | return sprintf(page, "%d\n", |
| 3803 | atomic_read(&mddev->max_corr_read_errors)); |
| 3804 | } |
| 3805 | |
| 3806 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3807 | 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] | 3808 | { |
| 3809 | char *e; |
| 3810 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3811 | |
| 3812 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3813 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3814 | return len; |
| 3815 | } |
| 3816 | return -EINVAL; |
| 3817 | } |
| 3818 | |
| 3819 | static struct md_sysfs_entry max_corr_read_errors = |
| 3820 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3821 | max_corrected_read_errors_store); |
| 3822 | |
| 3823 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3824 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3825 | { |
| 3826 | return -EINVAL; |
| 3827 | } |
| 3828 | |
| 3829 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3830 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3831 | { |
| 3832 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3833 | /* The new device is added to the array. |
| 3834 | * If the array has a persistent superblock, we read the |
| 3835 | * superblock to initialise info and check validity. |
| 3836 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3837 | * which mainly checks size. |
| 3838 | */ |
| 3839 | char *e; |
| 3840 | int major = simple_strtoul(buf, &e, 10); |
| 3841 | int minor; |
| 3842 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3843 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3844 | int err; |
| 3845 | |
| 3846 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3847 | return -EINVAL; |
| 3848 | minor = simple_strtoul(e+1, &e, 10); |
| 3849 | if (*e && *e != '\n') |
| 3850 | return -EINVAL; |
| 3851 | dev = MKDEV(major, minor); |
| 3852 | if (major != MAJOR(dev) || |
| 3853 | minor != MINOR(dev)) |
| 3854 | return -EOVERFLOW; |
| 3855 | |
| 3856 | |
| 3857 | if (mddev->persistent) { |
| 3858 | rdev = md_import_device(dev, mddev->major_version, |
| 3859 | mddev->minor_version); |
| 3860 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3861 | struct md_rdev *rdev0 |
| 3862 | = list_entry(mddev->disks.next, |
| 3863 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3864 | err = super_types[mddev->major_version] |
| 3865 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3866 | if (err < 0) |
| 3867 | goto out; |
| 3868 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3869 | } else if (mddev->external) |
| 3870 | rdev = md_import_device(dev, -2, -1); |
| 3871 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3872 | rdev = md_import_device(dev, -1, -1); |
| 3873 | |
| 3874 | if (IS_ERR(rdev)) |
| 3875 | return PTR_ERR(rdev); |
| 3876 | err = bind_rdev_to_array(rdev, mddev); |
| 3877 | out: |
| 3878 | if (err) |
| 3879 | export_rdev(rdev); |
| 3880 | return err ? err : len; |
| 3881 | } |
| 3882 | |
| 3883 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3884 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3885 | |
| 3886 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3887 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3888 | { |
| 3889 | char *end; |
| 3890 | unsigned long chunk, end_chunk; |
| 3891 | |
| 3892 | if (!mddev->bitmap) |
| 3893 | goto out; |
| 3894 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3895 | while (*buf) { |
| 3896 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3897 | if (buf == end) break; |
| 3898 | if (*end == '-') { /* range */ |
| 3899 | buf = end + 1; |
| 3900 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3901 | if (buf == end) break; |
| 3902 | } |
| 3903 | if (*end && !isspace(*end)) break; |
| 3904 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3905 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3906 | } |
| 3907 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3908 | out: |
| 3909 | return len; |
| 3910 | } |
| 3911 | |
| 3912 | static struct md_sysfs_entry md_bitmap = |
| 3913 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3914 | |
| 3915 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3916 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3917 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3918 | return sprintf(page, "%llu\n", |
| 3919 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3920 | } |
| 3921 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3922 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3923 | |
| 3924 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3925 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3926 | { |
| 3927 | /* If array is inactive, we can reduce the component size, but |
| 3928 | * not increase it (except from 0). |
| 3929 | * If array is active, we can try an on-line resize |
| 3930 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3931 | sector_t sectors; |
| 3932 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3933 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3934 | if (err < 0) |
| 3935 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3936 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3937 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3938 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3939 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3940 | if (mddev->dev_sectors == 0 || |
| 3941 | mddev->dev_sectors > sectors) |
| 3942 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3943 | else |
| 3944 | err = -ENOSPC; |
| 3945 | } |
| 3946 | return err ? err : len; |
| 3947 | } |
| 3948 | |
| 3949 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3950 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3951 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3952 | |
| 3953 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3954 | * This is one of |
| 3955 | * 'none' for arrays with no metadata (good luck...) |
| 3956 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3957 | * or N.M for internally known formats |
| 3958 | */ |
| 3959 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3960 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3961 | { |
| 3962 | if (mddev->persistent) |
| 3963 | return sprintf(page, "%d.%d\n", |
| 3964 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3965 | else if (mddev->external) |
| 3966 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3967 | else |
| 3968 | return sprintf(page, "none\n"); |
| 3969 | } |
| 3970 | |
| 3971 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3972 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3973 | { |
| 3974 | int major, minor; |
| 3975 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3976 | /* Changing the details of 'external' metadata is |
| 3977 | * always permitted. Otherwise there must be |
| 3978 | * no devices attached to the array. |
| 3979 | */ |
| 3980 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3981 | ; |
| 3982 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3983 | return -EBUSY; |
| 3984 | |
| 3985 | if (cmd_match(buf, "none")) { |
| 3986 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3987 | mddev->external = 0; |
| 3988 | mddev->major_version = 0; |
| 3989 | mddev->minor_version = 90; |
| 3990 | return len; |
| 3991 | } |
| 3992 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3993 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3994 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3995 | namelen = sizeof(mddev->metadata_type)-1; |
| 3996 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3997 | mddev->metadata_type[namelen] = 0; |
| 3998 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3999 | mddev->metadata_type[--namelen] = 0; |
| 4000 | mddev->persistent = 0; |
| 4001 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4002 | mddev->major_version = 0; |
| 4003 | mddev->minor_version = 90; |
| 4004 | return len; |
| 4005 | } |
| 4006 | major = simple_strtoul(buf, &e, 10); |
| 4007 | if (e==buf || *e != '.') |
| 4008 | return -EINVAL; |
| 4009 | buf = e+1; |
| 4010 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4011 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4012 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4013 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4014 | return -ENOENT; |
| 4015 | mddev->major_version = major; |
| 4016 | mddev->minor_version = minor; |
| 4017 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4018 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4019 | return len; |
| 4020 | } |
| 4021 | |
| 4022 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4023 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4024 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4025 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4026 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4027 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4028 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4029 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4030 | type = "frozen"; |
| 4031 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4032 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4033 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4034 | type = "reshape"; |
| 4035 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4036 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4037 | type = "resync"; |
| 4038 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4039 | type = "check"; |
| 4040 | else |
| 4041 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4042 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4043 | type = "recover"; |
| 4044 | } |
| 4045 | return sprintf(page, "%s\n", type); |
| 4046 | } |
| 4047 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4048 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4049 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4050 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4051 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4052 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4053 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4054 | return -EINVAL; |
| 4055 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4056 | if (cmd_match(page, "frozen")) |
| 4057 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4058 | else |
| 4059 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4060 | |
| 4061 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4062 | if (mddev->sync_thread) { |
| 4063 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4064 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4065 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4066 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4067 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4068 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4069 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4070 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4071 | else if (cmd_match(page, "recover")) { |
| 4072 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4073 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4074 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4075 | int err; |
| 4076 | if (mddev->pers->start_reshape == NULL) |
| 4077 | return -EINVAL; |
| 4078 | err = mddev->pers->start_reshape(mddev); |
| 4079 | if (err) |
| 4080 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4081 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4082 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4083 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4084 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4085 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4086 | return -EINVAL; |
| 4087 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4088 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4089 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4090 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4091 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4092 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4093 | return len; |
| 4094 | } |
| 4095 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4096 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4097 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4098 | { |
| 4099 | return sprintf(page, "%llu\n", |
| 4100 | (unsigned long long) mddev->resync_mismatches); |
| 4101 | } |
| 4102 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4103 | static struct md_sysfs_entry md_scan_mode = |
| 4104 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4105 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4106 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4107 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4108 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4109 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4110 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4111 | { |
| 4112 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4113 | mddev->sync_speed_min ? "local": "system"); |
| 4114 | } |
| 4115 | |
| 4116 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4117 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4118 | { |
| 4119 | int min; |
| 4120 | char *e; |
| 4121 | if (strncmp(buf, "system", 6)==0) { |
| 4122 | mddev->sync_speed_min = 0; |
| 4123 | return len; |
| 4124 | } |
| 4125 | min = simple_strtoul(buf, &e, 10); |
| 4126 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4127 | return -EINVAL; |
| 4128 | mddev->sync_speed_min = min; |
| 4129 | return len; |
| 4130 | } |
| 4131 | |
| 4132 | static struct md_sysfs_entry md_sync_min = |
| 4133 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4134 | |
| 4135 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4136 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4137 | { |
| 4138 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4139 | mddev->sync_speed_max ? "local": "system"); |
| 4140 | } |
| 4141 | |
| 4142 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4143 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4144 | { |
| 4145 | int max; |
| 4146 | char *e; |
| 4147 | if (strncmp(buf, "system", 6)==0) { |
| 4148 | mddev->sync_speed_max = 0; |
| 4149 | return len; |
| 4150 | } |
| 4151 | max = simple_strtoul(buf, &e, 10); |
| 4152 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4153 | return -EINVAL; |
| 4154 | mddev->sync_speed_max = max; |
| 4155 | return len; |
| 4156 | } |
| 4157 | |
| 4158 | static struct md_sysfs_entry md_sync_max = |
| 4159 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4160 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4161 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4162 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4163 | { |
| 4164 | return sprintf(page, "%d\n", mddev->degraded); |
| 4165 | } |
| 4166 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4167 | |
| 4168 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4169 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4170 | { |
| 4171 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4172 | } |
| 4173 | |
| 4174 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4175 | 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] | 4176 | { |
| 4177 | long n; |
| 4178 | |
| 4179 | if (strict_strtol(buf, 10, &n)) |
| 4180 | return -EINVAL; |
| 4181 | |
| 4182 | if (n != 0 && n != 1) |
| 4183 | return -EINVAL; |
| 4184 | |
| 4185 | mddev->parallel_resync = n; |
| 4186 | |
| 4187 | if (mddev->sync_thread) |
| 4188 | wake_up(&resync_wait); |
| 4189 | |
| 4190 | return len; |
| 4191 | } |
| 4192 | |
| 4193 | /* force parallel resync, even with shared block devices */ |
| 4194 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4195 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4196 | sync_force_parallel_show, sync_force_parallel_store); |
| 4197 | |
| 4198 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4199 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4200 | { |
| 4201 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4202 | if (mddev->curr_resync == 0) |
| 4203 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4204 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4205 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4206 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4207 | db = resync - mddev->resync_mark_cnt; |
| 4208 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4209 | } |
| 4210 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4211 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4212 | |
| 4213 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4214 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4215 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4216 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4217 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4218 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4219 | return sprintf(page, "none\n"); |
| 4220 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4221 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4222 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4223 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4224 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4225 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4226 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4227 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4228 | } |
| 4229 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4230 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4231 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4232 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4233 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4234 | { |
| 4235 | return sprintf(page, "%llu\n", |
| 4236 | (unsigned long long)mddev->resync_min); |
| 4237 | } |
| 4238 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4239 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4240 | { |
| 4241 | unsigned long long min; |
| 4242 | if (strict_strtoull(buf, 10, &min)) |
| 4243 | return -EINVAL; |
| 4244 | if (min > mddev->resync_max) |
| 4245 | return -EINVAL; |
| 4246 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4247 | return -EBUSY; |
| 4248 | |
| 4249 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4250 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4251 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4252 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4253 | return -EINVAL; |
| 4254 | } |
| 4255 | mddev->resync_min = min; |
| 4256 | |
| 4257 | return len; |
| 4258 | } |
| 4259 | |
| 4260 | static struct md_sysfs_entry md_min_sync = |
| 4261 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4262 | |
| 4263 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4264 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4265 | { |
| 4266 | if (mddev->resync_max == MaxSector) |
| 4267 | return sprintf(page, "max\n"); |
| 4268 | else |
| 4269 | return sprintf(page, "%llu\n", |
| 4270 | (unsigned long long)mddev->resync_max); |
| 4271 | } |
| 4272 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4273 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4274 | { |
| 4275 | if (strncmp(buf, "max", 3) == 0) |
| 4276 | mddev->resync_max = MaxSector; |
| 4277 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4278 | unsigned long long max; |
| 4279 | if (strict_strtoull(buf, 10, &max)) |
| 4280 | return -EINVAL; |
| 4281 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4282 | return -EINVAL; |
| 4283 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4284 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4285 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4286 | return -EBUSY; |
| 4287 | |
| 4288 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4289 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4290 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4291 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4292 | return -EINVAL; |
| 4293 | } |
| 4294 | mddev->resync_max = max; |
| 4295 | } |
| 4296 | wake_up(&mddev->recovery_wait); |
| 4297 | return len; |
| 4298 | } |
| 4299 | |
| 4300 | static struct md_sysfs_entry md_max_sync = |
| 4301 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4302 | |
| 4303 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4304 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4305 | { |
| 4306 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4307 | } |
| 4308 | |
| 4309 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4310 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4311 | { |
| 4312 | char *e; |
| 4313 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4314 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4315 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4316 | if (mddev->pers == NULL || |
| 4317 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4318 | return -EINVAL; |
| 4319 | if (buf == e || (*e && *e != '\n')) |
| 4320 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4321 | |
| 4322 | mddev->suspend_lo = new; |
| 4323 | if (new >= old) |
| 4324 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4325 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4326 | else { |
| 4327 | /* Expanding suspended region - need to wait */ |
| 4328 | mddev->pers->quiesce(mddev, 1); |
| 4329 | mddev->pers->quiesce(mddev, 0); |
| 4330 | } |
| 4331 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4332 | } |
| 4333 | static struct md_sysfs_entry md_suspend_lo = |
| 4334 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4335 | |
| 4336 | |
| 4337 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4338 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4339 | { |
| 4340 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4341 | } |
| 4342 | |
| 4343 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4344 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4345 | { |
| 4346 | char *e; |
| 4347 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4348 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4349 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4350 | if (mddev->pers == NULL || |
| 4351 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4352 | return -EINVAL; |
| 4353 | if (buf == e || (*e && *e != '\n')) |
| 4354 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4355 | |
| 4356 | mddev->suspend_hi = new; |
| 4357 | if (new <= old) |
| 4358 | /* Shrinking suspended region */ |
| 4359 | mddev->pers->quiesce(mddev, 2); |
| 4360 | else { |
| 4361 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4362 | mddev->pers->quiesce(mddev, 1); |
| 4363 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4364 | } |
| 4365 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4366 | } |
| 4367 | static struct md_sysfs_entry md_suspend_hi = |
| 4368 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4369 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4370 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4371 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4372 | { |
| 4373 | if (mddev->reshape_position != MaxSector) |
| 4374 | return sprintf(page, "%llu\n", |
| 4375 | (unsigned long long)mddev->reshape_position); |
| 4376 | strcpy(page, "none\n"); |
| 4377 | return 5; |
| 4378 | } |
| 4379 | |
| 4380 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4381 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4382 | { |
| 4383 | char *e; |
| 4384 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4385 | if (mddev->pers) |
| 4386 | return -EBUSY; |
| 4387 | if (buf == e || (*e && *e != '\n')) |
| 4388 | return -EINVAL; |
| 4389 | mddev->reshape_position = new; |
| 4390 | mddev->delta_disks = 0; |
| 4391 | mddev->new_level = mddev->level; |
| 4392 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4393 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4394 | return len; |
| 4395 | } |
| 4396 | |
| 4397 | static struct md_sysfs_entry md_reshape_position = |
| 4398 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4399 | reshape_position_store); |
| 4400 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4401 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4402 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4403 | { |
| 4404 | if (mddev->external_size) |
| 4405 | return sprintf(page, "%llu\n", |
| 4406 | (unsigned long long)mddev->array_sectors/2); |
| 4407 | else |
| 4408 | return sprintf(page, "default\n"); |
| 4409 | } |
| 4410 | |
| 4411 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4412 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4413 | { |
| 4414 | sector_t sectors; |
| 4415 | |
| 4416 | if (strncmp(buf, "default", 7) == 0) { |
| 4417 | if (mddev->pers) |
| 4418 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4419 | else |
| 4420 | sectors = mddev->array_sectors; |
| 4421 | |
| 4422 | mddev->external_size = 0; |
| 4423 | } else { |
| 4424 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4425 | return -EINVAL; |
| 4426 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4427 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4428 | |
| 4429 | mddev->external_size = 1; |
| 4430 | } |
| 4431 | |
| 4432 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4433 | if (mddev->pers) { |
| 4434 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4435 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4436 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4437 | return len; |
| 4438 | } |
| 4439 | |
| 4440 | static struct md_sysfs_entry md_array_size = |
| 4441 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4442 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4443 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4444 | static struct attribute *md_default_attrs[] = { |
| 4445 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4446 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4447 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4448 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4449 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4450 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4451 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4452 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4453 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4454 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4455 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4456 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4457 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4458 | NULL, |
| 4459 | }; |
| 4460 | |
| 4461 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4462 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4463 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4464 | &md_sync_min.attr, |
| 4465 | &md_sync_max.attr, |
| 4466 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4467 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4468 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4469 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4470 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4471 | &md_suspend_lo.attr, |
| 4472 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4473 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4474 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4475 | NULL, |
| 4476 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4477 | static struct attribute_group md_redundancy_group = { |
| 4478 | .name = NULL, |
| 4479 | .attrs = md_redundancy_attrs, |
| 4480 | }; |
| 4481 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4482 | |
| 4483 | static ssize_t |
| 4484 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4485 | { |
| 4486 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4488 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4489 | |
| 4490 | if (!entry->show) |
| 4491 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4492 | rv = mddev_lock(mddev); |
| 4493 | if (!rv) { |
| 4494 | rv = entry->show(mddev, page); |
| 4495 | mddev_unlock(mddev); |
| 4496 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4497 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | static ssize_t |
| 4501 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4502 | const char *page, size_t length) |
| 4503 | { |
| 4504 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4505 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4506 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4507 | |
| 4508 | if (!entry->store) |
| 4509 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4510 | if (!capable(CAP_SYS_ADMIN)) |
| 4511 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4512 | rv = mddev_lock(mddev); |
| 4513 | if (!rv) { |
| 4514 | rv = entry->store(mddev, page, length); |
| 4515 | mddev_unlock(mddev); |
| 4516 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4517 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static void md_free(struct kobject *ko) |
| 4521 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4522 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4523 | |
| 4524 | if (mddev->sysfs_state) |
| 4525 | sysfs_put(mddev->sysfs_state); |
| 4526 | |
| 4527 | if (mddev->gendisk) { |
| 4528 | del_gendisk(mddev->gendisk); |
| 4529 | put_disk(mddev->gendisk); |
| 4530 | } |
| 4531 | if (mddev->queue) |
| 4532 | blk_cleanup_queue(mddev->queue); |
| 4533 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4534 | kfree(mddev); |
| 4535 | } |
| 4536 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4537 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4538 | .show = md_attr_show, |
| 4539 | .store = md_attr_store, |
| 4540 | }; |
| 4541 | static struct kobj_type md_ktype = { |
| 4542 | .release = md_free, |
| 4543 | .sysfs_ops = &md_sysfs_ops, |
| 4544 | .default_attrs = md_default_attrs, |
| 4545 | }; |
| 4546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | int mdp_major = 0; |
| 4548 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4549 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4550 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4551 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4552 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4553 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4554 | kobject_del(&mddev->kobj); |
| 4555 | kobject_put(&mddev->kobj); |
| 4556 | } |
| 4557 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4558 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4560 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4561 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4563 | int partitioned; |
| 4564 | int shift; |
| 4565 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4566 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
| 4568 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4569 | return -ENODEV; |
| 4570 | |
| 4571 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4572 | shift = partitioned ? MdpMinorShift : 0; |
| 4573 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4575 | /* wait for any previous instance of this device to be |
| 4576 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4577 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4578 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4579 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4580 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4581 | error = -EEXIST; |
| 4582 | if (mddev->gendisk) |
| 4583 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4584 | |
| 4585 | if (name) { |
| 4586 | /* Need to ensure that 'name' is not a duplicate. |
| 4587 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4588 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4589 | spin_lock(&all_mddevs_lock); |
| 4590 | |
| 4591 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4592 | if (mddev2->gendisk && |
| 4593 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4594 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4595 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4596 | } |
| 4597 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4599 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4600 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4601 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4602 | if (!mddev->queue) |
| 4603 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4604 | mddev->queue->queuedata = mddev; |
| 4605 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4606 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | disk = alloc_disk(1 << shift); |
| 4609 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4610 | blk_cleanup_queue(mddev->queue); |
| 4611 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4612 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4614 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4616 | if (name) |
| 4617 | strcpy(disk->disk_name, name); |
| 4618 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4620 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | disk->fops = &md_fops; |
| 4623 | disk->private_data = mddev; |
| 4624 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4625 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4626 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4627 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4628 | * remove it now. |
| 4629 | */ |
| 4630 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4632 | /* As soon as we call add_disk(), another thread could get |
| 4633 | * through to md_open, so make sure it doesn't get too far |
| 4634 | */ |
| 4635 | mutex_lock(&mddev->open_mutex); |
| 4636 | add_disk(disk); |
| 4637 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4638 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4639 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4640 | if (error) { |
| 4641 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4642 | * __must_check, we must do something with the result |
| 4643 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4644 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4645 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4646 | error = 0; |
| 4647 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4648 | if (mddev->kobj.sd && |
| 4649 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4650 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4651 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4652 | abort: |
| 4653 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4654 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4655 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4656 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4657 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4658 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4659 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4663 | { |
| 4664 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | return NULL; |
| 4666 | } |
| 4667 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4668 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4669 | { |
| 4670 | /* val must be "md_*" where * is not all digits. |
| 4671 | * We allocate an array with a large free minor number, and |
| 4672 | * set the name to val. val must not already be an active name. |
| 4673 | */ |
| 4674 | int len = strlen(val); |
| 4675 | char buf[DISK_NAME_LEN]; |
| 4676 | |
| 4677 | while (len && val[len-1] == '\n') |
| 4678 | len--; |
| 4679 | if (len >= DISK_NAME_LEN) |
| 4680 | return -E2BIG; |
| 4681 | strlcpy(buf, val, len+1); |
| 4682 | if (strncmp(buf, "md_", 3) != 0) |
| 4683 | return -EINVAL; |
| 4684 | return md_alloc(0, buf); |
| 4685 | } |
| 4686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | static void md_safemode_timeout(unsigned long data) |
| 4688 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4689 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4691 | if (!atomic_read(&mddev->writes_pending)) { |
| 4692 | mddev->safemode = 1; |
| 4693 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4694 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4695 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | md_wakeup_thread(mddev->thread); |
| 4697 | } |
| 4698 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4699 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4701 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4703 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4704 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4705 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4707 | if (list_empty(&mddev->disks)) |
| 4708 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | |
| 4711 | if (mddev->pers) |
| 4712 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4713 | /* Cannot run until previous stop completes properly */ |
| 4714 | if (mddev->sysfs_active) |
| 4715 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | /* |
| 4718 | * Analyze all RAID superblock(s) |
| 4719 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4720 | if (!mddev->raid_disks) { |
| 4721 | if (!mddev->persistent) |
| 4722 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4723 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4726 | if (mddev->level != LEVEL_NONE) |
| 4727 | request_module("md-level-%d", mddev->level); |
| 4728 | else if (mddev->clevel[0]) |
| 4729 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | |
| 4731 | /* |
| 4732 | * Drop all container device buffers, from now on |
| 4733 | * the only valid external interface is through the md |
| 4734 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4736 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4737 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | continue; |
| 4739 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4740 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4741 | |
| 4742 | /* perform some consistency tests on the device. |
| 4743 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4744 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4745 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4746 | if (rdev->meta_bdev) { |
| 4747 | /* Nothing to check */; |
| 4748 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4749 | if (mddev->dev_sectors && |
| 4750 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4751 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4752 | printk("md: %s: data overlaps metadata\n", |
| 4753 | mdname(mddev)); |
| 4754 | return -EINVAL; |
| 4755 | } |
| 4756 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4757 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4758 | > rdev->data_offset) { |
| 4759 | printk("md: %s: metadata overlaps data\n", |
| 4760 | mdname(mddev)); |
| 4761 | return -EINVAL; |
| 4762 | } |
| 4763 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4764 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | } |
| 4766 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4767 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4768 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4769 | sizeof(struct mddev *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4772 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4773 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4775 | if (mddev->level != LEVEL_NONE) |
| 4776 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4777 | mddev->level); |
| 4778 | else |
| 4779 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4780 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | return -EINVAL; |
| 4782 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4783 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4785 | if (mddev->level != pers->level) { |
| 4786 | mddev->level = pers->level; |
| 4787 | mddev->new_level = pers->level; |
| 4788 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4789 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4791 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4792 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4793 | /* This personality cannot handle reshaping... */ |
| 4794 | mddev->pers = NULL; |
| 4795 | module_put(pers->owner); |
| 4796 | return -EINVAL; |
| 4797 | } |
| 4798 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4799 | if (pers->sync_request) { |
| 4800 | /* Warn if this is a potentially silly |
| 4801 | * configuration. |
| 4802 | */ |
| 4803 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4804 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4805 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4806 | |
| 4807 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4808 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4809 | if (rdev < rdev2 && |
| 4810 | rdev->bdev->bd_contains == |
| 4811 | rdev2->bdev->bd_contains) { |
| 4812 | printk(KERN_WARNING |
| 4813 | "%s: WARNING: %s appears to be" |
| 4814 | " on the same physical disk as" |
| 4815 | " %s.\n", |
| 4816 | mdname(mddev), |
| 4817 | bdevname(rdev->bdev,b), |
| 4818 | bdevname(rdev2->bdev,b2)); |
| 4819 | warned = 1; |
| 4820 | } |
| 4821 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4822 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4823 | if (warned) |
| 4824 | printk(KERN_WARNING |
| 4825 | "True protection against single-disk" |
| 4826 | " failure might be compromised.\n"); |
| 4827 | } |
| 4828 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4829 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4830 | /* may be over-ridden by personality */ |
| 4831 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4832 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4833 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4835 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4836 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4837 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4838 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4839 | if (err) |
| 4840 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4841 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4842 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4843 | " but 'external_size' not in effect?\n", __func__); |
| 4844 | printk(KERN_ERR |
| 4845 | "md: invalid array_size %llu > default size %llu\n", |
| 4846 | (unsigned long long)mddev->array_sectors / 2, |
| 4847 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4848 | err = -EINVAL; |
| 4849 | mddev->pers->stop(mddev); |
| 4850 | } |
| 4851 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4852 | err = bitmap_create(mddev); |
| 4853 | if (err) { |
| 4854 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4855 | mdname(mddev), err); |
| 4856 | mddev->pers->stop(mddev); |
| 4857 | } |
| 4858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | module_put(mddev->pers->owner); |
| 4861 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4862 | bitmap_destroy(mddev); |
| 4863 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4865 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4866 | if (mddev->kobj.sd && |
| 4867 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4868 | printk(KERN_WARNING |
| 4869 | "md: cannot register extra attributes for %s\n", |
| 4870 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4871 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4872 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4873 | mddev->ro = 0; |
| 4874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4876 | atomic_set(&mddev->max_corr_read_errors, |
| 4877 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | mddev->safemode = 0; |
| 4879 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4880 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4881 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4883 | smp_wmb(); |
| 4884 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4885 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4886 | if (rdev->raid_disk >= 0) |
| 4887 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4888 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | |
| 4890 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4891 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4892 | if (mddev->flags) |
| 4893 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4895 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4896 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4897 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4898 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | return 0; |
| 4900 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4901 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4903 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4904 | { |
| 4905 | int err; |
| 4906 | |
| 4907 | err = md_run(mddev); |
| 4908 | if (err) |
| 4909 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4910 | err = bitmap_load(mddev); |
| 4911 | if (err) { |
| 4912 | bitmap_destroy(mddev); |
| 4913 | goto out; |
| 4914 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4915 | |
| 4916 | md_wakeup_thread(mddev->thread); |
| 4917 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4918 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4919 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4920 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4921 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4922 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4923 | out: |
| 4924 | return err; |
| 4925 | } |
| 4926 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4927 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | { |
| 4929 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4931 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4933 | return -ENXIO; |
| 4934 | if (!mddev->pers) |
| 4935 | return -EINVAL; |
| 4936 | if (!mddev->ro) |
| 4937 | return -EBUSY; |
| 4938 | mddev->safemode = 0; |
| 4939 | mddev->ro = 0; |
| 4940 | set_disk_ro(disk, 0); |
| 4941 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4942 | mdname(mddev)); |
| 4943 | /* Kick recovery or resync if necessary */ |
| 4944 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4945 | md_wakeup_thread(mddev->thread); |
| 4946 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4947 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4948 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4951 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4952 | * to the file ourselves */ |
| 4953 | static int deny_bitmap_write_access(struct file * file) |
| 4954 | { |
| 4955 | struct inode *inode = file->f_mapping->host; |
| 4956 | |
| 4957 | spin_lock(&inode->i_lock); |
| 4958 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4959 | spin_unlock(&inode->i_lock); |
| 4960 | return -ETXTBSY; |
| 4961 | } |
| 4962 | atomic_set(&inode->i_writecount, -1); |
| 4963 | spin_unlock(&inode->i_lock); |
| 4964 | |
| 4965 | return 0; |
| 4966 | } |
| 4967 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4968 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4969 | { |
| 4970 | struct inode *inode = file->f_mapping->host; |
| 4971 | |
| 4972 | spin_lock(&inode->i_lock); |
| 4973 | atomic_set(&inode->i_writecount, 1); |
| 4974 | spin_unlock(&inode->i_lock); |
| 4975 | } |
| 4976 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4977 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4978 | { |
| 4979 | mddev->array_sectors = 0; |
| 4980 | mddev->external_size = 0; |
| 4981 | mddev->dev_sectors = 0; |
| 4982 | mddev->raid_disks = 0; |
| 4983 | mddev->recovery_cp = 0; |
| 4984 | mddev->resync_min = 0; |
| 4985 | mddev->resync_max = MaxSector; |
| 4986 | mddev->reshape_position = MaxSector; |
| 4987 | mddev->external = 0; |
| 4988 | mddev->persistent = 0; |
| 4989 | mddev->level = LEVEL_NONE; |
| 4990 | mddev->clevel[0] = 0; |
| 4991 | mddev->flags = 0; |
| 4992 | mddev->ro = 0; |
| 4993 | mddev->metadata_type[0] = 0; |
| 4994 | mddev->chunk_sectors = 0; |
| 4995 | mddev->ctime = mddev->utime = 0; |
| 4996 | mddev->layout = 0; |
| 4997 | mddev->max_disks = 0; |
| 4998 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4999 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5000 | mddev->delta_disks = 0; |
| 5001 | mddev->new_level = LEVEL_NONE; |
| 5002 | mddev->new_layout = 0; |
| 5003 | mddev->new_chunk_sectors = 0; |
| 5004 | mddev->curr_resync = 0; |
| 5005 | mddev->resync_mismatches = 0; |
| 5006 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5007 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5008 | mddev->recovery = 0; |
| 5009 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5010 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5011 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5012 | mddev->safemode = 0; |
| 5013 | mddev->bitmap_info.offset = 0; |
| 5014 | mddev->bitmap_info.default_offset = 0; |
| 5015 | mddev->bitmap_info.chunksize = 0; |
| 5016 | mddev->bitmap_info.daemon_sleep = 0; |
| 5017 | mddev->bitmap_info.max_write_behind = 0; |
| 5018 | } |
| 5019 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5020 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5021 | { |
| 5022 | if (mddev->sync_thread) { |
| 5023 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5024 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5025 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | del_timer_sync(&mddev->safemode_timer); |
| 5029 | |
| 5030 | bitmap_flush(mddev); |
| 5031 | md_super_wait(mddev); |
| 5032 | |
| 5033 | if (!mddev->in_sync || mddev->flags) { |
| 5034 | /* mark array as shutdown cleanly */ |
| 5035 | mddev->in_sync = 1; |
| 5036 | md_update_sb(mddev, 1); |
| 5037 | } |
| 5038 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5039 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5040 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5041 | { |
| 5042 | mddev_lock(mddev); |
| 5043 | __md_stop_writes(mddev); |
| 5044 | mddev_unlock(mddev); |
| 5045 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5046 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5047 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5048 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5049 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5050 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5051 | mddev->pers->stop(mddev); |
| 5052 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5053 | mddev->to_remove = &md_redundancy_group; |
| 5054 | module_put(mddev->pers->owner); |
| 5055 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5056 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5057 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5058 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5059 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5060 | static int md_set_readonly(struct mddev *mddev, int is_open) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5061 | { |
| 5062 | int err = 0; |
| 5063 | mutex_lock(&mddev->open_mutex); |
| 5064 | if (atomic_read(&mddev->openers) > is_open) { |
| 5065 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5066 | err = -EBUSY; |
| 5067 | goto out; |
| 5068 | } |
| 5069 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5070 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5071 | |
| 5072 | err = -ENXIO; |
| 5073 | if (mddev->ro==1) |
| 5074 | goto out; |
| 5075 | mddev->ro = 1; |
| 5076 | set_disk_ro(mddev->gendisk, 1); |
| 5077 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5078 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5079 | err = 0; |
| 5080 | } |
| 5081 | out: |
| 5082 | mutex_unlock(&mddev->open_mutex); |
| 5083 | return err; |
| 5084 | } |
| 5085 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5086 | /* mode: |
| 5087 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5088 | * 2 - stop but do not disassemble array |
| 5089 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5090 | 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] | 5091 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5093 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5095 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5096 | if (atomic_read(&mddev->openers) > is_open || |
| 5097 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5098 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5099 | mutex_unlock(&mddev->open_mutex); |
| 5100 | return -EBUSY; |
| 5101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5103 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5104 | if (mddev->ro) |
| 5105 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5106 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5107 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5108 | md_stop(mddev); |
| 5109 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5110 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5111 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5112 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5113 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5114 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5115 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5116 | if (rdev->raid_disk >= 0) |
| 5117 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5118 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5119 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5120 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5121 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5122 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5123 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5124 | if (mddev->ro) |
| 5125 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5126 | } else |
| 5127 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | /* |
| 5129 | * Free resources if final stop |
| 5130 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5131 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5133 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5134 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5135 | if (mddev->bitmap_info.file) { |
| 5136 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5137 | fput(mddev->bitmap_info.file); |
| 5138 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5139 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5140 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | export_array(mddev); |
| 5143 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5144 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5145 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5146 | if (mddev->hold_active == UNTIL_STOP) |
| 5147 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5148 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5149 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5150 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5151 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5152 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5155 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5156 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5158 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | int err; |
| 5160 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5161 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | |
| 5164 | printk(KERN_INFO "md: running: "); |
| 5165 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5166 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | char b[BDEVNAME_SIZE]; |
| 5168 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5169 | } |
| 5170 | printk("\n"); |
| 5171 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5172 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | if (err) { |
| 5174 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5175 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | |
| 5179 | /* |
| 5180 | * lets try to run arrays based on all disks that have arrived |
| 5181 | * until now. (those are in pending_raid_disks) |
| 5182 | * |
| 5183 | * the method: pick the first pending disk, collect all disks with |
| 5184 | * the same UUID, remove all from the pending list and put them into |
| 5185 | * the 'same_array' list. Then order this list based on superblock |
| 5186 | * update time (freshest comes first), kick out 'old' disks and |
| 5187 | * compare superblocks. If everything's fine then run it. |
| 5188 | * |
| 5189 | * If "unit" is allocated, then bump its reference count |
| 5190 | */ |
| 5191 | static void autorun_devices(int part) |
| 5192 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5193 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5194 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | char b[BDEVNAME_SIZE]; |
| 5196 | |
| 5197 | printk(KERN_INFO "md: autorun ...\n"); |
| 5198 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5199 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5201 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5203 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | |
| 5205 | printk(KERN_INFO "md: considering %s ...\n", |
| 5206 | bdevname(rdev0->bdev,b)); |
| 5207 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5208 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5210 | printk(KERN_INFO "md: adding %s ...\n", |
| 5211 | bdevname(rdev->bdev,b)); |
| 5212 | list_move(&rdev->same_set, &candidates); |
| 5213 | } |
| 5214 | /* |
| 5215 | * now we have a set of devices, with all of them having |
| 5216 | * mostly sane superblocks. It's time to allocate the |
| 5217 | * mddev. |
| 5218 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5219 | if (part) { |
| 5220 | dev = MKDEV(mdp_major, |
| 5221 | rdev0->preferred_minor << MdpMinorShift); |
| 5222 | unit = MINOR(dev) >> MdpMinorShift; |
| 5223 | } else { |
| 5224 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5225 | unit = MINOR(dev); |
| 5226 | } |
| 5227 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5229 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5230 | break; |
| 5231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | |
| 5233 | md_probe(dev, NULL, NULL); |
| 5234 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5235 | if (!mddev || !mddev->gendisk) { |
| 5236 | if (mddev) |
| 5237 | mddev_put(mddev); |
| 5238 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | "md: cannot allocate memory for md drive.\n"); |
| 5240 | break; |
| 5241 | } |
| 5242 | if (mddev_lock(mddev)) |
| 5243 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5244 | mdname(mddev)); |
| 5245 | else if (mddev->raid_disks || mddev->major_version |
| 5246 | || !list_empty(&mddev->disks)) { |
| 5247 | printk(KERN_WARNING |
| 5248 | "md: %s already running, cannot run %s\n", |
| 5249 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5250 | mddev_unlock(mddev); |
| 5251 | } else { |
| 5252 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5253 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5254 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | list_del_init(&rdev->same_set); |
| 5256 | if (bind_rdev_to_array(rdev, mddev)) |
| 5257 | export_rdev(rdev); |
| 5258 | } |
| 5259 | autorun_array(mddev); |
| 5260 | mddev_unlock(mddev); |
| 5261 | } |
| 5262 | /* on success, candidates will be empty, on error |
| 5263 | * it won't... |
| 5264 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5265 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5266 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | mddev_put(mddev); |
| 5270 | } |
| 5271 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5272 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5273 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | static int get_version(void __user * arg) |
| 5276 | { |
| 5277 | mdu_version_t ver; |
| 5278 | |
| 5279 | ver.major = MD_MAJOR_VERSION; |
| 5280 | ver.minor = MD_MINOR_VERSION; |
| 5281 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5282 | |
| 5283 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5284 | return -EFAULT; |
| 5285 | |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5289 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | { |
| 5291 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5292 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5293 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5295 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5296 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5298 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | failed++; |
| 5300 | else { |
| 5301 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5302 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5303 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | else |
| 5305 | spare++; |
| 5306 | } |
| 5307 | } |
| 5308 | |
| 5309 | info.major_version = mddev->major_version; |
| 5310 | info.minor_version = mddev->minor_version; |
| 5311 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5312 | info.ctime = mddev->ctime; |
| 5313 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5314 | info.size = mddev->dev_sectors / 2; |
| 5315 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5316 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | info.nr_disks = nr; |
| 5318 | info.raid_disks = mddev->raid_disks; |
| 5319 | info.md_minor = mddev->md_minor; |
| 5320 | info.not_persistent= !mddev->persistent; |
| 5321 | |
| 5322 | info.utime = mddev->utime; |
| 5323 | info.state = 0; |
| 5324 | if (mddev->in_sync) |
| 5325 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5326 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5327 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5328 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | info.working_disks = working; |
| 5330 | info.failed_disks = failed; |
| 5331 | info.spare_disks = spare; |
| 5332 | |
| 5333 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5334 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | |
| 5336 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5337 | return -EFAULT; |
| 5338 | |
| 5339 | return 0; |
| 5340 | } |
| 5341 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5342 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5343 | { |
| 5344 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5345 | char *ptr, *buf = NULL; |
| 5346 | int err = -ENOMEM; |
| 5347 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5348 | if (md_allow_write(mddev)) |
| 5349 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5350 | else |
| 5351 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5352 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5353 | if (!file) |
| 5354 | goto out; |
| 5355 | |
| 5356 | /* bitmap disabled, zero the first byte and copy out */ |
| 5357 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5358 | file->pathname[0] = '\0'; |
| 5359 | goto copy_out; |
| 5360 | } |
| 5361 | |
| 5362 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5363 | if (!buf) |
| 5364 | goto out; |
| 5365 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5366 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5367 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5368 | goto out; |
| 5369 | |
| 5370 | strcpy(file->pathname, ptr); |
| 5371 | |
| 5372 | copy_out: |
| 5373 | err = 0; |
| 5374 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5375 | err = -EFAULT; |
| 5376 | out: |
| 5377 | kfree(buf); |
| 5378 | kfree(file); |
| 5379 | return err; |
| 5380 | } |
| 5381 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5382 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | { |
| 5384 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5385 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | |
| 5387 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5388 | return -EFAULT; |
| 5389 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5390 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | if (rdev) { |
| 5392 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5393 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5394 | info.raid_disk = rdev->raid_disk; |
| 5395 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5396 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5398 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5400 | info.state |= (1<<MD_DISK_SYNC); |
| 5401 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5402 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5403 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | } else { |
| 5405 | info.major = info.minor = 0; |
| 5406 | info.raid_disk = -1; |
| 5407 | info.state = (1<<MD_DISK_REMOVED); |
| 5408 | } |
| 5409 | |
| 5410 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5411 | return -EFAULT; |
| 5412 | |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5416 | 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] | 5417 | { |
| 5418 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5419 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | dev_t dev = MKDEV(info->major,info->minor); |
| 5421 | |
| 5422 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5423 | return -EOVERFLOW; |
| 5424 | |
| 5425 | if (!mddev->raid_disks) { |
| 5426 | int err; |
| 5427 | /* expecting a device which has a superblock */ |
| 5428 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5429 | if (IS_ERR(rdev)) { |
| 5430 | printk(KERN_WARNING |
| 5431 | "md: md_import_device returned %ld\n", |
| 5432 | PTR_ERR(rdev)); |
| 5433 | return PTR_ERR(rdev); |
| 5434 | } |
| 5435 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5436 | struct md_rdev *rdev0 |
| 5437 | = list_entry(mddev->disks.next, |
| 5438 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5439 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5441 | if (err < 0) { |
| 5442 | printk(KERN_WARNING |
| 5443 | "md: %s has different UUID to %s\n", |
| 5444 | bdevname(rdev->bdev,b), |
| 5445 | bdevname(rdev0->bdev,b2)); |
| 5446 | export_rdev(rdev); |
| 5447 | return -EINVAL; |
| 5448 | } |
| 5449 | } |
| 5450 | err = bind_rdev_to_array(rdev, mddev); |
| 5451 | if (err) |
| 5452 | export_rdev(rdev); |
| 5453 | return err; |
| 5454 | } |
| 5455 | |
| 5456 | /* |
| 5457 | * add_new_disk can be used once the array is assembled |
| 5458 | * to add "hot spares". They must already have a superblock |
| 5459 | * written |
| 5460 | */ |
| 5461 | if (mddev->pers) { |
| 5462 | int err; |
| 5463 | if (!mddev->pers->hot_add_disk) { |
| 5464 | printk(KERN_WARNING |
| 5465 | "%s: personality does not support diskops!\n", |
| 5466 | mdname(mddev)); |
| 5467 | return -EINVAL; |
| 5468 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5469 | if (mddev->persistent) |
| 5470 | rdev = md_import_device(dev, mddev->major_version, |
| 5471 | mddev->minor_version); |
| 5472 | else |
| 5473 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | if (IS_ERR(rdev)) { |
| 5475 | printk(KERN_WARNING |
| 5476 | "md: md_import_device returned %ld\n", |
| 5477 | PTR_ERR(rdev)); |
| 5478 | return PTR_ERR(rdev); |
| 5479 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5480 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5481 | if (!mddev->persistent) { |
| 5482 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5483 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5484 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5485 | set_bit(In_sync, &rdev->flags); |
| 5486 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5487 | rdev->raid_disk = -1; |
| 5488 | } else |
| 5489 | super_types[mddev->major_version]. |
| 5490 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5491 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5492 | (!test_bit(In_sync, &rdev->flags) || |
| 5493 | rdev->raid_disk != info->raid_disk)) { |
| 5494 | /* This was a hot-add request, but events doesn't |
| 5495 | * match, so reject it. |
| 5496 | */ |
| 5497 | export_rdev(rdev); |
| 5498 | return -EINVAL; |
| 5499 | } |
| 5500 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5501 | if (test_bit(In_sync, &rdev->flags)) |
| 5502 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5503 | else |
| 5504 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5505 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5506 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5507 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5508 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5509 | else |
| 5510 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | rdev->raid_disk = -1; |
| 5513 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5514 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5515 | /* If there is hot_add_disk but no hot_remove_disk |
| 5516 | * then added disks for geometry changes, |
| 5517 | * and should be added immediately. |
| 5518 | */ |
| 5519 | super_types[mddev->major_version]. |
| 5520 | validate_super(mddev, rdev); |
| 5521 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5522 | if (err) |
| 5523 | unbind_rdev_from_array(rdev); |
| 5524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | if (err) |
| 5526 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5527 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5528 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5529 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5530 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5531 | if (mddev->degraded) |
| 5532 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5533 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5534 | if (!err) |
| 5535 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5536 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | return err; |
| 5538 | } |
| 5539 | |
| 5540 | /* otherwise, add_new_disk is only allowed |
| 5541 | * for major_version==0 superblocks |
| 5542 | */ |
| 5543 | if (mddev->major_version != 0) { |
| 5544 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5545 | mdname(mddev)); |
| 5546 | return -EINVAL; |
| 5547 | } |
| 5548 | |
| 5549 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5550 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5551 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | if (IS_ERR(rdev)) { |
| 5553 | printk(KERN_WARNING |
| 5554 | "md: error, md_import_device() returned %ld\n", |
| 5555 | PTR_ERR(rdev)); |
| 5556 | return PTR_ERR(rdev); |
| 5557 | } |
| 5558 | rdev->desc_nr = info->number; |
| 5559 | if (info->raid_disk < mddev->raid_disks) |
| 5560 | rdev->raid_disk = info->raid_disk; |
| 5561 | else |
| 5562 | rdev->raid_disk = -1; |
| 5563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5565 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5566 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5568 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5569 | set_bit(WriteMostly, &rdev->flags); |
| 5570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | if (!mddev->persistent) { |
| 5572 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5573 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5574 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5575 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5576 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5578 | err = bind_rdev_to_array(rdev, mddev); |
| 5579 | if (err) { |
| 5580 | export_rdev(rdev); |
| 5581 | return err; |
| 5582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | return 0; |
| 5586 | } |
| 5587 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5588 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | { |
| 5590 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5591 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | rdev = find_rdev(mddev, dev); |
| 5594 | if (!rdev) |
| 5595 | return -ENXIO; |
| 5596 | |
| 5597 | if (rdev->raid_disk >= 0) |
| 5598 | goto busy; |
| 5599 | |
| 5600 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5601 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5602 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | |
| 5604 | return 0; |
| 5605 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5606 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5608 | return -EBUSY; |
| 5609 | } |
| 5610 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5611 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | { |
| 5613 | char b[BDEVNAME_SIZE]; |
| 5614 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5615 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | |
| 5617 | if (!mddev->pers) |
| 5618 | return -ENODEV; |
| 5619 | |
| 5620 | if (mddev->major_version != 0) { |
| 5621 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5622 | " version-0 superblocks.\n", |
| 5623 | mdname(mddev)); |
| 5624 | return -EINVAL; |
| 5625 | } |
| 5626 | if (!mddev->pers->hot_add_disk) { |
| 5627 | printk(KERN_WARNING |
| 5628 | "%s: personality does not support diskops!\n", |
| 5629 | mdname(mddev)); |
| 5630 | return -EINVAL; |
| 5631 | } |
| 5632 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5633 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | if (IS_ERR(rdev)) { |
| 5635 | printk(KERN_WARNING |
| 5636 | "md: error, md_import_device() returned %ld\n", |
| 5637 | PTR_ERR(rdev)); |
| 5638 | return -EINVAL; |
| 5639 | } |
| 5640 | |
| 5641 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5642 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5644 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5646 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5648 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | printk(KERN_WARNING |
| 5650 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5651 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5652 | err = -EINVAL; |
| 5653 | goto abort_export; |
| 5654 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5655 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5657 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5658 | err = bind_rdev_to_array(rdev, mddev); |
| 5659 | if (err) |
| 5660 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | |
| 5662 | /* |
| 5663 | * The rest should better be atomic, we can have disk failures |
| 5664 | * noticed in interrupt contexts ... |
| 5665 | */ |
| 5666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | rdev->raid_disk = -1; |
| 5668 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5669 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
| 5671 | /* |
| 5672 | * Kick recovery, maybe this spare has to be added to the |
| 5673 | * array immediately. |
| 5674 | */ |
| 5675 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5676 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5677 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | return 0; |
| 5679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | abort_export: |
| 5681 | export_rdev(rdev); |
| 5682 | return err; |
| 5683 | } |
| 5684 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5685 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5686 | { |
| 5687 | int err; |
| 5688 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5689 | if (mddev->pers) { |
| 5690 | if (!mddev->pers->quiesce) |
| 5691 | return -EBUSY; |
| 5692 | if (mddev->recovery || mddev->sync_thread) |
| 5693 | return -EBUSY; |
| 5694 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5695 | } |
| 5696 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5697 | |
| 5698 | if (fd >= 0) { |
| 5699 | if (mddev->bitmap) |
| 5700 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5701 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5702 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5703 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5704 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5705 | mdname(mddev)); |
| 5706 | return -EBADF; |
| 5707 | } |
| 5708 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5709 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5710 | if (err) { |
| 5711 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5712 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5713 | fput(mddev->bitmap_info.file); |
| 5714 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5715 | return err; |
| 5716 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5717 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5718 | } else if (mddev->bitmap == NULL) |
| 5719 | return -ENOENT; /* cannot remove what isn't there */ |
| 5720 | err = 0; |
| 5721 | if (mddev->pers) { |
| 5722 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5723 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5724 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5725 | if (!err) |
| 5726 | err = bitmap_load(mddev); |
| 5727 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5728 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5729 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5730 | fd = -1; /* make sure to put the file */ |
| 5731 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5732 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5733 | } |
| 5734 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5735 | if (mddev->bitmap_info.file) { |
| 5736 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5737 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5738 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5739 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5740 | } |
| 5741 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5742 | return err; |
| 5743 | } |
| 5744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | /* |
| 5746 | * set_array_info is used two different ways |
| 5747 | * The original usage is when creating a new array. |
| 5748 | * In this usage, raid_disks is > 0 and it together with |
| 5749 | * level, size, not_persistent,layout,chunksize determine the |
| 5750 | * shape of the array. |
| 5751 | * This will always create an array with a type-0.90.0 superblock. |
| 5752 | * The newer usage is when assembling an array. |
| 5753 | * In this case raid_disks will be 0, and the major_version field is |
| 5754 | * use to determine which style super-blocks are to be found on the devices. |
| 5755 | * The minor and patch _version numbers are also kept incase the |
| 5756 | * super_block handler wishes to interpret them. |
| 5757 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5758 | 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] | 5759 | { |
| 5760 | |
| 5761 | if (info->raid_disks == 0) { |
| 5762 | /* just setting version number for superblock loading */ |
| 5763 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5764 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | super_types[info->major_version].name == NULL) { |
| 5766 | /* maybe try to auto-load a module? */ |
| 5767 | printk(KERN_INFO |
| 5768 | "md: superblock version %d not known\n", |
| 5769 | info->major_version); |
| 5770 | return -EINVAL; |
| 5771 | } |
| 5772 | mddev->major_version = info->major_version; |
| 5773 | mddev->minor_version = info->minor_version; |
| 5774 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5775 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5776 | /* ensure mddev_put doesn't delete this now that there |
| 5777 | * is some minimal configuration. |
| 5778 | */ |
| 5779 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | return 0; |
| 5781 | } |
| 5782 | mddev->major_version = MD_MAJOR_VERSION; |
| 5783 | mddev->minor_version = MD_MINOR_VERSION; |
| 5784 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5785 | mddev->ctime = get_seconds(); |
| 5786 | |
| 5787 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5788 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5789 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | mddev->raid_disks = info->raid_disks; |
| 5791 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5792 | * openned |
| 5793 | */ |
| 5794 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5795 | mddev->recovery_cp = MaxSector; |
| 5796 | else |
| 5797 | mddev->recovery_cp = 0; |
| 5798 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5799 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | |
| 5801 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5802 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | |
| 5804 | mddev->max_disks = MD_SB_DISKS; |
| 5805 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5806 | if (mddev->persistent) |
| 5807 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5808 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5810 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5811 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5812 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5813 | mddev->reshape_position = MaxSector; |
| 5814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | /* |
| 5816 | * Generate a 128 bit UUID |
| 5817 | */ |
| 5818 | get_random_bytes(mddev->uuid, 16); |
| 5819 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5820 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5821 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5822 | mddev->new_layout = mddev->layout; |
| 5823 | mddev->delta_disks = 0; |
| 5824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | return 0; |
| 5826 | } |
| 5827 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5828 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5829 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5830 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5831 | |
| 5832 | if (mddev->external_size) |
| 5833 | return; |
| 5834 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5835 | mddev->array_sectors = array_sectors; |
| 5836 | } |
| 5837 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5838 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5839 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5840 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5841 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5842 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5843 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5844 | |
| 5845 | if (mddev->pers->resize == NULL) |
| 5846 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5847 | /* The "num_sectors" is the number of sectors of each device that |
| 5848 | * is used. This can only make sense for arrays with redundancy. |
| 5849 | * linear and raid0 always use whatever space is available. We can only |
| 5850 | * consider changing this number if no resync or reconstruction is |
| 5851 | * 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] | 5852 | * 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] | 5853 | * of each device. If num_sectors is zero, we find the largest size |
| 5854 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5855 | */ |
| 5856 | if (mddev->sync_thread) |
| 5857 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5858 | if (mddev->bitmap) |
| 5859 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5860 | * grow, and re-add. |
| 5861 | */ |
| 5862 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5863 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5864 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5865 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5866 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5867 | num_sectors = avail; |
| 5868 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5869 | return -ENOSPC; |
| 5870 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5871 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5872 | if (!rv) |
| 5873 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5874 | return rv; |
| 5875 | } |
| 5876 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5877 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5878 | { |
| 5879 | int rv; |
| 5880 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5881 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5882 | return -EINVAL; |
| 5883 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5884 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5885 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5886 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5887 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5888 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5889 | |
| 5890 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5891 | if (rv < 0) |
| 5892 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5893 | return rv; |
| 5894 | } |
| 5895 | |
| 5896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | /* |
| 5898 | * update_array_info is used to change the configuration of an |
| 5899 | * on-line array. |
| 5900 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5901 | * fields in the info are checked against the array. |
| 5902 | * Any differences that cannot be handled will cause an error. |
| 5903 | * Normally, only one change can be managed at a time. |
| 5904 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5905 | 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] | 5906 | { |
| 5907 | int rv = 0; |
| 5908 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5909 | int state = 0; |
| 5910 | |
| 5911 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5912 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5913 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | |
| 5915 | if (mddev->major_version != info->major_version || |
| 5916 | mddev->minor_version != info->minor_version || |
| 5917 | /* mddev->patch_version != info->patch_version || */ |
| 5918 | mddev->ctime != info->ctime || |
| 5919 | mddev->level != info->level || |
| 5920 | /* mddev->layout != info->layout || */ |
| 5921 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5922 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5923 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5924 | ((state^info->state) & 0xfffffe00) |
| 5925 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | return -EINVAL; |
| 5927 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5928 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5929 | cnt++; |
| 5930 | if (mddev->raid_disks != info->raid_disks) |
| 5931 | cnt++; |
| 5932 | if (mddev->layout != info->layout) |
| 5933 | cnt++; |
| 5934 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5935 | cnt++; |
| 5936 | if (cnt == 0) |
| 5937 | return 0; |
| 5938 | if (cnt > 1) |
| 5939 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | |
| 5941 | if (mddev->layout != info->layout) { |
| 5942 | /* Change layout |
| 5943 | * we don't need to do anything at the md level, the |
| 5944 | * personality will take care of it all. |
| 5945 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5946 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5948 | else { |
| 5949 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5950 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5951 | if (rv) |
| 5952 | mddev->new_layout = mddev->layout; |
| 5953 | return rv; |
| 5954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5956 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5957 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5959 | if (mddev->raid_disks != info->raid_disks) |
| 5960 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5962 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5963 | if (mddev->pers->quiesce == NULL) |
| 5964 | return -EINVAL; |
| 5965 | if (mddev->recovery || mddev->sync_thread) |
| 5966 | return -EBUSY; |
| 5967 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5968 | /* add the bitmap */ |
| 5969 | if (mddev->bitmap) |
| 5970 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5971 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5972 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5973 | mddev->bitmap_info.offset = |
| 5974 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5975 | mddev->pers->quiesce(mddev, 1); |
| 5976 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5977 | if (!rv) |
| 5978 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5979 | if (rv) |
| 5980 | bitmap_destroy(mddev); |
| 5981 | mddev->pers->quiesce(mddev, 0); |
| 5982 | } else { |
| 5983 | /* remove the bitmap */ |
| 5984 | if (!mddev->bitmap) |
| 5985 | return -ENOENT; |
| 5986 | if (mddev->bitmap->file) |
| 5987 | return -EINVAL; |
| 5988 | mddev->pers->quiesce(mddev, 1); |
| 5989 | bitmap_destroy(mddev); |
| 5990 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5991 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5992 | } |
| 5993 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5994 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | return rv; |
| 5996 | } |
| 5997 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5998 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6000 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | |
| 6002 | if (mddev->pers == NULL) |
| 6003 | return -ENODEV; |
| 6004 | |
| 6005 | rdev = find_rdev(mddev, dev); |
| 6006 | if (!rdev) |
| 6007 | return -ENODEV; |
| 6008 | |
| 6009 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6010 | if (!test_bit(Faulty, &rdev->flags)) |
| 6011 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | return 0; |
| 6013 | } |
| 6014 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6015 | /* |
| 6016 | * We have a problem here : there is no easy way to give a CHS |
| 6017 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6018 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6019 | * dosfs just mad... ;-) |
| 6020 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6021 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6022 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6023 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6024 | |
| 6025 | geo->heads = 2; |
| 6026 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6027 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6028 | return 0; |
| 6029 | } |
| 6030 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6031 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | unsigned int cmd, unsigned long arg) |
| 6033 | { |
| 6034 | int err = 0; |
| 6035 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6036 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6037 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | |
| 6039 | if (!capable(CAP_SYS_ADMIN)) |
| 6040 | return -EACCES; |
| 6041 | |
| 6042 | /* |
| 6043 | * Commands dealing with the RAID driver but not any |
| 6044 | * particular array: |
| 6045 | */ |
| 6046 | switch (cmd) |
| 6047 | { |
| 6048 | case RAID_VERSION: |
| 6049 | err = get_version(argp); |
| 6050 | goto done; |
| 6051 | |
| 6052 | case PRINT_RAID_DEBUG: |
| 6053 | err = 0; |
| 6054 | md_print_devices(); |
| 6055 | goto done; |
| 6056 | |
| 6057 | #ifndef MODULE |
| 6058 | case RAID_AUTORUN: |
| 6059 | err = 0; |
| 6060 | autostart_arrays(arg); |
| 6061 | goto done; |
| 6062 | #endif |
| 6063 | default:; |
| 6064 | } |
| 6065 | |
| 6066 | /* |
| 6067 | * Commands creating/starting a new array: |
| 6068 | */ |
| 6069 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6070 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
| 6072 | if (!mddev) { |
| 6073 | BUG(); |
| 6074 | goto abort; |
| 6075 | } |
| 6076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | err = mddev_lock(mddev); |
| 6078 | if (err) { |
| 6079 | printk(KERN_INFO |
| 6080 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6081 | err, cmd); |
| 6082 | goto abort; |
| 6083 | } |
| 6084 | |
| 6085 | switch (cmd) |
| 6086 | { |
| 6087 | case SET_ARRAY_INFO: |
| 6088 | { |
| 6089 | mdu_array_info_t info; |
| 6090 | if (!arg) |
| 6091 | memset(&info, 0, sizeof(info)); |
| 6092 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6093 | err = -EFAULT; |
| 6094 | goto abort_unlock; |
| 6095 | } |
| 6096 | if (mddev->pers) { |
| 6097 | err = update_array_info(mddev, &info); |
| 6098 | if (err) { |
| 6099 | printk(KERN_WARNING "md: couldn't update" |
| 6100 | " array info. %d\n", err); |
| 6101 | goto abort_unlock; |
| 6102 | } |
| 6103 | goto done_unlock; |
| 6104 | } |
| 6105 | if (!list_empty(&mddev->disks)) { |
| 6106 | printk(KERN_WARNING |
| 6107 | "md: array %s already has disks!\n", |
| 6108 | mdname(mddev)); |
| 6109 | err = -EBUSY; |
| 6110 | goto abort_unlock; |
| 6111 | } |
| 6112 | if (mddev->raid_disks) { |
| 6113 | printk(KERN_WARNING |
| 6114 | "md: array %s already initialised!\n", |
| 6115 | mdname(mddev)); |
| 6116 | err = -EBUSY; |
| 6117 | goto abort_unlock; |
| 6118 | } |
| 6119 | err = set_array_info(mddev, &info); |
| 6120 | if (err) { |
| 6121 | printk(KERN_WARNING "md: couldn't set" |
| 6122 | " array info. %d\n", err); |
| 6123 | goto abort_unlock; |
| 6124 | } |
| 6125 | } |
| 6126 | goto done_unlock; |
| 6127 | |
| 6128 | default:; |
| 6129 | } |
| 6130 | |
| 6131 | /* |
| 6132 | * Commands querying/configuring an existing array: |
| 6133 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6134 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6135 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6136 | if ((!mddev->raid_disks && !mddev->external) |
| 6137 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6138 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6139 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | err = -ENODEV; |
| 6141 | goto abort_unlock; |
| 6142 | } |
| 6143 | |
| 6144 | /* |
| 6145 | * Commands even a read-only array can execute: |
| 6146 | */ |
| 6147 | switch (cmd) |
| 6148 | { |
| 6149 | case GET_ARRAY_INFO: |
| 6150 | err = get_array_info(mddev, argp); |
| 6151 | goto done_unlock; |
| 6152 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6153 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6154 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6155 | goto done_unlock; |
| 6156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | case GET_DISK_INFO: |
| 6158 | err = get_disk_info(mddev, argp); |
| 6159 | goto done_unlock; |
| 6160 | |
| 6161 | case RESTART_ARRAY_RW: |
| 6162 | err = restart_array(mddev); |
| 6163 | goto done_unlock; |
| 6164 | |
| 6165 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6166 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | goto done_unlock; |
| 6168 | |
| 6169 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6170 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | goto done_unlock; |
| 6172 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6173 | case BLKROSET: |
| 6174 | if (get_user(ro, (int __user *)(arg))) { |
| 6175 | err = -EFAULT; |
| 6176 | goto done_unlock; |
| 6177 | } |
| 6178 | err = -EINVAL; |
| 6179 | |
| 6180 | /* if the bdev is going readonly the value of mddev->ro |
| 6181 | * does not matter, no writes are coming |
| 6182 | */ |
| 6183 | if (ro) |
| 6184 | goto done_unlock; |
| 6185 | |
| 6186 | /* are we are already prepared for writes? */ |
| 6187 | if (mddev->ro != 1) |
| 6188 | goto done_unlock; |
| 6189 | |
| 6190 | /* transitioning to readauto need only happen for |
| 6191 | * arrays that call md_write_start |
| 6192 | */ |
| 6193 | if (mddev->pers) { |
| 6194 | err = restart_array(mddev); |
| 6195 | if (err == 0) { |
| 6196 | mddev->ro = 2; |
| 6197 | set_disk_ro(mddev->gendisk, 0); |
| 6198 | } |
| 6199 | } |
| 6200 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
| 6203 | /* |
| 6204 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6205 | * superblock, so we do not allow them on read-only arrays. |
| 6206 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6207 | * here and hit the 'default' below, so only disallow |
| 6208 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6210 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6211 | if (mddev->ro == 2) { |
| 6212 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6213 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6214 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6215 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6216 | } else { |
| 6217 | err = -EROFS; |
| 6218 | goto abort_unlock; |
| 6219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | switch (cmd) |
| 6223 | { |
| 6224 | case ADD_NEW_DISK: |
| 6225 | { |
| 6226 | mdu_disk_info_t info; |
| 6227 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6228 | err = -EFAULT; |
| 6229 | else |
| 6230 | err = add_new_disk(mddev, &info); |
| 6231 | goto done_unlock; |
| 6232 | } |
| 6233 | |
| 6234 | case HOT_REMOVE_DISK: |
| 6235 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6236 | goto done_unlock; |
| 6237 | |
| 6238 | case HOT_ADD_DISK: |
| 6239 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6240 | goto done_unlock; |
| 6241 | |
| 6242 | case SET_DISK_FAULTY: |
| 6243 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6244 | goto done_unlock; |
| 6245 | |
| 6246 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6247 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | goto done_unlock; |
| 6249 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6250 | case SET_BITMAP_FILE: |
| 6251 | err = set_bitmap_file(mddev, (int)arg); |
| 6252 | goto done_unlock; |
| 6253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | err = -EINVAL; |
| 6256 | goto abort_unlock; |
| 6257 | } |
| 6258 | |
| 6259 | done_unlock: |
| 6260 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6261 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6262 | err != -EINVAL) |
| 6263 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | mddev_unlock(mddev); |
| 6265 | |
| 6266 | return err; |
| 6267 | done: |
| 6268 | if (err) |
| 6269 | MD_BUG(); |
| 6270 | abort: |
| 6271 | return err; |
| 6272 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6273 | #ifdef CONFIG_COMPAT |
| 6274 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6275 | unsigned int cmd, unsigned long arg) |
| 6276 | { |
| 6277 | switch (cmd) { |
| 6278 | case HOT_REMOVE_DISK: |
| 6279 | case HOT_ADD_DISK: |
| 6280 | case SET_DISK_FAULTY: |
| 6281 | case SET_BITMAP_FILE: |
| 6282 | /* These take in integer arg, do not convert */ |
| 6283 | break; |
| 6284 | default: |
| 6285 | arg = (unsigned long)compat_ptr(arg); |
| 6286 | break; |
| 6287 | } |
| 6288 | |
| 6289 | return md_ioctl(bdev, mode, cmd, arg); |
| 6290 | } |
| 6291 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6293 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | { |
| 6295 | /* |
| 6296 | * Succeed if we can lock the mddev, which confirms that |
| 6297 | * it isn't being stopped right now. |
| 6298 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6299 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | int err; |
| 6301 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6302 | if (mddev->gendisk != bdev->bd_disk) { |
| 6303 | /* we are racing with mddev_put which is discarding this |
| 6304 | * bd_disk. |
| 6305 | */ |
| 6306 | mddev_put(mddev); |
| 6307 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6308 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6309 | /* Then retry the open from the top */ |
| 6310 | return -ERESTARTSYS; |
| 6311 | } |
| 6312 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6313 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6314 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | goto out; |
| 6316 | |
| 6317 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6318 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6319 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6321 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | out: |
| 6323 | return err; |
| 6324 | } |
| 6325 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6326 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6328 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6329 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6330 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6331 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | mddev_put(mddev); |
| 6333 | |
| 6334 | return 0; |
| 6335 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6336 | |
| 6337 | static int md_media_changed(struct gendisk *disk) |
| 6338 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6339 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6340 | |
| 6341 | return mddev->changed; |
| 6342 | } |
| 6343 | |
| 6344 | static int md_revalidate(struct gendisk *disk) |
| 6345 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6346 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6347 | |
| 6348 | mddev->changed = 0; |
| 6349 | return 0; |
| 6350 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6351 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | { |
| 6353 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6354 | .open = md_open, |
| 6355 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6356 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6357 | #ifdef CONFIG_COMPAT |
| 6358 | .compat_ioctl = md_compat_ioctl, |
| 6359 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6360 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6361 | .media_changed = md_media_changed, |
| 6362 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | }; |
| 6364 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6365 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6367 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | /* |
| 6370 | * md_thread is a 'system-thread', it's priority should be very |
| 6371 | * high. We avoid resource deadlocks individually in each |
| 6372 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6373 | * the very same RT priority as kswapd, thus we will never get |
| 6374 | * into a priority inversion deadlock. |
| 6375 | * |
| 6376 | * we definitely have to have equal or higher priority than |
| 6377 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6378 | * many dirty RAID5 blocks. |
| 6379 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6381 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6382 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6384 | /* We need to wait INTERRUPTIBLE so that |
| 6385 | * we don't add to the load-average. |
| 6386 | * That means we need to be sure no signals are |
| 6387 | * pending |
| 6388 | */ |
| 6389 | if (signal_pending(current)) |
| 6390 | flush_signals(current); |
| 6391 | |
| 6392 | wait_event_interruptible_timeout |
| 6393 | (thread->wqueue, |
| 6394 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6395 | || kthread_should_stop(), |
| 6396 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6398 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6399 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6400 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | return 0; |
| 6404 | } |
| 6405 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6406 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | { |
| 6408 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6409 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6411 | wake_up(&thread->wqueue); |
| 6412 | } |
| 6413 | } |
| 6414 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6415 | 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] | 6416 | const char *name) |
| 6417 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6418 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6420 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | if (!thread) |
| 6422 | return NULL; |
| 6423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | init_waitqueue_head(&thread->wqueue); |
| 6425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | thread->run = run; |
| 6427 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6428 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6429 | thread->tsk = kthread_run(md_thread, thread, |
| 6430 | "%s_%s", |
| 6431 | mdname(thread->mddev), |
| 6432 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6433 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | kfree(thread); |
| 6435 | return NULL; |
| 6436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | return thread; |
| 6438 | } |
| 6439 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6440 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6442 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6443 | if (!thread) |
| 6444 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6445 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6446 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6447 | * non-existent thread |
| 6448 | */ |
| 6449 | spin_lock(&pers_lock); |
| 6450 | *threadp = NULL; |
| 6451 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6452 | |
| 6453 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | kfree(thread); |
| 6455 | } |
| 6456 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6457 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | { |
| 6459 | if (!mddev) { |
| 6460 | MD_BUG(); |
| 6461 | return; |
| 6462 | } |
| 6463 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6464 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6466 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6467 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | return; |
| 6469 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6470 | if (mddev->degraded) |
| 6471 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6472 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6474 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6475 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6476 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6477 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6478 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | } |
| 6480 | |
| 6481 | /* seq_file implementation /proc/mdstat */ |
| 6482 | |
| 6483 | static void status_unused(struct seq_file *seq) |
| 6484 | { |
| 6485 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6486 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | |
| 6488 | seq_printf(seq, "unused devices: "); |
| 6489 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6490 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | char b[BDEVNAME_SIZE]; |
| 6492 | i++; |
| 6493 | seq_printf(seq, "%s ", |
| 6494 | bdevname(rdev->bdev,b)); |
| 6495 | } |
| 6496 | if (!i) |
| 6497 | seq_printf(seq, "<none>"); |
| 6498 | |
| 6499 | seq_printf(seq, "\n"); |
| 6500 | } |
| 6501 | |
| 6502 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6503 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6505 | sector_t max_sectors, resync, res; |
| 6506 | unsigned long dt, db; |
| 6507 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6508 | int scale; |
| 6509 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6511 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | |
| 6513 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6514 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6516 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | |
| 6518 | /* |
| 6519 | * Should not happen. |
| 6520 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6521 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | MD_BUG(); |
| 6523 | return; |
| 6524 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6525 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6526 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6527 | * u32, as those are the requirements for sector_div. |
| 6528 | * Thus 'scale' must be at least 10 |
| 6529 | */ |
| 6530 | scale = 10; |
| 6531 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6532 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6533 | scale++; |
| 6534 | } |
| 6535 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6536 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6537 | |
| 6538 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6540 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | seq_printf(seq, "["); |
| 6542 | for (i = 0; i < x; i++) |
| 6543 | seq_printf(seq, "="); |
| 6544 | seq_printf(seq, ">"); |
| 6545 | for (i = 0; i < y; i++) |
| 6546 | seq_printf(seq, "."); |
| 6547 | seq_printf(seq, "] "); |
| 6548 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6549 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6550 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6551 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6552 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6553 | "check" : |
| 6554 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6555 | "resync" : "recovery"))), |
| 6556 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6557 | (unsigned long long) resync/2, |
| 6558 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | |
| 6560 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | * dt: time from mark until now |
| 6562 | * db: blocks written from mark until now |
| 6563 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6564 | * |
| 6565 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6566 | * So we divide before multiply in case it is 32bit and close |
| 6567 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6568 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6569 | * near the end of resync when the number of remaining sectors |
| 6570 | * is close to 'db'. |
| 6571 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6572 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | */ |
| 6574 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6575 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6576 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6577 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6579 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6580 | sector_div(rt, db/32+1); |
| 6581 | rt *= dt; |
| 6582 | rt >>= 5; |
| 6583 | |
| 6584 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6585 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6587 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6591 | { |
| 6592 | struct list_head *tmp; |
| 6593 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6594 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | |
| 6596 | if (l >= 0x10000) |
| 6597 | return NULL; |
| 6598 | if (!l--) |
| 6599 | /* header */ |
| 6600 | return (void*)1; |
| 6601 | |
| 6602 | spin_lock(&all_mddevs_lock); |
| 6603 | list_for_each(tmp,&all_mddevs) |
| 6604 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6605 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6606 | mddev_get(mddev); |
| 6607 | spin_unlock(&all_mddevs_lock); |
| 6608 | return mddev; |
| 6609 | } |
| 6610 | spin_unlock(&all_mddevs_lock); |
| 6611 | if (!l--) |
| 6612 | return (void*)2;/* tail */ |
| 6613 | return NULL; |
| 6614 | } |
| 6615 | |
| 6616 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6617 | { |
| 6618 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6619 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | |
| 6621 | ++*pos; |
| 6622 | if (v == (void*)2) |
| 6623 | return NULL; |
| 6624 | |
| 6625 | spin_lock(&all_mddevs_lock); |
| 6626 | if (v == (void*)1) |
| 6627 | tmp = all_mddevs.next; |
| 6628 | else |
| 6629 | tmp = mddev->all_mddevs.next; |
| 6630 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6631 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | else { |
| 6633 | next_mddev = (void*)2; |
| 6634 | *pos = 0x10000; |
| 6635 | } |
| 6636 | spin_unlock(&all_mddevs_lock); |
| 6637 | |
| 6638 | if (v != (void*)1) |
| 6639 | mddev_put(mddev); |
| 6640 | return next_mddev; |
| 6641 | |
| 6642 | } |
| 6643 | |
| 6644 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6645 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6646 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | |
| 6648 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6649 | mddev_put(mddev); |
| 6650 | } |
| 6651 | |
| 6652 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6653 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6654 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6655 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6656 | struct md_rdev *rdev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6657 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | |
| 6659 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6660 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | seq_printf(seq, "Personalities : "); |
| 6662 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6663 | list_for_each_entry(pers, &pers_list, list) |
| 6664 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | |
| 6666 | spin_unlock(&pers_lock); |
| 6667 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6668 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | return 0; |
| 6670 | } |
| 6671 | if (v == (void*)2) { |
| 6672 | status_unused(seq); |
| 6673 | return 0; |
| 6674 | } |
| 6675 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6676 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6680 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6681 | mddev->pers ? "" : "in"); |
| 6682 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6683 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6684 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6685 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6686 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6687 | seq_printf(seq, " %s", mddev->pers->name); |
| 6688 | } |
| 6689 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6690 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6691 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | char b[BDEVNAME_SIZE]; |
| 6693 | seq_printf(seq, " %s[%d]", |
| 6694 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6695 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6696 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6697 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | seq_printf(seq, "(F)"); |
| 6699 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6700 | } else if (rdev->raid_disk < 0) |
| 6701 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6702 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | } |
| 6704 | |
| 6705 | if (!list_empty(&mddev->disks)) { |
| 6706 | if (mddev->pers) |
| 6707 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6708 | (unsigned long long) |
| 6709 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | else |
| 6711 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6712 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6714 | if (mddev->persistent) { |
| 6715 | if (mddev->major_version != 0 || |
| 6716 | mddev->minor_version != 90) { |
| 6717 | seq_printf(seq," super %d.%d", |
| 6718 | mddev->major_version, |
| 6719 | mddev->minor_version); |
| 6720 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6721 | } else if (mddev->external) |
| 6722 | seq_printf(seq, " super external:%s", |
| 6723 | mddev->metadata_type); |
| 6724 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6725 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | |
| 6727 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6728 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6730 | if (mddev->pers->sync_request) { |
| 6731 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6732 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6733 | seq_printf(seq, "\n "); |
| 6734 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6735 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6736 | else if (mddev->recovery_cp < MaxSector) |
| 6737 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6738 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6739 | } else |
| 6740 | seq_printf(seq, "\n "); |
| 6741 | |
| 6742 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6743 | unsigned long chunk_kb; |
| 6744 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6745 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6746 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6747 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6748 | "%lu%s chunk", |
| 6749 | bitmap->pages - bitmap->missing_pages, |
| 6750 | bitmap->pages, |
| 6751 | (bitmap->pages - bitmap->missing_pages) |
| 6752 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6753 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6754 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6755 | if (bitmap->file) { |
| 6756 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6757 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6758 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6759 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6760 | seq_printf(seq, "\n"); |
| 6761 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | } |
| 6763 | |
| 6764 | seq_printf(seq, "\n"); |
| 6765 | } |
| 6766 | mddev_unlock(mddev); |
| 6767 | |
| 6768 | return 0; |
| 6769 | } |
| 6770 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6771 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | .start = md_seq_start, |
| 6773 | .next = md_seq_next, |
| 6774 | .stop = md_seq_stop, |
| 6775 | .show = md_seq_show, |
| 6776 | }; |
| 6777 | |
| 6778 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6779 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6780 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | int error; |
| 6782 | |
| 6783 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6784 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6785 | return error; |
| 6786 | |
| 6787 | seq = file->private_data; |
| 6788 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | return error; |
| 6790 | } |
| 6791 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6792 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6793 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6794 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6795 | int mask; |
| 6796 | |
| 6797 | poll_wait(filp, &md_event_waiters, wait); |
| 6798 | |
| 6799 | /* always allow read */ |
| 6800 | mask = POLLIN | POLLRDNORM; |
| 6801 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6802 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6803 | mask |= POLLERR | POLLPRI; |
| 6804 | return mask; |
| 6805 | } |
| 6806 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6807 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6808 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | .open = md_seq_open, |
| 6810 | .read = seq_read, |
| 6811 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6812 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6813 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | }; |
| 6815 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6816 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6819 | list_add_tail(&p->list, &pers_list); |
| 6820 | 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] | 6821 | spin_unlock(&pers_lock); |
| 6822 | return 0; |
| 6823 | } |
| 6824 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6825 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6827 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6829 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | spin_unlock(&pers_lock); |
| 6831 | return 0; |
| 6832 | } |
| 6833 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6834 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6836 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6838 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | |
| 6840 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6841 | rcu_read_lock(); |
| 6842 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6844 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6845 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6846 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6847 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6848 | * as sync_io is counted when a request starts, and |
| 6849 | * disk_stats is counted when it completes. |
| 6850 | * So resync activity will cause curr_events to be smaller than |
| 6851 | * when there was no such activity. |
| 6852 | * non-sync IO will cause disk_stat to increase without |
| 6853 | * increasing sync_io so curr_events will (eventually) |
| 6854 | * be larger than it was before. Once it becomes |
| 6855 | * substantially larger, the test below will cause |
| 6856 | * the array to appear non-idle, and resync will slow |
| 6857 | * down. |
| 6858 | * If there is a lot of outstanding resync activity when |
| 6859 | * we set last_event to curr_events, then all that activity |
| 6860 | * completing might cause the array to appear non-idle |
| 6861 | * and resync will be slowed down even though there might |
| 6862 | * not have been non-resync activity. This will only |
| 6863 | * happen once though. 'last_events' will soon reflect |
| 6864 | * the state where there is little or no outstanding |
| 6865 | * resync requests, and further resync activity will |
| 6866 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6867 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6869 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | rdev->last_events = curr_events; |
| 6871 | idle = 0; |
| 6872 | } |
| 6873 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6874 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | return idle; |
| 6876 | } |
| 6877 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6878 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6879 | { |
| 6880 | /* another "blocks" (512byte) blocks have been synced */ |
| 6881 | atomic_sub(blocks, &mddev->recovery_active); |
| 6882 | wake_up(&mddev->recovery_wait); |
| 6883 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6884 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | md_wakeup_thread(mddev->thread); |
| 6886 | // stop recovery, signal do_sync .... |
| 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6891 | /* md_write_start(mddev, bi) |
| 6892 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6893 | * in superblock) before writing, schedule a superblock update |
| 6894 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6895 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6896 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6898 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6899 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6900 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6901 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6902 | BUG_ON(mddev->ro == 1); |
| 6903 | if (mddev->ro == 2) { |
| 6904 | /* need to switch to read/write */ |
| 6905 | mddev->ro = 0; |
| 6906 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6907 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6908 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6909 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6910 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6911 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6912 | if (mddev->safemode == 1) |
| 6913 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6914 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6915 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6916 | if (mddev->in_sync) { |
| 6917 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6918 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6919 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6920 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6921 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6922 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6923 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6924 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6925 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6926 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6927 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6928 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | } |
| 6930 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6931 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | { |
| 6933 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6934 | if (mddev->safemode == 2) |
| 6935 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6936 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6938 | } |
| 6939 | } |
| 6940 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6941 | /* md_allow_write(mddev) |
| 6942 | * Calling this ensures that the array is marked 'active' so that writes |
| 6943 | * may proceed without blocking. It is important to call this before |
| 6944 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6945 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6946 | * |
| 6947 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6948 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6949 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6950 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6951 | { |
| 6952 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6953 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6954 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6955 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6956 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6957 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6958 | |
| 6959 | spin_lock_irq(&mddev->write_lock); |
| 6960 | if (mddev->in_sync) { |
| 6961 | mddev->in_sync = 0; |
| 6962 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6963 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6964 | if (mddev->safemode_delay && |
| 6965 | mddev->safemode == 0) |
| 6966 | mddev->safemode = 1; |
| 6967 | spin_unlock_irq(&mddev->write_lock); |
| 6968 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6969 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6970 | } else |
| 6971 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6972 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6973 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6974 | return -EAGAIN; |
| 6975 | else |
| 6976 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6977 | } |
| 6978 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | #define SYNC_MARKS 10 |
| 6981 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6982 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6984 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | unsigned int currspeed = 0, |
| 6986 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6987 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | unsigned long mark[SYNC_MARKS]; |
| 6989 | sector_t mark_cnt[SYNC_MARKS]; |
| 6990 | int last_mark,m; |
| 6991 | struct list_head *tmp; |
| 6992 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6993 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6994 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6995 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | |
| 6997 | /* just incase thread restarts... */ |
| 6998 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6999 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7000 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7001 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7003 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7004 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7005 | desc = "data-check"; |
| 7006 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7007 | desc = "requested-resync"; |
| 7008 | else |
| 7009 | desc = "resync"; |
| 7010 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7011 | desc = "reshape"; |
| 7012 | else |
| 7013 | desc = "recovery"; |
| 7014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | /* we overload curr_resync somewhat here. |
| 7016 | * 0 == not engaged in resync at all |
| 7017 | * 2 == checking that there is no conflict with another sync |
| 7018 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7019 | * commense |
| 7020 | * other == active in resync - this many blocks |
| 7021 | * |
| 7022 | * Before starting a resync we must have set curr_resync to |
| 7023 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7024 | * less than ours. When we find one that is the same or higher |
| 7025 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7026 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7027 | * This will mean we have to start checking from the beginning again. |
| 7028 | * |
| 7029 | */ |
| 7030 | |
| 7031 | do { |
| 7032 | mddev->curr_resync = 2; |
| 7033 | |
| 7034 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7035 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7036 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7037 | |
| 7038 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7040 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | if (mddev2 == mddev) |
| 7042 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7043 | if (!mddev->parallel_resync |
| 7044 | && mddev2->curr_resync |
| 7045 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | DEFINE_WAIT(wq); |
| 7047 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7048 | /* arbitrarily yield */ |
| 7049 | mddev->curr_resync = 1; |
| 7050 | wake_up(&resync_wait); |
| 7051 | } |
| 7052 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7053 | /* no need to wait here, we can wait the next |
| 7054 | * time 'round when curr_resync == 2 |
| 7055 | */ |
| 7056 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7057 | /* We need to wait 'interruptible' so as not to |
| 7058 | * contribute to the load average, and not to |
| 7059 | * be caught by 'softlockup' |
| 7060 | */ |
| 7061 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7062 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7063 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7064 | printk(KERN_INFO "md: delaying %s of %s" |
| 7065 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7067 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7069 | if (signal_pending(current)) |
| 7070 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7071 | schedule(); |
| 7072 | finish_wait(&resync_wait, &wq); |
| 7073 | goto try_again; |
| 7074 | } |
| 7075 | finish_wait(&resync_wait, &wq); |
| 7076 | } |
| 7077 | } |
| 7078 | } while (mddev->curr_resync < 2); |
| 7079 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7080 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7081 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7082 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7083 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7084 | */ |
| 7085 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7086 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7087 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7088 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7089 | j = mddev->resync_min; |
| 7090 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7091 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7092 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7093 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7094 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7095 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7097 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7098 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7099 | rcu_read_lock(); |
| 7100 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7101 | if (rdev->raid_disk >= 0 && |
| 7102 | !test_bit(Faulty, &rdev->flags) && |
| 7103 | !test_bit(In_sync, &rdev->flags) && |
| 7104 | rdev->recovery_offset < j) |
| 7105 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7106 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7109 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7110 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7111 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7112 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7113 | "(but not more than %d KB/sec) for %s.\n", |
| 7114 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7116 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7117 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7118 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7120 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7121 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | } |
| 7123 | last_mark = 0; |
| 7124 | mddev->resync_mark = mark[last_mark]; |
| 7125 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7126 | |
| 7127 | /* |
| 7128 | * Tune reconstruction: |
| 7129 | */ |
| 7130 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7131 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7132 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | |
| 7134 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | last_check = 0; |
| 7136 | |
| 7137 | if (j>2) { |
| 7138 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7139 | "md: resuming %s of %s from checkpoint.\n", |
| 7140 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | mddev->curr_resync = j; |
| 7142 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7143 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7144 | |
| 7145 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7146 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7148 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7149 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7150 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7151 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7152 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7153 | > (max_sectors >> 4)) || |
| 7154 | (j - mddev->curr_resync_completed)*2 |
| 7155 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7156 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7157 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7158 | wait_event(mddev->recovery_wait, |
| 7159 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7160 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7161 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7162 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7163 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7164 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7165 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7166 | /* As this condition is controlled by user-space, |
| 7167 | * we can block indefinitely, so use '_interruptible' |
| 7168 | * to avoid triggering warnings. |
| 7169 | */ |
| 7170 | flush_signals(current); /* just in case */ |
| 7171 | wait_event_interruptible(mddev->recovery_wait, |
| 7172 | mddev->resync_max > j |
| 7173 | || kthread_should_stop()); |
| 7174 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7175 | |
| 7176 | if (kthread_should_stop()) |
| 7177 | goto interrupted; |
| 7178 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7179 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7180 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7181 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7182 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | goto out; |
| 7184 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7185 | |
| 7186 | if (!skipped) { /* actual IO requested */ |
| 7187 | io_sectors += sectors; |
| 7188 | atomic_add(sectors, &mddev->recovery_active); |
| 7189 | } |
| 7190 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7191 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7192 | break; |
| 7193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | j += sectors; |
| 7195 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7196 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7197 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7198 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7199 | * visible in /proc/mdstat |
| 7200 | */ |
| 7201 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7202 | |
| 7203 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | continue; |
| 7205 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7206 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | repeat: |
| 7208 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7209 | /* step marks */ |
| 7210 | int next = (last_mark+1) % SYNC_MARKS; |
| 7211 | |
| 7212 | mddev->resync_mark = mark[next]; |
| 7213 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7214 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7215 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7216 | last_mark = next; |
| 7217 | } |
| 7218 | |
| 7219 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7220 | if (kthread_should_stop()) |
| 7221 | goto interrupted; |
| 7222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | |
| 7224 | /* |
| 7225 | * this loop exits only if either when we are slower than |
| 7226 | * the 'hard' speed limit, or the system was IO-idle for |
| 7227 | * a jiffy. |
| 7228 | * the system might be non-idle CPU-wise, but we only care |
| 7229 | * about not overloading the IO subsystem. (things like an |
| 7230 | * e2fsck being done on the RAID array should execute fast) |
| 7231 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7232 | cond_resched(); |
| 7233 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7234 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7235 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7237 | if (currspeed > speed_min(mddev)) { |
| 7238 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7239 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7240 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | goto repeat; |
| 7242 | } |
| 7243 | } |
| 7244 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7245 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | /* |
| 7247 | * this also signals 'finished resyncing' to md_stop |
| 7248 | */ |
| 7249 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7251 | |
| 7252 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7253 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7254 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7255 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7256 | mddev->curr_resync > 2) { |
| 7257 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7258 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7259 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7260 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7261 | "md: checkpointing %s of %s.\n", |
| 7262 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7263 | mddev->recovery_cp = mddev->curr_resync; |
| 7264 | } |
| 7265 | } else |
| 7266 | mddev->recovery_cp = MaxSector; |
| 7267 | } else { |
| 7268 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7269 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7270 | rcu_read_lock(); |
| 7271 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7272 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7273 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7274 | !test_bit(Faulty, &rdev->flags) && |
| 7275 | !test_bit(In_sync, &rdev->flags) && |
| 7276 | rdev->recovery_offset < mddev->curr_resync) |
| 7277 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7278 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7280 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7281 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7284 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7285 | /* We completed so min/max setting can be forgotten if used. */ |
| 7286 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7287 | mddev->resync_min = 0; |
| 7288 | mddev->resync_max = MaxSector; |
| 7289 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7290 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | mddev->curr_resync = 0; |
| 7292 | wake_up(&resync_wait); |
| 7293 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7294 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7295 | return; |
| 7296 | |
| 7297 | interrupted: |
| 7298 | /* |
| 7299 | * got a signal, exit. |
| 7300 | */ |
| 7301 | printk(KERN_INFO |
| 7302 | "md: md_do_sync() got signal ... exiting\n"); |
| 7303 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7304 | goto out; |
| 7305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7307 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7309 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7310 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7311 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7312 | int spares = 0; |
| 7313 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7314 | mddev->curr_resync_completed = 0; |
| 7315 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7316 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7317 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7318 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7319 | (test_bit(Faulty, &rdev->flags) || |
| 7320 | ! test_bit(In_sync, &rdev->flags)) && |
| 7321 | atomic_read(&rdev->nr_pending)==0) { |
| 7322 | if (mddev->pers->hot_remove_disk( |
| 7323 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7324 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7325 | rdev->raid_disk = -1; |
| 7326 | } |
| 7327 | } |
| 7328 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7329 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7330 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7331 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7332 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7333 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7334 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7335 | if (rdev->raid_disk < 0 |
| 7336 | && !test_bit(Faulty, &rdev->flags)) { |
| 7337 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7338 | if (mddev->pers-> |
| 7339 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7340 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7341 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7342 | spares++; |
| 7343 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7344 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7345 | } else |
| 7346 | break; |
| 7347 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7348 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7349 | } |
| 7350 | return spares; |
| 7351 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7352 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7353 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7354 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7355 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7356 | |
| 7357 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7358 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7359 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7360 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7361 | /* success...*/ |
| 7362 | /* activate any spares */ |
| 7363 | if (mddev->pers->spare_active(mddev)) |
| 7364 | sysfs_notify(&mddev->kobj, NULL, |
| 7365 | "degraded"); |
| 7366 | } |
| 7367 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7368 | mddev->pers->finish_reshape) |
| 7369 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7370 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7371 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7372 | * information must be scrapped. Also if any device is now |
| 7373 | * In_sync we must scrape the saved_raid_disk for that device |
| 7374 | * do the superblock for an incrementally recovered device |
| 7375 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7376 | */ |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7377 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7378 | if (!mddev->degraded || |
| 7379 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7380 | rdev->saved_raid_disk = -1; |
| 7381 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7382 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7383 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7384 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7385 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7386 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7387 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7388 | /* flag recovery needed just to double check */ |
| 7389 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7390 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7391 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7392 | if (mddev->event_work.func) |
| 7393 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7394 | } |
| 7395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | /* |
| 7397 | * This routine is regularly called by all per-raid-array threads to |
| 7398 | * deal with generic issues like resync and super-block update. |
| 7399 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7400 | * need this as they never do any recovery or update the superblock. |
| 7401 | * |
| 7402 | * It does not do any resync itself, but rather "forks" off other threads |
| 7403 | * to do that as needed. |
| 7404 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7405 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7406 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | * and wakeups up this thread which will reap the thread and finish up. |
| 7408 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7409 | * |
| 7410 | * The overall approach is: |
| 7411 | * 1/ if the superblock needs updating, update it. |
| 7412 | * 2/ If a recovery thread is running, don't do anything else. |
| 7413 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7414 | * 4/ If there are any faulty devices, remove them. |
| 7415 | * 5/ If array is degraded, try to add spares devices |
| 7416 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7417 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7418 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7419 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7420 | if (mddev->suspended) |
| 7421 | return; |
| 7422 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7423 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7424 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7426 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7427 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7428 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7429 | mdname(mddev)); |
| 7430 | mddev->safemode = 2; |
| 7431 | } |
| 7432 | flush_signals(current); |
| 7433 | } |
| 7434 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7435 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7436 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7438 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7440 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7441 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7442 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7443 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | )) |
| 7445 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7446 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7447 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7448 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7449 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7450 | if (mddev->ro) { |
| 7451 | /* Only thing we do on a ro array is remove |
| 7452 | * failed devices. |
| 7453 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7454 | struct md_rdev *rdev; |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7455 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7456 | if (rdev->raid_disk >= 0 && |
| 7457 | !test_bit(Blocked, &rdev->flags) && |
| 7458 | test_bit(Faulty, &rdev->flags) && |
| 7459 | atomic_read(&rdev->nr_pending)==0) { |
| 7460 | if (mddev->pers->hot_remove_disk( |
| 7461 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7462 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7463 | rdev->raid_disk = -1; |
| 7464 | } |
| 7465 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7466 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7467 | goto unlock; |
| 7468 | } |
| 7469 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7470 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7471 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7472 | spin_lock_irq(&mddev->write_lock); |
| 7473 | if (mddev->safemode && |
| 7474 | !atomic_read(&mddev->writes_pending) && |
| 7475 | !mddev->in_sync && |
| 7476 | mddev->recovery_cp == MaxSector) { |
| 7477 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7478 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7479 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7480 | } |
| 7481 | if (mddev->safemode == 1) |
| 7482 | mddev->safemode = 0; |
| 7483 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7484 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7485 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7486 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7487 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7488 | if (mddev->flags) |
| 7489 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7491 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7492 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7493 | /* resync/recovery still happening */ |
| 7494 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7495 | goto unlock; |
| 7496 | } |
| 7497 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7498 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | goto unlock; |
| 7500 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7501 | /* Set RUNNING before clearing NEEDED to avoid |
| 7502 | * any transients in the value of "sync_action". |
| 7503 | */ |
| 7504 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7505 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7506 | /* Clear some bits that don't mean anything, but |
| 7507 | * might be left set |
| 7508 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7509 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7510 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7511 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7512 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7513 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | /* no recovery is running. |
| 7515 | * remove any failed drives, then |
| 7516 | * add spares if possible. |
| 7517 | * Spare are also removed and re-added, to allow |
| 7518 | * the personality to fail the re-add. |
| 7519 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7521 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7522 | if (mddev->pers->check_reshape == NULL || |
| 7523 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7524 | /* Cannot proceed */ |
| 7525 | goto unlock; |
| 7526 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7527 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7528 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7529 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7530 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7531 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7532 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7533 | } else if (mddev->recovery_cp < MaxSector) { |
| 7534 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7535 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7536 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7537 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7540 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7541 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7542 | /* We are adding a device or devices to an array |
| 7543 | * which has the bitmap stored on all devices. |
| 7544 | * So make sure all bitmap pages get written |
| 7545 | */ |
| 7546 | bitmap_write_all(mddev->bitmap); |
| 7547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7548 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7549 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7550 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | if (!mddev->sync_thread) { |
| 7552 | printk(KERN_ERR "%s: could not start resync" |
| 7553 | " thread...\n", |
| 7554 | mdname(mddev)); |
| 7555 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7556 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7557 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7558 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7559 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7560 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7561 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7562 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7563 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7564 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7565 | } |
| 7566 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7567 | if (!mddev->sync_thread) { |
| 7568 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7569 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7570 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7571 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7572 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7574 | mddev_unlock(mddev); |
| 7575 | } |
| 7576 | } |
| 7577 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7578 | 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] | 7579 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7580 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7581 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7582 | !test_bit(Blocked, &rdev->flags) && |
| 7583 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7584 | msecs_to_jiffies(5000)); |
| 7585 | rdev_dec_pending(rdev, mddev); |
| 7586 | } |
| 7587 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7588 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7589 | |
| 7590 | /* Bad block management. |
| 7591 | * We can record which blocks on each device are 'bad' and so just |
| 7592 | * fail those blocks, or that stripe, rather than the whole device. |
| 7593 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7594 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7595 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7596 | * A 'shift' can be set so that larger blocks are tracked and |
| 7597 | * consequently larger devices can be covered. |
| 7598 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7599 | * |
| 7600 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7601 | * might need to retry if it is very unlucky. |
| 7602 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7603 | * so we use the write_seqlock_irq variant. |
| 7604 | * |
| 7605 | * When looking for a bad block we specify a range and want to |
| 7606 | * know if any block in the range is bad. So we binary-search |
| 7607 | * to the last range that starts at-or-before the given endpoint, |
| 7608 | * (or "before the sector after the target range") |
| 7609 | * then see if it ends after the given start. |
| 7610 | * We return |
| 7611 | * 0 if there are no known bad blocks in the range |
| 7612 | * 1 if there are known bad block which are all acknowledged |
| 7613 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7614 | * plus the start/length of the first bad section we overlap. |
| 7615 | */ |
| 7616 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7617 | sector_t *first_bad, int *bad_sectors) |
| 7618 | { |
| 7619 | int hi; |
| 7620 | int lo = 0; |
| 7621 | u64 *p = bb->page; |
| 7622 | int rv = 0; |
| 7623 | sector_t target = s + sectors; |
| 7624 | unsigned seq; |
| 7625 | |
| 7626 | if (bb->shift > 0) { |
| 7627 | /* round the start down, and the end up */ |
| 7628 | s >>= bb->shift; |
| 7629 | target += (1<<bb->shift) - 1; |
| 7630 | target >>= bb->shift; |
| 7631 | sectors = target - s; |
| 7632 | } |
| 7633 | /* 'target' is now the first block after the bad range */ |
| 7634 | |
| 7635 | retry: |
| 7636 | seq = read_seqbegin(&bb->lock); |
| 7637 | |
| 7638 | hi = bb->count; |
| 7639 | |
| 7640 | /* Binary search between lo and hi for 'target' |
| 7641 | * i.e. for the last range that starts before 'target' |
| 7642 | */ |
| 7643 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7644 | * are known not to be the last range before target. |
| 7645 | * VARIANT: hi-lo is the number of possible |
| 7646 | * ranges, and decreases until it reaches 1 |
| 7647 | */ |
| 7648 | while (hi - lo > 1) { |
| 7649 | int mid = (lo + hi) / 2; |
| 7650 | sector_t a = BB_OFFSET(p[mid]); |
| 7651 | if (a < target) |
| 7652 | /* This could still be the one, earlier ranges |
| 7653 | * could not. */ |
| 7654 | lo = mid; |
| 7655 | else |
| 7656 | /* This and later ranges are definitely out. */ |
| 7657 | hi = mid; |
| 7658 | } |
| 7659 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7660 | if (hi > lo) { |
| 7661 | /* need to check all range that end after 's' to see if |
| 7662 | * any are unacknowledged. |
| 7663 | */ |
| 7664 | while (lo >= 0 && |
| 7665 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7666 | if (BB_OFFSET(p[lo]) < target) { |
| 7667 | /* starts before the end, and finishes after |
| 7668 | * the start, so they must overlap |
| 7669 | */ |
| 7670 | if (rv != -1 && BB_ACK(p[lo])) |
| 7671 | rv = 1; |
| 7672 | else |
| 7673 | rv = -1; |
| 7674 | *first_bad = BB_OFFSET(p[lo]); |
| 7675 | *bad_sectors = BB_LEN(p[lo]); |
| 7676 | } |
| 7677 | lo--; |
| 7678 | } |
| 7679 | } |
| 7680 | |
| 7681 | if (read_seqretry(&bb->lock, seq)) |
| 7682 | goto retry; |
| 7683 | |
| 7684 | return rv; |
| 7685 | } |
| 7686 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7687 | |
| 7688 | /* |
| 7689 | * Add a range of bad blocks to the table. |
| 7690 | * This might extend the table, or might contract it |
| 7691 | * if two adjacent ranges can be merged. |
| 7692 | * We binary-search to find the 'insertion' point, then |
| 7693 | * decide how best to handle it. |
| 7694 | */ |
| 7695 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7696 | int acknowledged) |
| 7697 | { |
| 7698 | u64 *p; |
| 7699 | int lo, hi; |
| 7700 | int rv = 1; |
| 7701 | |
| 7702 | if (bb->shift < 0) |
| 7703 | /* badblocks are disabled */ |
| 7704 | return 0; |
| 7705 | |
| 7706 | if (bb->shift) { |
| 7707 | /* round the start down, and the end up */ |
| 7708 | sector_t next = s + sectors; |
| 7709 | s >>= bb->shift; |
| 7710 | next += (1<<bb->shift) - 1; |
| 7711 | next >>= bb->shift; |
| 7712 | sectors = next - s; |
| 7713 | } |
| 7714 | |
| 7715 | write_seqlock_irq(&bb->lock); |
| 7716 | |
| 7717 | p = bb->page; |
| 7718 | lo = 0; |
| 7719 | hi = bb->count; |
| 7720 | /* Find the last range that starts at-or-before 's' */ |
| 7721 | while (hi - lo > 1) { |
| 7722 | int mid = (lo + hi) / 2; |
| 7723 | sector_t a = BB_OFFSET(p[mid]); |
| 7724 | if (a <= s) |
| 7725 | lo = mid; |
| 7726 | else |
| 7727 | hi = mid; |
| 7728 | } |
| 7729 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7730 | hi = lo; |
| 7731 | |
| 7732 | if (hi > lo) { |
| 7733 | /* we found a range that might merge with the start |
| 7734 | * of our new range |
| 7735 | */ |
| 7736 | sector_t a = BB_OFFSET(p[lo]); |
| 7737 | sector_t e = a + BB_LEN(p[lo]); |
| 7738 | int ack = BB_ACK(p[lo]); |
| 7739 | if (e >= s) { |
| 7740 | /* Yes, we can merge with a previous range */ |
| 7741 | if (s == a && s + sectors >= e) |
| 7742 | /* new range covers old */ |
| 7743 | ack = acknowledged; |
| 7744 | else |
| 7745 | ack = ack && acknowledged; |
| 7746 | |
| 7747 | if (e < s + sectors) |
| 7748 | e = s + sectors; |
| 7749 | if (e - a <= BB_MAX_LEN) { |
| 7750 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7751 | s = e; |
| 7752 | } else { |
| 7753 | /* does not all fit in one range, |
| 7754 | * make p[lo] maximal |
| 7755 | */ |
| 7756 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7757 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7758 | s = a + BB_MAX_LEN; |
| 7759 | } |
| 7760 | sectors = e - s; |
| 7761 | } |
| 7762 | } |
| 7763 | if (sectors && hi < bb->count) { |
| 7764 | /* 'hi' points to the first range that starts after 's'. |
| 7765 | * Maybe we can merge with the start of that range */ |
| 7766 | sector_t a = BB_OFFSET(p[hi]); |
| 7767 | sector_t e = a + BB_LEN(p[hi]); |
| 7768 | int ack = BB_ACK(p[hi]); |
| 7769 | if (a <= s + sectors) { |
| 7770 | /* merging is possible */ |
| 7771 | if (e <= s + sectors) { |
| 7772 | /* full overlap */ |
| 7773 | e = s + sectors; |
| 7774 | ack = acknowledged; |
| 7775 | } else |
| 7776 | ack = ack && acknowledged; |
| 7777 | |
| 7778 | a = s; |
| 7779 | if (e - a <= BB_MAX_LEN) { |
| 7780 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7781 | s = e; |
| 7782 | } else { |
| 7783 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7784 | s = a + BB_MAX_LEN; |
| 7785 | } |
| 7786 | sectors = e - s; |
| 7787 | lo = hi; |
| 7788 | hi++; |
| 7789 | } |
| 7790 | } |
| 7791 | if (sectors == 0 && hi < bb->count) { |
| 7792 | /* we might be able to combine lo and hi */ |
| 7793 | /* Note: 's' is at the end of 'lo' */ |
| 7794 | sector_t a = BB_OFFSET(p[hi]); |
| 7795 | int lolen = BB_LEN(p[lo]); |
| 7796 | int hilen = BB_LEN(p[hi]); |
| 7797 | int newlen = lolen + hilen - (s - a); |
| 7798 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7799 | /* yes, we can combine them */ |
| 7800 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7801 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7802 | memmove(p + hi, p + hi + 1, |
| 7803 | (bb->count - hi - 1) * 8); |
| 7804 | bb->count--; |
| 7805 | } |
| 7806 | } |
| 7807 | while (sectors) { |
| 7808 | /* didn't merge (it all). |
| 7809 | * Need to add a range just before 'hi' */ |
| 7810 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7811 | /* No room for more */ |
| 7812 | rv = 0; |
| 7813 | break; |
| 7814 | } else { |
| 7815 | int this_sectors = sectors; |
| 7816 | memmove(p + hi + 1, p + hi, |
| 7817 | (bb->count - hi) * 8); |
| 7818 | bb->count++; |
| 7819 | |
| 7820 | if (this_sectors > BB_MAX_LEN) |
| 7821 | this_sectors = BB_MAX_LEN; |
| 7822 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7823 | sectors -= this_sectors; |
| 7824 | s += this_sectors; |
| 7825 | } |
| 7826 | } |
| 7827 | |
| 7828 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7829 | if (!acknowledged) |
| 7830 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7831 | write_sequnlock_irq(&bb->lock); |
| 7832 | |
| 7833 | return rv; |
| 7834 | } |
| 7835 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7836 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7837 | int acknowledged) |
| 7838 | { |
| 7839 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7840 | s + rdev->data_offset, sectors, acknowledged); |
| 7841 | if (rv) { |
| 7842 | /* Make sure they get written out promptly */ |
| 7843 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7844 | md_wakeup_thread(rdev->mddev->thread); |
| 7845 | } |
| 7846 | return rv; |
| 7847 | } |
| 7848 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7849 | |
| 7850 | /* |
| 7851 | * Remove a range of bad blocks from the table. |
| 7852 | * This may involve extending the table if we spilt a region, |
| 7853 | * but it must not fail. So if the table becomes full, we just |
| 7854 | * drop the remove request. |
| 7855 | */ |
| 7856 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7857 | { |
| 7858 | u64 *p; |
| 7859 | int lo, hi; |
| 7860 | sector_t target = s + sectors; |
| 7861 | int rv = 0; |
| 7862 | |
| 7863 | if (bb->shift > 0) { |
| 7864 | /* When clearing we round the start up and the end down. |
| 7865 | * This should not matter as the shift should align with |
| 7866 | * the block size and no rounding should ever be needed. |
| 7867 | * However it is better the think a block is bad when it |
| 7868 | * isn't than to think a block is not bad when it is. |
| 7869 | */ |
| 7870 | s += (1<<bb->shift) - 1; |
| 7871 | s >>= bb->shift; |
| 7872 | target >>= bb->shift; |
| 7873 | sectors = target - s; |
| 7874 | } |
| 7875 | |
| 7876 | write_seqlock_irq(&bb->lock); |
| 7877 | |
| 7878 | p = bb->page; |
| 7879 | lo = 0; |
| 7880 | hi = bb->count; |
| 7881 | /* Find the last range that starts before 'target' */ |
| 7882 | while (hi - lo > 1) { |
| 7883 | int mid = (lo + hi) / 2; |
| 7884 | sector_t a = BB_OFFSET(p[mid]); |
| 7885 | if (a < target) |
| 7886 | lo = mid; |
| 7887 | else |
| 7888 | hi = mid; |
| 7889 | } |
| 7890 | if (hi > lo) { |
| 7891 | /* p[lo] is the last range that could overlap the |
| 7892 | * current range. Earlier ranges could also overlap, |
| 7893 | * but only this one can overlap the end of the range. |
| 7894 | */ |
| 7895 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7896 | /* Partial overlap, leave the tail of this range */ |
| 7897 | int ack = BB_ACK(p[lo]); |
| 7898 | sector_t a = BB_OFFSET(p[lo]); |
| 7899 | sector_t end = a + BB_LEN(p[lo]); |
| 7900 | |
| 7901 | if (a < s) { |
| 7902 | /* we need to split this range */ |
| 7903 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7904 | rv = 0; |
| 7905 | goto out; |
| 7906 | } |
| 7907 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7908 | bb->count++; |
| 7909 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7910 | lo++; |
| 7911 | } |
| 7912 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7913 | /* there is no longer an overlap */ |
| 7914 | hi = lo; |
| 7915 | lo--; |
| 7916 | } |
| 7917 | while (lo >= 0 && |
| 7918 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7919 | /* This range does overlap */ |
| 7920 | if (BB_OFFSET(p[lo]) < s) { |
| 7921 | /* Keep the early parts of this range. */ |
| 7922 | int ack = BB_ACK(p[lo]); |
| 7923 | sector_t start = BB_OFFSET(p[lo]); |
| 7924 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7925 | /* now low doesn't overlap, so.. */ |
| 7926 | break; |
| 7927 | } |
| 7928 | lo--; |
| 7929 | } |
| 7930 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7931 | * anything between needs to be discarded |
| 7932 | */ |
| 7933 | if (hi - lo > 1) { |
| 7934 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7935 | bb->count -= (hi - lo - 1); |
| 7936 | } |
| 7937 | } |
| 7938 | |
| 7939 | bb->changed = 1; |
| 7940 | out: |
| 7941 | write_sequnlock_irq(&bb->lock); |
| 7942 | return rv; |
| 7943 | } |
| 7944 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7945 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7946 | { |
| 7947 | return md_clear_badblocks(&rdev->badblocks, |
| 7948 | s + rdev->data_offset, |
| 7949 | sectors); |
| 7950 | } |
| 7951 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 7952 | |
| 7953 | /* |
| 7954 | * Acknowledge all bad blocks in a list. |
| 7955 | * This only succeeds if ->changed is clear. It is used by |
| 7956 | * in-kernel metadata updates |
| 7957 | */ |
| 7958 | void md_ack_all_badblocks(struct badblocks *bb) |
| 7959 | { |
| 7960 | if (bb->page == NULL || bb->changed) |
| 7961 | /* no point even trying */ |
| 7962 | return; |
| 7963 | write_seqlock_irq(&bb->lock); |
| 7964 | |
| 7965 | if (bb->changed == 0) { |
| 7966 | u64 *p = bb->page; |
| 7967 | int i; |
| 7968 | for (i = 0; i < bb->count ; i++) { |
| 7969 | if (!BB_ACK(p[i])) { |
| 7970 | sector_t start = BB_OFFSET(p[i]); |
| 7971 | int len = BB_LEN(p[i]); |
| 7972 | p[i] = BB_MAKE(start, len, 1); |
| 7973 | } |
| 7974 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7975 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7976 | } |
| 7977 | write_sequnlock_irq(&bb->lock); |
| 7978 | } |
| 7979 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 7980 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7981 | /* sysfs access to bad-blocks list. |
| 7982 | * We present two files. |
| 7983 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 7984 | * are recorded as bad. The list is truncated to fit within |
| 7985 | * the one-page limit of sysfs. |
| 7986 | * Writing "sector length" to this file adds an acknowledged |
| 7987 | * bad block list. |
| 7988 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 7989 | * been acknowledged. Writing to this file adds bad blocks |
| 7990 | * without acknowledging them. This is largely for testing. |
| 7991 | */ |
| 7992 | |
| 7993 | static ssize_t |
| 7994 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 7995 | { |
| 7996 | size_t len; |
| 7997 | int i; |
| 7998 | u64 *p = bb->page; |
| 7999 | unsigned seq; |
| 8000 | |
| 8001 | if (bb->shift < 0) |
| 8002 | return 0; |
| 8003 | |
| 8004 | retry: |
| 8005 | seq = read_seqbegin(&bb->lock); |
| 8006 | |
| 8007 | len = 0; |
| 8008 | i = 0; |
| 8009 | |
| 8010 | while (len < PAGE_SIZE && i < bb->count) { |
| 8011 | sector_t s = BB_OFFSET(p[i]); |
| 8012 | unsigned int length = BB_LEN(p[i]); |
| 8013 | int ack = BB_ACK(p[i]); |
| 8014 | i++; |
| 8015 | |
| 8016 | if (unack && ack) |
| 8017 | continue; |
| 8018 | |
| 8019 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8020 | (unsigned long long)s << bb->shift, |
| 8021 | length << bb->shift); |
| 8022 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8023 | if (unack && len == 0) |
| 8024 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8025 | |
| 8026 | if (read_seqretry(&bb->lock, seq)) |
| 8027 | goto retry; |
| 8028 | |
| 8029 | return len; |
| 8030 | } |
| 8031 | |
| 8032 | #define DO_DEBUG 1 |
| 8033 | |
| 8034 | static ssize_t |
| 8035 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8036 | { |
| 8037 | unsigned long long sector; |
| 8038 | int length; |
| 8039 | char newline; |
| 8040 | #ifdef DO_DEBUG |
| 8041 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8042 | * Normally only a successful write may clear a badblock |
| 8043 | */ |
| 8044 | int clear = 0; |
| 8045 | if (page[0] == '-') { |
| 8046 | clear = 1; |
| 8047 | page++; |
| 8048 | } |
| 8049 | #endif /* DO_DEBUG */ |
| 8050 | |
| 8051 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8052 | case 3: |
| 8053 | if (newline != '\n') |
| 8054 | return -EINVAL; |
| 8055 | case 2: |
| 8056 | if (length <= 0) |
| 8057 | return -EINVAL; |
| 8058 | break; |
| 8059 | default: |
| 8060 | return -EINVAL; |
| 8061 | } |
| 8062 | |
| 8063 | #ifdef DO_DEBUG |
| 8064 | if (clear) { |
| 8065 | md_clear_badblocks(bb, sector, length); |
| 8066 | return len; |
| 8067 | } |
| 8068 | #endif /* DO_DEBUG */ |
| 8069 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8070 | return len; |
| 8071 | else |
| 8072 | return -ENOSPC; |
| 8073 | } |
| 8074 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8075 | static int md_notify_reboot(struct notifier_block *this, |
| 8076 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8077 | { |
| 8078 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8079 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8080 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8081 | |
| 8082 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 8083 | |
| 8084 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 8085 | |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8086 | for_each_mddev(mddev, tmp) { |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8087 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8088 | /* Force a switch to readonly even array |
| 8089 | * appears to still be in use. Hence |
| 8090 | * the '100'. |
| 8091 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 8092 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8093 | mddev_unlock(mddev); |
| 8094 | } |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8095 | need_delay = 1; |
| 8096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8097 | /* |
| 8098 | * certain more exotic SCSI devices are known to be |
| 8099 | * volatile wrt too early system reboots. While the |
| 8100 | * right place to handle this issue is the given |
| 8101 | * driver, we do want to have a safe RAID driver ... |
| 8102 | */ |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8103 | if (need_delay) |
| 8104 | mdelay(1000*1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8105 | } |
| 8106 | return NOTIFY_DONE; |
| 8107 | } |
| 8108 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8109 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8110 | .notifier_call = md_notify_reboot, |
| 8111 | .next = NULL, |
| 8112 | .priority = INT_MAX, /* before any real devices */ |
| 8113 | }; |
| 8114 | |
| 8115 | static void md_geninit(void) |
| 8116 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8117 | 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] | 8118 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8119 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8120 | } |
| 8121 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8122 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8123 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8124 | int ret = -ENOMEM; |
| 8125 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8126 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8127 | if (!md_wq) |
| 8128 | goto err_wq; |
| 8129 | |
| 8130 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8131 | if (!md_misc_wq) |
| 8132 | goto err_misc_wq; |
| 8133 | |
| 8134 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8135 | goto err_md; |
| 8136 | |
| 8137 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8138 | goto err_mdp; |
| 8139 | mdp_major = ret; |
| 8140 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8141 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8142 | md_probe, NULL, NULL); |
| 8143 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8144 | md_probe, NULL, NULL); |
| 8145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8146 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8147 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8148 | |
| 8149 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8151 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8152 | err_mdp: |
| 8153 | unregister_blkdev(MD_MAJOR, "md"); |
| 8154 | err_md: |
| 8155 | destroy_workqueue(md_misc_wq); |
| 8156 | err_misc_wq: |
| 8157 | destroy_workqueue(md_wq); |
| 8158 | err_wq: |
| 8159 | return ret; |
| 8160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | |
| 8162 | #ifndef MODULE |
| 8163 | |
| 8164 | /* |
| 8165 | * Searches all registered partitions for autorun RAID arrays |
| 8166 | * at boot time. |
| 8167 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8168 | |
| 8169 | static LIST_HEAD(all_detected_devices); |
| 8170 | struct detected_devices_node { |
| 8171 | struct list_head list; |
| 8172 | dev_t dev; |
| 8173 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | |
| 8175 | void md_autodetect_dev(dev_t dev) |
| 8176 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8177 | struct detected_devices_node *node_detected_dev; |
| 8178 | |
| 8179 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8180 | if (node_detected_dev) { |
| 8181 | node_detected_dev->dev = dev; |
| 8182 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8183 | } else { |
| 8184 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8185 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8187 | } |
| 8188 | |
| 8189 | |
| 8190 | static void autostart_arrays(int part) |
| 8191 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8192 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8193 | struct detected_devices_node *node_detected_dev; |
| 8194 | dev_t dev; |
| 8195 | int i_scanned, i_passed; |
| 8196 | |
| 8197 | i_scanned = 0; |
| 8198 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8199 | |
| 8200 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8201 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8202 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8203 | i_scanned++; |
| 8204 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8205 | struct detected_devices_node, list); |
| 8206 | list_del(&node_detected_dev->list); |
| 8207 | dev = node_detected_dev->dev; |
| 8208 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8209 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | if (IS_ERR(rdev)) |
| 8211 | continue; |
| 8212 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8213 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8214 | MD_BUG(); |
| 8215 | continue; |
| 8216 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8217 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8218 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8219 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8220 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8221 | |
| 8222 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8223 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8224 | |
| 8225 | autorun_devices(part); |
| 8226 | } |
| 8227 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8228 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8229 | |
| 8230 | static __exit void md_exit(void) |
| 8231 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8232 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8233 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8234 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8235 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8236 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8237 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8238 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | unregister_blkdev(mdp_major, "mdp"); |
| 8240 | unregister_reboot_notifier(&md_notifier); |
| 8241 | unregister_sysctl_table(raid_table_header); |
| 8242 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8243 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8245 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8246 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8247 | destroy_workqueue(md_misc_wq); |
| 8248 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | } |
| 8250 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8251 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8252 | module_exit(md_exit) |
| 8253 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8254 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8255 | { |
| 8256 | return sprintf(buffer, "%d", start_readonly); |
| 8257 | } |
| 8258 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8259 | { |
| 8260 | char *e; |
| 8261 | int num = simple_strtoul(val, &e, 10); |
| 8262 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8263 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8264 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8265 | } |
| 8266 | return -EINVAL; |
| 8267 | } |
| 8268 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8269 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8270 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8271 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8272 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8274 | EXPORT_SYMBOL(register_md_personality); |
| 8275 | EXPORT_SYMBOL(unregister_md_personality); |
| 8276 | EXPORT_SYMBOL(md_error); |
| 8277 | EXPORT_SYMBOL(md_done_sync); |
| 8278 | EXPORT_SYMBOL(md_write_start); |
| 8279 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8280 | EXPORT_SYMBOL(md_register_thread); |
| 8281 | EXPORT_SYMBOL(md_unregister_thread); |
| 8282 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8283 | EXPORT_SYMBOL(md_check_recovery); |
| 8284 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8285 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8286 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8287 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |