Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 61 | /* pers_list is a list of registered personalities protected |
| 62 | * by pers_lock. |
| 63 | * pers_lock does extra service to protect accesses to |
| 64 | * mddev->thread when the mutex cannot be held. |
| 65 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 66 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(pers_lock); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | static void md_print_devices(void); |
| 70 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 72 | static struct workqueue_struct *md_wq; |
| 73 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 74 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 75 | static int remove_and_add_spares(struct mddev *mddev, |
| 76 | struct md_rdev *this); |
| 77 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 78 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 81 | * Default number of read corrections we'll attempt on an rdev |
| 82 | * before ejecting it from the array. We divide the read error |
| 83 | * count by 2 for every hour elapsed between read errors. |
| 84 | */ |
| 85 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 86 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 88 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 89 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 90 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 92 | * speed limit - in case reconstruction slows down your system despite |
| 93 | * idle IO detection. |
| 94 | * |
| 95 | * 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] | 96 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | */ |
| 98 | |
| 99 | static int sysctl_speed_limit_min = 1000; |
| 100 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 101 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 102 | { |
| 103 | return mddev->sync_speed_min ? |
| 104 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 105 | } |
| 106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 107 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 108 | { |
| 109 | return mddev->sync_speed_max ? |
| 110 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | static struct ctl_table_header *raid_table_header; |
| 114 | |
| 115 | static ctl_table raid_table[] = { |
| 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .procname = "speed_limit_min", |
| 118 | .data = &sysctl_speed_limit_min, |
| 119 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 120 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 121 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }, |
| 123 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .procname = "speed_limit_max", |
| 125 | .data = &sysctl_speed_limit_max, |
| 126 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 127 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 128 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 130 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static ctl_table raid_dir_table[] = { |
| 134 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .procname = "raid", |
| 136 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 137 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .child = raid_table, |
| 139 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 140 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | static ctl_table raid_root_table[] = { |
| 144 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | .procname = "dev", |
| 146 | .maxlen = 0, |
| 147 | .mode = 0555, |
| 148 | .child = raid_dir_table, |
| 149 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 150 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 153 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 155 | static int start_readonly; |
| 156 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 157 | /* bio_clone_mddev |
| 158 | * like bio_clone, but with a local bio set |
| 159 | */ |
| 160 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 162 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 163 | { |
| 164 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 165 | |
| 166 | if (!mddev || !mddev->bio_set) |
| 167 | return bio_alloc(gfp_mask, nr_iovecs); |
| 168 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 169 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 170 | if (!b) |
| 171 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 172 | return b; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 175 | |
| 176 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 177 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 178 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 179 | if (!mddev || !mddev->bio_set) |
| 180 | return bio_clone(bio, gfp_mask); |
| 181 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 182 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 183 | } |
| 184 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 185 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 186 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 187 | { |
| 188 | /* 'bio' is a cloned bio which we need to trim to match |
| 189 | * the given offset and size. |
| 190 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 191 | */ |
| 192 | int i; |
| 193 | struct bio_vec *bvec; |
| 194 | int sofar = 0; |
| 195 | |
| 196 | size <<= 9; |
| 197 | if (offset == 0 && size == bio->bi_size) |
| 198 | return; |
| 199 | |
| 200 | bio->bi_sector += offset; |
| 201 | bio->bi_size = size; |
| 202 | offset <<= 9; |
| 203 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 204 | |
| 205 | while (bio->bi_idx < bio->bi_vcnt && |
| 206 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 207 | /* remove this whole bio_vec */ |
| 208 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 209 | bio->bi_idx++; |
| 210 | } |
| 211 | if (bio->bi_idx < bio->bi_vcnt) { |
| 212 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 213 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 214 | } |
| 215 | /* avoid any complications with bi_idx being non-zero*/ |
| 216 | if (bio->bi_idx) { |
| 217 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 218 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 219 | bio->bi_vcnt -= bio->bi_idx; |
| 220 | bio->bi_idx = 0; |
| 221 | } |
| 222 | /* Make sure vcnt and last bv are not too big */ |
| 223 | bio_for_each_segment(bvec, bio, i) { |
| 224 | if (sofar + bvec->bv_len > size) |
| 225 | bvec->bv_len = size - sofar; |
| 226 | if (bvec->bv_len == 0) { |
| 227 | bio->bi_vcnt = i; |
| 228 | break; |
| 229 | } |
| 230 | sofar += bvec->bv_len; |
| 231 | } |
| 232 | } |
| 233 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 236 | * We have a system wide 'event count' that is incremented |
| 237 | * on any 'interesting' event, and readers of /proc/mdstat |
| 238 | * can use 'poll' or 'select' to find out when the event |
| 239 | * count increases. |
| 240 | * |
| 241 | * Events are: |
| 242 | * start array, stop array, error, add device, remove device, |
| 243 | * start build, activate spare |
| 244 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 245 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 246 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 247 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 248 | { |
| 249 | atomic_inc(&md_event_count); |
| 250 | wake_up(&md_event_waiters); |
| 251 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 253 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 254 | /* Alternate version that can be called from interrupts |
| 255 | * when calling sysfs_notify isn't needed. |
| 256 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 257 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 258 | { |
| 259 | atomic_inc(&md_event_count); |
| 260 | wake_up(&md_event_waiters); |
| 261 | } |
| 262 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 263 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | * Enables to iterate over all existing md arrays |
| 265 | * all_mddevs_lock protects this list. |
| 266 | */ |
| 267 | static LIST_HEAD(all_mddevs); |
| 268 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * iterates through all used mddevs in the system. |
| 273 | * We take care to grab the all_mddevs_lock whenever navigating |
| 274 | * the list, and to always hold a refcount when unlocked. |
| 275 | * Any code which breaks out of this loop while own |
| 276 | * a reference to the current mddev and must mddev_put it. |
| 277 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 278 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | \ |
| 280 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 281 | _tmp = all_mddevs.next; \ |
| 282 | _mddev = NULL;}); \ |
| 283 | ({ if (_tmp != &all_mddevs) \ |
| 284 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 286 | if (_mddev) mddev_put(_mddev); \ |
| 287 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 288 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 290 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | ) |
| 292 | |
| 293 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 294 | /* Rather than calling directly into the personality make_request function, |
| 295 | * IO requests come here first so that we can check if the device is |
| 296 | * being suspended pending a reconfiguration. |
| 297 | * We hold a refcount over the call to ->make_request. By the time that |
| 298 | * call has finished, the bio has been linked into some internal structure |
| 299 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 300 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 301 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 303 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 304 | struct mddev *mddev = q->queuedata; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 305 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 306 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 307 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 308 | if (mddev == NULL || mddev->pers == NULL |
| 309 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 310 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 311 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 312 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 313 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 314 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 315 | return; |
| 316 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 317 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 319 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | DEFINE_WAIT(__wait); |
| 321 | for (;;) { |
| 322 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 323 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 324 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 325 | break; |
| 326 | rcu_read_unlock(); |
| 327 | schedule(); |
| 328 | rcu_read_lock(); |
| 329 | } |
| 330 | finish_wait(&mddev->sb_wait, &__wait); |
| 331 | } |
| 332 | atomic_inc(&mddev->active_io); |
| 333 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 334 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 335 | /* |
| 336 | * save the sectors now since our bio can |
| 337 | * go away inside make_request |
| 338 | */ |
| 339 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 340 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 341 | |
| 342 | cpu = part_stat_lock(); |
| 343 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 344 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 345 | part_stat_unlock(); |
| 346 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 347 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 348 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 351 | /* mddev_suspend makes sure no new requests are submitted |
| 352 | * to the device, and that any requests that have been submitted |
| 353 | * are completely handled. |
| 354 | * Once ->stop is called and completes, the module will be completely |
| 355 | * unused. |
| 356 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 357 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 358 | { |
| 359 | BUG_ON(mddev->suspended); |
| 360 | mddev->suspended = 1; |
| 361 | synchronize_rcu(); |
| 362 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 363 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 364 | |
| 365 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 366 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 368 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 369 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 370 | { |
| 371 | mddev->suspended = 0; |
| 372 | wake_up(&mddev->sb_wait); |
| 373 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 374 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 375 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 376 | md_wakeup_thread(mddev->thread); |
| 377 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 378 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 379 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 380 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 381 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 382 | { |
| 383 | return mddev->suspended; |
| 384 | } |
| 385 | EXPORT_SYMBOL(mddev_congested); |
| 386 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 388 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | */ |
| 390 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 391 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 392 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 393 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 394 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | |
| 396 | rdev_dec_pending(rdev, mddev); |
| 397 | |
| 398 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 399 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 400 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | } |
| 402 | bio_put(bio); |
| 403 | } |
| 404 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 405 | static void md_submit_flush_data(struct work_struct *ws); |
| 406 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 407 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 409 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 410 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 412 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 413 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 415 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | if (rdev->raid_disk >= 0 && |
| 417 | !test_bit(Faulty, &rdev->flags)) { |
| 418 | /* Take two references, one is dropped |
| 419 | * when request finishes, one after |
| 420 | * we reclaim rcu_read_lock |
| 421 | */ |
| 422 | struct bio *bi; |
| 423 | atomic_inc(&rdev->nr_pending); |
| 424 | atomic_inc(&rdev->nr_pending); |
| 425 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 426 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 427 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | bi->bi_private = rdev; |
| 429 | bi->bi_bdev = rdev->bdev; |
| 430 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 431 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | rcu_read_lock(); |
| 433 | rdev_dec_pending(rdev, mddev); |
| 434 | } |
| 435 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 436 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 437 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | } |
| 439 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 440 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 442 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 443 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 445 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 446 | /* an empty barrier - all done */ |
| 447 | bio_endio(bio, 0); |
| 448 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 449 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 450 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 451 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 452 | |
| 453 | mddev->flush_bio = NULL; |
| 454 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 455 | } |
| 456 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 457 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 458 | { |
| 459 | spin_lock_irq(&mddev->write_lock); |
| 460 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 461 | !mddev->flush_bio, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 462 | mddev->write_lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 463 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 464 | spin_unlock_irq(&mddev->write_lock); |
| 465 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 466 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 467 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 468 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 469 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 470 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 471 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 472 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 473 | struct mddev *mddev = cb->data; |
| 474 | md_wakeup_thread(mddev->thread); |
| 475 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 476 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 477 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 478 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 479 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | atomic_inc(&mddev->active); |
| 482 | return mddev; |
| 483 | } |
| 484 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 485 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 486 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 487 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 489 | struct bio_set *bs = NULL; |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 492 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 493 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 494 | mddev->ctime == 0 && !mddev->hold_active) { |
| 495 | /* Array is not configured at all, and not held active, |
| 496 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 497 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 498 | bs = mddev->bio_set; |
| 499 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 500 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 501 | /* We did a probe so need to clean up. Call |
| 502 | * queue_work inside the spinlock so that |
| 503 | * flush_workqueue() after mddev_find will |
| 504 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 505 | */ |
| 506 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 507 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 508 | } else |
| 509 | kfree(mddev); |
| 510 | } |
| 511 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 512 | if (bs) |
| 513 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 516 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 517 | { |
| 518 | mutex_init(&mddev->open_mutex); |
| 519 | mutex_init(&mddev->reconfig_mutex); |
| 520 | mutex_init(&mddev->bitmap_info.mutex); |
| 521 | INIT_LIST_HEAD(&mddev->disks); |
| 522 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 523 | init_timer(&mddev->safemode_timer); |
| 524 | atomic_set(&mddev->active, 1); |
| 525 | atomic_set(&mddev->openers, 0); |
| 526 | atomic_set(&mddev->active_io, 0); |
| 527 | spin_lock_init(&mddev->write_lock); |
| 528 | atomic_set(&mddev->flush_pending, 0); |
| 529 | init_waitqueue_head(&mddev->sb_wait); |
| 530 | init_waitqueue_head(&mddev->recovery_wait); |
| 531 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 532 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 533 | mddev->resync_min = 0; |
| 534 | mddev->resync_max = MaxSector; |
| 535 | mddev->level = LEVEL_NONE; |
| 536 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 537 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 538 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 539 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 541 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 543 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 544 | unit &= ~((1<<MdpMinorShift)-1); |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | retry: |
| 547 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 549 | if (unit) { |
| 550 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 551 | if (mddev->unit == unit) { |
| 552 | mddev_get(mddev); |
| 553 | spin_unlock(&all_mddevs_lock); |
| 554 | kfree(new); |
| 555 | return mddev; |
| 556 | } |
| 557 | |
| 558 | if (new) { |
| 559 | list_add(&new->all_mddevs, &all_mddevs); |
| 560 | spin_unlock(&all_mddevs_lock); |
| 561 | new->hold_active = UNTIL_IOCTL; |
| 562 | return new; |
| 563 | } |
| 564 | } else if (new) { |
| 565 | /* find an unused unit number */ |
| 566 | static int next_minor = 512; |
| 567 | int start = next_minor; |
| 568 | int is_free = 0; |
| 569 | int dev = 0; |
| 570 | while (!is_free) { |
| 571 | dev = MKDEV(MD_MAJOR, next_minor); |
| 572 | next_minor++; |
| 573 | if (next_minor > MINORMASK) |
| 574 | next_minor = 0; |
| 575 | if (next_minor == start) { |
| 576 | /* Oh dear, all in use. */ |
| 577 | spin_unlock(&all_mddevs_lock); |
| 578 | kfree(new); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | is_free = 1; |
| 583 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 584 | if (mddev->unit == dev) { |
| 585 | is_free = 0; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | new->unit = dev; |
| 590 | new->md_minor = MINOR(dev); |
| 591 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | list_add(&new->all_mddevs, &all_mddevs); |
| 593 | spin_unlock(&all_mddevs_lock); |
| 594 | return new; |
| 595 | } |
| 596 | spin_unlock(&all_mddevs_lock); |
| 597 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 598 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (!new) |
| 600 | return NULL; |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | new->unit = unit; |
| 603 | if (MAJOR(unit) == MD_MAJOR) |
| 604 | new->md_minor = MINOR(unit); |
| 605 | else |
| 606 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 607 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 608 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | goto retry; |
| 611 | } |
| 612 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 613 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 615 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 618 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 619 | { |
| 620 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 621 | } |
| 622 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 623 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 625 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 628 | static struct attribute_group md_redundancy_group; |
| 629 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 630 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 632 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 633 | /* These cannot be removed under reconfig_mutex as |
| 634 | * an access to the files will try to take reconfig_mutex |
| 635 | * while holding the file unremovable, which leads to |
| 636 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 637 | * So hold set sysfs_active while the remove in happeing, |
| 638 | * and anything else which might set ->to_remove or my |
| 639 | * otherwise change the sysfs namespace will fail with |
| 640 | * -EBUSY if sysfs_active is still set. |
| 641 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 642 | * test it under the same mutex to ensure its correct value |
| 643 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 644 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 645 | struct attribute_group *to_remove = mddev->to_remove; |
| 646 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 647 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 648 | mutex_unlock(&mddev->reconfig_mutex); |
| 649 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 650 | if (mddev->kobj.sd) { |
| 651 | if (to_remove != &md_redundancy_group) |
| 652 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 653 | if (mddev->pers == NULL || |
| 654 | mddev->pers->sync_request == NULL) { |
| 655 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 656 | if (mddev->sysfs_action) |
| 657 | sysfs_put(mddev->sysfs_action); |
| 658 | mddev->sysfs_action = NULL; |
| 659 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 660 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 661 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 662 | } else |
| 663 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 665 | /* As we've dropped the mutex we need a spinlock to |
| 666 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 667 | */ |
| 668 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 669 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 670 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 673 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 675 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 677 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (rdev->desc_nr == nr) |
| 679 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return NULL; |
| 682 | } |
| 683 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 684 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 685 | { |
| 686 | struct md_rdev *rdev; |
| 687 | |
| 688 | rdev_for_each_rcu(rdev, mddev) |
| 689 | if (rdev->desc_nr == nr) |
| 690 | return rdev; |
| 691 | |
| 692 | return NULL; |
| 693 | } |
| 694 | |
| 695 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 697 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 699 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | if (rdev->bdev->bd_dev == dev) |
| 701 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | return NULL; |
| 704 | } |
| 705 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 706 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 707 | { |
| 708 | struct md_rdev *rdev; |
| 709 | |
| 710 | rdev_for_each_rcu(rdev, mddev) |
| 711 | if (rdev->bdev->bd_dev == dev) |
| 712 | return rdev; |
| 713 | |
| 714 | return NULL; |
| 715 | } |
| 716 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 717 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 718 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 719 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 720 | list_for_each_entry(pers, &pers_list, list) { |
| 721 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 722 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 723 | if (strcmp(pers->name, clevel)==0) |
| 724 | return pers; |
| 725 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 726 | return NULL; |
| 727 | } |
| 728 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 729 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 730 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 732 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 733 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 736 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | if (rdev->sb_page) |
| 739 | MD_BUG(); |
| 740 | |
| 741 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 742 | if (!rdev->sb_page) { |
| 743 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 744 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 750 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
| 752 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 753 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | rdev->sb_loaded = 0; |
| 755 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 756 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 757 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 759 | if (rdev->bb_page) { |
| 760 | put_page(rdev->bb_page); |
| 761 | rdev->bb_page = NULL; |
| 762 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 763 | kfree(rdev->badblocks.page); |
| 764 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 766 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 768 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 769 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 770 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 771 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 772 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 773 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 774 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 775 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 776 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 777 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 778 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 779 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 780 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 781 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 782 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 783 | } |
| 784 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 785 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 786 | sector_t sector, int size, struct page *page) |
| 787 | { |
| 788 | /* write first size bytes of page to sector of rdev |
| 789 | * Increment mddev->pending_writes before returning |
| 790 | * and decrement it on completion, waking up sb_wait |
| 791 | * if zero is reached. |
| 792 | * If an error occurred, call md_error |
| 793 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 794 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 795 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 796 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 797 | bio->bi_sector = sector; |
| 798 | bio_add_page(bio, page, size, 0); |
| 799 | bio->bi_private = rdev; |
| 800 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 801 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 802 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 803 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 804 | } |
| 805 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 806 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 807 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 808 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 809 | DEFINE_WAIT(wq); |
| 810 | for(;;) { |
| 811 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 812 | if (atomic_read(&mddev->pending_writes)==0) |
| 813 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 814 | schedule(); |
| 815 | } |
| 816 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 817 | } |
| 818 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 819 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 824 | 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] | 825 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 827 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | struct completion event; |
| 829 | int ret; |
| 830 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 831 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 833 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 834 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 835 | if (metadata_op) |
| 836 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 837 | else if (rdev->mddev->reshape_position != MaxSector && |
| 838 | (rdev->mddev->reshape_backwards == |
| 839 | (sector >= rdev->mddev->reshape_position))) |
| 840 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 841 | else |
| 842 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | bio_add_page(bio, page, size, 0); |
| 844 | init_completion(&event); |
| 845 | bio->bi_private = &event; |
| 846 | bio->bi_end_io = bi_complete; |
| 847 | submit_bio(rw, bio); |
| 848 | wait_for_completion(&event); |
| 849 | |
| 850 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 851 | bio_put(bio); |
| 852 | return ret; |
| 853 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 854 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 856 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
| 858 | char b[BDEVNAME_SIZE]; |
| 859 | if (!rdev->sb_page) { |
| 860 | MD_BUG(); |
| 861 | return -EINVAL; |
| 862 | } |
| 863 | if (rdev->sb_loaded) |
| 864 | return 0; |
| 865 | |
| 866 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 867 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | goto fail; |
| 869 | rdev->sb_loaded = 1; |
| 870 | return 0; |
| 871 | |
| 872 | fail: |
| 873 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 874 | bdevname(rdev->bdev,b)); |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | |
| 878 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 879 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 880 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 881 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 882 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 883 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 887 | { |
| 888 | int ret; |
| 889 | mdp_super_t *tmp1, *tmp2; |
| 890 | |
| 891 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 892 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 893 | |
| 894 | if (!tmp1 || !tmp2) { |
| 895 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 896 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | goto abort; |
| 898 | } |
| 899 | |
| 900 | *tmp1 = *sb1; |
| 901 | *tmp2 = *sb2; |
| 902 | |
| 903 | /* |
| 904 | * nr_disks is not constant |
| 905 | */ |
| 906 | tmp1->nr_disks = 0; |
| 907 | tmp2->nr_disks = 0; |
| 908 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 909 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 911 | kfree(tmp1); |
| 912 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | return ret; |
| 914 | } |
| 915 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 916 | |
| 917 | static u32 md_csum_fold(u32 csum) |
| 918 | { |
| 919 | csum = (csum & 0xffff) + (csum >> 16); |
| 920 | return (csum & 0xffff) + (csum >> 16); |
| 921 | } |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 924 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 925 | u64 newcsum = 0; |
| 926 | u32 *sb32 = (u32*)sb; |
| 927 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | unsigned int disk_csum, csum; |
| 929 | |
| 930 | disk_csum = sb->sb_csum; |
| 931 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 932 | |
| 933 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 934 | newcsum += sb32[i]; |
| 935 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 936 | |
| 937 | |
| 938 | #ifdef CONFIG_ALPHA |
| 939 | /* This used to use csum_partial, which was wrong for several |
| 940 | * reasons including that different results are returned on |
| 941 | * different architectures. It isn't critical that we get exactly |
| 942 | * the same return value as before (we always csum_fold before |
| 943 | * testing, and that removes any differences). However as we |
| 944 | * know that csum_partial always returned a 16bit value on |
| 945 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 946 | */ |
| 947 | sb->sb_csum = md_csum_fold(disk_csum); |
| 948 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 950 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | return csum; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* |
| 956 | * Handle superblock details. |
| 957 | * We want to be able to handle multiple superblock formats |
| 958 | * so we have a common interface to them all, and an array of |
| 959 | * different handlers. |
| 960 | * We rely on user-space to write the initial superblock, and support |
| 961 | * reading and updating of superblocks. |
| 962 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 963 | * 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] | 964 | * loads and validates a superblock on dev. |
| 965 | * if refdev != NULL, compare superblocks on both devices |
| 966 | * Return: |
| 967 | * 0 - dev has a superblock that is compatible with refdev |
| 968 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 969 | * so dev should be used as the refdev in future |
| 970 | * -EINVAL superblock incompatible or invalid |
| 971 | * -othererror e.g. -EIO |
| 972 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 973 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | * Verify that dev is acceptable into mddev. |
| 975 | * The first time, mddev->raid_disks will be 0, and data from |
| 976 | * dev should be merged in. Subsequent calls check that dev |
| 977 | * is new enough. Return 0 or -EINVAL |
| 978 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 979 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * Update the superblock for rdev with data in mddev |
| 981 | * This does not write to disc. |
| 982 | * |
| 983 | */ |
| 984 | |
| 985 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 986 | char *name; |
| 987 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 988 | int (*load_super)(struct md_rdev *rdev, |
| 989 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 990 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 991 | int (*validate_super)(struct mddev *mddev, |
| 992 | struct md_rdev *rdev); |
| 993 | void (*sync_super)(struct mddev *mddev, |
| 994 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 995 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 996 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 997 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 998 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | }; |
| 1000 | |
| 1001 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1002 | * Check that the given mddev has no bitmap. |
| 1003 | * |
| 1004 | * This function is called from the run method of all personalities that do not |
| 1005 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1006 | * has a bitmap. Otherwise, it returns 0. |
| 1007 | * |
| 1008 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1009 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1010 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1011 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1012 | return 0; |
| 1013 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1014 | mdname(mddev), mddev->pers->name); |
| 1015 | return 1; |
| 1016 | } |
| 1017 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1018 | |
| 1019 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | * load_super for 0.90.0 |
| 1021 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1022 | 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] | 1023 | { |
| 1024 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1025 | mdp_super_t *sb; |
| 1026 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1029 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | * it's at the end of the disk. |
| 1031 | * |
| 1032 | * It also happens to be a multiple of 4Kb. |
| 1033 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1034 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1036 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | if (ret) return ret; |
| 1038 | |
| 1039 | ret = -EINVAL; |
| 1040 | |
| 1041 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1042 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1045 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1046 | b); |
| 1047 | goto abort; |
| 1048 | } |
| 1049 | |
| 1050 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1051 | sb->minor_version < 90 || |
| 1052 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1054 | sb->major_version, sb->minor_version, |
| 1055 | b); |
| 1056 | goto abort; |
| 1057 | } |
| 1058 | |
| 1059 | if (sb->raid_disks <= 0) |
| 1060 | goto abort; |
| 1061 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1062 | 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] | 1063 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1064 | b); |
| 1065 | goto abort; |
| 1066 | } |
| 1067 | |
| 1068 | rdev->preferred_minor = sb->md_minor; |
| 1069 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1070 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1071 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1072 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | if (sb->level == LEVEL_MULTIPATH) |
| 1075 | rdev->desc_nr = -1; |
| 1076 | else |
| 1077 | rdev->desc_nr = sb->this_disk.number; |
| 1078 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1079 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1081 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1083 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (!uuid_equal(refsb, sb)) { |
| 1085 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1086 | b, bdevname(refdev->bdev,b2)); |
| 1087 | goto abort; |
| 1088 | } |
| 1089 | if (!sb_equal(refsb, sb)) { |
| 1090 | printk(KERN_WARNING "md: %s has same UUID" |
| 1091 | " but different superblock to %s\n", |
| 1092 | b, bdevname(refdev->bdev, b2)); |
| 1093 | goto abort; |
| 1094 | } |
| 1095 | ev1 = md_event(sb); |
| 1096 | ev2 = md_event(refsb); |
| 1097 | if (ev1 > ev2) |
| 1098 | ret = 1; |
| 1099 | else |
| 1100 | ret = 0; |
| 1101 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1102 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1103 | /* Limit to 4TB as metadata cannot record more than that. |
| 1104 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1105 | * record this size) |
| 1106 | */ |
| 1107 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1108 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1110 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1111 | /* "this cannot possibly happen" ... */ |
| 1112 | ret = -EINVAL; |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | abort: |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * validate_super for 0.90.0 |
| 1120 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1121 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | { |
| 1123 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1124 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1125 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1127 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1128 | clear_bit(Faulty, &rdev->flags); |
| 1129 | clear_bit(In_sync, &rdev->flags); |
| 1130 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | if (mddev->raid_disks == 0) { |
| 1133 | mddev->major_version = 0; |
| 1134 | mddev->minor_version = sb->minor_version; |
| 1135 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1136 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1137 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | mddev->ctime = sb->ctime; |
| 1139 | mddev->utime = sb->utime; |
| 1140 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1141 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | mddev->layout = sb->layout; |
| 1143 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1144 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1145 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1146 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1147 | mddev->bitmap_info.space = 0; |
| 1148 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1149 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1150 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1151 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1153 | if (mddev->minor_version >= 91) { |
| 1154 | mddev->reshape_position = sb->reshape_position; |
| 1155 | mddev->delta_disks = sb->delta_disks; |
| 1156 | mddev->new_level = sb->new_level; |
| 1157 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1158 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1159 | if (mddev->delta_disks < 0) |
| 1160 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1161 | } else { |
| 1162 | mddev->reshape_position = MaxSector; |
| 1163 | mddev->delta_disks = 0; |
| 1164 | mddev->new_level = mddev->level; |
| 1165 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1166 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1170 | mddev->recovery_cp = MaxSector; |
| 1171 | else { |
| 1172 | if (sb->events_hi == sb->cp_events_hi && |
| 1173 | sb->events_lo == sb->cp_events_lo) { |
| 1174 | mddev->recovery_cp = sb->recovery_cp; |
| 1175 | } else |
| 1176 | mddev->recovery_cp = 0; |
| 1177 | } |
| 1178 | |
| 1179 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1180 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1181 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1182 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1183 | |
| 1184 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1187 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1188 | mddev->bitmap_info.offset = |
| 1189 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1190 | mddev->bitmap_info.space = |
| 1191 | mddev->bitmap_info.space; |
| 1192 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1193 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1194 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1195 | /* Insist on good event counter while assembling, except |
| 1196 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1198 | if (sb->disks[rdev->desc_nr].state & ( |
| 1199 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1200 | if (ev1 < mddev->events) |
| 1201 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1202 | } else if (mddev->bitmap) { |
| 1203 | /* if adding to array with a bitmap, then we can accept an |
| 1204 | * older device ... but not too old. |
| 1205 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1206 | if (ev1 < mddev->bitmap->events_cleared) |
| 1207 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1208 | } else { |
| 1209 | if (ev1 < mddev->events) |
| 1210 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1211 | return 0; |
| 1212 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | desc = sb->disks + rdev->desc_nr; |
| 1216 | |
| 1217 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1218 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1219 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1220 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1221 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1223 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1224 | /* active but not in sync implies recovery up to |
| 1225 | * reshape position. We don't know exactly where |
| 1226 | * that is, so set to zero for now */ |
| 1227 | if (mddev->minor_version >= 91) { |
| 1228 | rdev->recovery_offset = 0; |
| 1229 | rdev->raid_disk = desc->raid_disk; |
| 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1232 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1233 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1234 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1235 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * sync_super for 0.90.0 |
| 1241 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1242 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
| 1244 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1245 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | |
| 1249 | /* make rdev->sb match mddev data.. |
| 1250 | * |
| 1251 | * 1/ zero out disks |
| 1252 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1253 | * 3/ any empty disks < next_spare become removed |
| 1254 | * |
| 1255 | * disks[0] gets initialised to REMOVED because |
| 1256 | * we cannot be sure from other fields if it has |
| 1257 | * been initialised or not. |
| 1258 | */ |
| 1259 | int i; |
| 1260 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1261 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1262 | rdev->sb_size = MD_SB_BYTES; |
| 1263 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1264 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
| 1266 | memset(sb, 0, sizeof(*sb)); |
| 1267 | |
| 1268 | sb->md_magic = MD_SB_MAGIC; |
| 1269 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | sb->patch_version = mddev->patch_version; |
| 1271 | sb->gvalid_words = 0; /* ignored */ |
| 1272 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1273 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1274 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1275 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1276 | |
| 1277 | sb->ctime = mddev->ctime; |
| 1278 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1279 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | sb->raid_disks = mddev->raid_disks; |
| 1281 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1282 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | sb->utime = mddev->utime; |
| 1284 | sb->state = 0; |
| 1285 | sb->events_hi = (mddev->events>>32); |
| 1286 | sb->events_lo = (u32)mddev->events; |
| 1287 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1288 | if (mddev->reshape_position == MaxSector) |
| 1289 | sb->minor_version = 90; |
| 1290 | else { |
| 1291 | sb->minor_version = 91; |
| 1292 | sb->reshape_position = mddev->reshape_position; |
| 1293 | sb->new_level = mddev->new_level; |
| 1294 | sb->delta_disks = mddev->delta_disks; |
| 1295 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1296 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1297 | } |
| 1298 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | if (mddev->in_sync) |
| 1300 | { |
| 1301 | sb->recovery_cp = mddev->recovery_cp; |
| 1302 | sb->cp_events_hi = (mddev->events>>32); |
| 1303 | sb->cp_events_lo = (u32)mddev->events; |
| 1304 | if (mddev->recovery_cp == MaxSector) |
| 1305 | sb->state = (1<< MD_SB_CLEAN); |
| 1306 | } else |
| 1307 | sb->recovery_cp = 0; |
| 1308 | |
| 1309 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1310 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1312 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1313 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1316 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1318 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1319 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1320 | |
| 1321 | if (rdev2->raid_disk >= 0 && |
| 1322 | sb->minor_version >= 91) |
| 1323 | /* we have nowhere to store the recovery_offset, |
| 1324 | * but if it is not below the reshape_position, |
| 1325 | * we can piggy-back on that. |
| 1326 | */ |
| 1327 | is_active = 1; |
| 1328 | if (rdev2->raid_disk < 0 || |
| 1329 | test_bit(Faulty, &rdev2->flags)) |
| 1330 | is_active = 0; |
| 1331 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1332 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1334 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1335 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | d = &sb->disks[rdev2->desc_nr]; |
| 1337 | nr_disks++; |
| 1338 | d->number = rdev2->desc_nr; |
| 1339 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1340 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1341 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | d->raid_disk = rdev2->raid_disk; |
| 1343 | else |
| 1344 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1345 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1347 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1349 | if (test_bit(In_sync, &rdev2->flags)) |
| 1350 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | active++; |
| 1352 | working++; |
| 1353 | } else { |
| 1354 | d->state = 0; |
| 1355 | spare++; |
| 1356 | working++; |
| 1357 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1358 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1359 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1362 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1363 | mdp_disk_t *d = &sb->disks[i]; |
| 1364 | if (d->state == 0 && d->number == 0) { |
| 1365 | d->number = i; |
| 1366 | d->raid_disk = i; |
| 1367 | d->state = (1<<MD_DISK_REMOVED); |
| 1368 | d->state |= (1<<MD_DISK_FAULTY); |
| 1369 | failed++; |
| 1370 | } |
| 1371 | } |
| 1372 | sb->nr_disks = nr_disks; |
| 1373 | sb->active_disks = active; |
| 1374 | sb->working_disks = working; |
| 1375 | sb->failed_disks = failed; |
| 1376 | sb->spare_disks = spare; |
| 1377 | |
| 1378 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1379 | sb->sb_csum = calc_sb_csum(sb); |
| 1380 | } |
| 1381 | |
| 1382 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1383 | * rdev_size_change for 0.90.0 |
| 1384 | */ |
| 1385 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1386 | 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] | 1387 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1388 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1389 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1390 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1391 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1392 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1393 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1394 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1395 | /* Limit to 4TB as metadata cannot record more than that. |
| 1396 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1397 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1398 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1399 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1400 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1401 | rdev->sb_page); |
| 1402 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1403 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1404 | } |
| 1405 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1406 | static int |
| 1407 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1408 | { |
| 1409 | /* non-zero offset changes not possible with v0.90 */ |
| 1410 | return new_offset == 0; |
| 1411 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1412 | |
| 1413 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | * version 1 superblock |
| 1415 | */ |
| 1416 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1417 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1419 | __le32 disk_csum; |
| 1420 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | unsigned long long newcsum; |
| 1422 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1423 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | disk_csum = sb->sb_csum; |
| 1426 | sb->sb_csum = 0; |
| 1427 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1428 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | newcsum += le32_to_cpu(*isuper++); |
| 1430 | |
| 1431 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1432 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1435 | sb->sb_csum = disk_csum; |
| 1436 | return cpu_to_le32(csum); |
| 1437 | } |
| 1438 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1439 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1440 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1441 | 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] | 1442 | { |
| 1443 | struct mdp_superblock_1 *sb; |
| 1444 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1446 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1448 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
| 1450 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1451 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | * It is always aligned to a 4K boundary and |
| 1453 | * depeding on minor_version, it can be: |
| 1454 | * 0: At least 8K, but less than 12K, from end of device |
| 1455 | * 1: At start of device |
| 1456 | * 2: 4K from start of device. |
| 1457 | */ |
| 1458 | switch(minor_version) { |
| 1459 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1460 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1461 | sb_start -= 8*2; |
| 1462 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1465 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | break; |
| 1467 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1468 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | break; |
| 1470 | default: |
| 1471 | return -EINVAL; |
| 1472 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1473 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1475 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1476 | * and it is safe to read 4k, so we do that |
| 1477 | */ |
| 1478 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | if (ret) return ret; |
| 1480 | |
| 1481 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1482 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
| 1484 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1485 | sb->major_version != cpu_to_le32(1) || |
| 1486 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1487 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1488 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return -EINVAL; |
| 1490 | |
| 1491 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1492 | printk("md: invalid superblock checksum on %s\n", |
| 1493 | bdevname(rdev->bdev,b)); |
| 1494 | return -EINVAL; |
| 1495 | } |
| 1496 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1497 | printk("md: data_size too small on %s\n", |
| 1498 | bdevname(rdev->bdev,b)); |
| 1499 | return -EINVAL; |
| 1500 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1501 | if (sb->pad0 || |
| 1502 | sb->pad3[0] || |
| 1503 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1504 | /* Some padding is non-zero, might be a new feature */ |
| 1505 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | rdev->preferred_minor = 0xffff; |
| 1508 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1509 | rdev->new_data_offset = rdev->data_offset; |
| 1510 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1511 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1512 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1513 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1515 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1516 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1517 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1518 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1519 | |
| 1520 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1521 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1522 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1523 | if (minor_version |
| 1524 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1525 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1526 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1527 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1528 | rdev->desc_nr = -1; |
| 1529 | else |
| 1530 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1531 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1532 | if (!rdev->bb_page) { |
| 1533 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1534 | if (!rdev->bb_page) |
| 1535 | return -ENOMEM; |
| 1536 | } |
| 1537 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1538 | rdev->badblocks.count == 0) { |
| 1539 | /* need to load the bad block list. |
| 1540 | * Currently we limit it to one page. |
| 1541 | */ |
| 1542 | s32 offset; |
| 1543 | sector_t bb_sector; |
| 1544 | u64 *bbp; |
| 1545 | int i; |
| 1546 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1547 | if (sectors > (PAGE_SIZE / 512)) |
| 1548 | return -EINVAL; |
| 1549 | offset = le32_to_cpu(sb->bblog_offset); |
| 1550 | if (offset == 0) |
| 1551 | return -EINVAL; |
| 1552 | bb_sector = (long long)offset; |
| 1553 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1554 | rdev->bb_page, READ, true)) |
| 1555 | return -EIO; |
| 1556 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1557 | rdev->badblocks.shift = sb->bblog_shift; |
| 1558 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1559 | u64 bb = le64_to_cpu(*bbp); |
| 1560 | int count = bb & (0x3ff); |
| 1561 | u64 sector = bb >> 10; |
| 1562 | sector <<= sb->bblog_shift; |
| 1563 | count <<= sb->bblog_shift; |
| 1564 | if (bb + 1 == 0) |
| 1565 | break; |
| 1566 | if (md_set_badblocks(&rdev->badblocks, |
| 1567 | sector, count, 1) == 0) |
| 1568 | return -EINVAL; |
| 1569 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1570 | } else if (sb->bblog_offset != 0) |
| 1571 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1572 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1573 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1574 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1575 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1577 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1580 | sb->level != refsb->level || |
| 1581 | sb->layout != refsb->layout || |
| 1582 | sb->chunksize != refsb->chunksize) { |
| 1583 | printk(KERN_WARNING "md: %s has strangely different" |
| 1584 | " superblock to %s\n", |
| 1585 | bdevname(rdev->bdev,b), |
| 1586 | bdevname(refdev->bdev,b2)); |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | ev1 = le64_to_cpu(sb->events); |
| 1590 | ev2 = le64_to_cpu(refsb->events); |
| 1591 | |
| 1592 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1593 | ret = 1; |
| 1594 | else |
| 1595 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1597 | if (minor_version) { |
| 1598 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1599 | sectors -= rdev->data_offset; |
| 1600 | } else |
| 1601 | sectors = rdev->sb_start; |
| 1602 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1604 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1605 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1608 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1610 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1611 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1613 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1614 | clear_bit(Faulty, &rdev->flags); |
| 1615 | clear_bit(In_sync, &rdev->flags); |
| 1616 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | if (mddev->raid_disks == 0) { |
| 1619 | mddev->major_version = 1; |
| 1620 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1621 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1622 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1624 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1625 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1626 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | mddev->layout = le32_to_cpu(sb->layout); |
| 1628 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1629 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1630 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1631 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1632 | mddev->bitmap_info.space = 0; |
| 1633 | /* Default location for bitmap is 1K after superblock |
| 1634 | * using 3K - total of 4K |
| 1635 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1636 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1637 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1638 | mddev->reshape_backwards = 0; |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1641 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1642 | |
| 1643 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1644 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1645 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1646 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1647 | mddev->bitmap_info.offset = |
| 1648 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1649 | /* Metadata doesn't record how much space is available. |
| 1650 | * For 1.0, we assume we can use up to the superblock |
| 1651 | * if before, else to 4K beyond superblock. |
| 1652 | * For others, assume no change is possible. |
| 1653 | */ |
| 1654 | if (mddev->minor_version > 0) |
| 1655 | mddev->bitmap_info.space = 0; |
| 1656 | else if (mddev->bitmap_info.offset > 0) |
| 1657 | mddev->bitmap_info.space = |
| 1658 | 8 - mddev->bitmap_info.offset; |
| 1659 | else |
| 1660 | mddev->bitmap_info.space = |
| 1661 | -mddev->bitmap_info.offset; |
| 1662 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1663 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1664 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1665 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1666 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1667 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1668 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1669 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1670 | if (mddev->delta_disks < 0 || |
| 1671 | (mddev->delta_disks == 0 && |
| 1672 | (le32_to_cpu(sb->feature_map) |
| 1673 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1674 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1675 | } else { |
| 1676 | mddev->reshape_position = MaxSector; |
| 1677 | mddev->delta_disks = 0; |
| 1678 | mddev->new_level = mddev->level; |
| 1679 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1680 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1683 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1684 | /* Insist of good event counter while assembling, except for |
| 1685 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1687 | if (rdev->desc_nr >= 0 && |
| 1688 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1689 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1690 | if (ev1 < mddev->events) |
| 1691 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1692 | } else if (mddev->bitmap) { |
| 1693 | /* If adding to array with a bitmap, then we can accept an |
| 1694 | * older device, but not too old. |
| 1695 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1696 | if (ev1 < mddev->bitmap->events_cleared) |
| 1697 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1698 | } else { |
| 1699 | if (ev1 < mddev->events) |
| 1700 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1701 | return 0; |
| 1702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1704 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1705 | if (rdev->desc_nr < 0 || |
| 1706 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1707 | role = 0xffff; |
| 1708 | rdev->desc_nr = -1; |
| 1709 | } else |
| 1710 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | switch(role) { |
| 1712 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1715 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | break; |
| 1717 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1718 | if ((le32_to_cpu(sb->feature_map) & |
| 1719 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1720 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1721 | else |
| 1722 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | rdev->raid_disk = role; |
| 1724 | break; |
| 1725 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1726 | if (sb->devflags & WriteMostly1) |
| 1727 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1728 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1729 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1730 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1731 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1736 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
| 1738 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1739 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | int max_dev, i; |
| 1741 | /* make rdev->sb match mddev and rdev data. */ |
| 1742 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1743 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
| 1745 | sb->feature_map = 0; |
| 1746 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1747 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1749 | |
| 1750 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1751 | sb->events = cpu_to_le64(mddev->events); |
| 1752 | if (mddev->in_sync) |
| 1753 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1754 | else |
| 1755 | sb->resync_offset = cpu_to_le64(0); |
| 1756 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1757 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1758 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1759 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1760 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1761 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1762 | sb->level = cpu_to_le32(mddev->level); |
| 1763 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1764 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1765 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1766 | sb->devflags |= WriteMostly1; |
| 1767 | else |
| 1768 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1769 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1770 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1771 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1772 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1773 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1774 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1775 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1776 | |
| 1777 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1778 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1779 | sb->feature_map |= |
| 1780 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1781 | sb->recovery_offset = |
| 1782 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1783 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1784 | if (test_bit(Replacement, &rdev->flags)) |
| 1785 | sb->feature_map |= |
| 1786 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1787 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1788 | if (mddev->reshape_position != MaxSector) { |
| 1789 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1790 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1791 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1792 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1793 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1794 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1795 | if (mddev->delta_disks == 0 && |
| 1796 | mddev->reshape_backwards) |
| 1797 | sb->feature_map |
| 1798 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1799 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1800 | sb->feature_map |
| 1801 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1802 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1803 | - rdev->data_offset)); |
| 1804 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1805 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1806 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1807 | if (rdev->badblocks.count == 0) |
| 1808 | /* Nothing to do for bad blocks*/ ; |
| 1809 | else if (sb->bblog_offset == 0) |
| 1810 | /* Cannot record bad blocks on this device */ |
| 1811 | md_error(mddev, rdev); |
| 1812 | else { |
| 1813 | struct badblocks *bb = &rdev->badblocks; |
| 1814 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1815 | u64 *p = bb->page; |
| 1816 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1817 | if (bb->changed) { |
| 1818 | unsigned seq; |
| 1819 | |
| 1820 | retry: |
| 1821 | seq = read_seqbegin(&bb->lock); |
| 1822 | |
| 1823 | memset(bbp, 0xff, PAGE_SIZE); |
| 1824 | |
| 1825 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1826 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1827 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1828 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1829 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1830 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1831 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1832 | if (read_seqretry(&bb->lock, seq)) |
| 1833 | goto retry; |
| 1834 | |
| 1835 | bb->sector = (rdev->sb_start + |
| 1836 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1837 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1842 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | if (rdev2->desc_nr+1 > max_dev) |
| 1844 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1845 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1846 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1847 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1848 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1849 | rdev->sb_size = max_dev * 2 + 256; |
| 1850 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1851 | if (rdev->sb_size & bmask) |
| 1852 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1853 | } else |
| 1854 | max_dev = le32_to_cpu(sb->max_dev); |
| 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | for (i=0; i<max_dev;i++) |
| 1857 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1858 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1859 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1861 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1863 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1865 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1866 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | else |
| 1868 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1869 | } |
| 1870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1872 | } |
| 1873 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1874 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1875 | 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] | 1876 | { |
| 1877 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1878 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1879 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1880 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1881 | if (rdev->data_offset != rdev->new_data_offset) |
| 1882 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1883 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1884 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1885 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1886 | max_sectors -= rdev->data_offset; |
| 1887 | if (!num_sectors || num_sectors > max_sectors) |
| 1888 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1889 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1890 | /* minor version 0 with bitmap we can't move */ |
| 1891 | return 0; |
| 1892 | } else { |
| 1893 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1894 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1895 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1896 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1897 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1898 | if (!num_sectors || num_sectors > max_sectors) |
| 1899 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1900 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1901 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1902 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1903 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1904 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1905 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1906 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1907 | rdev->sb_page); |
| 1908 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1909 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1910 | |
| 1911 | } |
| 1912 | |
| 1913 | static int |
| 1914 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1915 | unsigned long long new_offset) |
| 1916 | { |
| 1917 | /* All necessary checks on new >= old have been done */ |
| 1918 | struct bitmap *bitmap; |
| 1919 | if (new_offset >= rdev->data_offset) |
| 1920 | return 1; |
| 1921 | |
| 1922 | /* with 1.0 metadata, there is no metadata to tread on |
| 1923 | * so we can always move back */ |
| 1924 | if (rdev->mddev->minor_version == 0) |
| 1925 | return 1; |
| 1926 | |
| 1927 | /* otherwise we must be sure not to step on |
| 1928 | * any metadata, so stay: |
| 1929 | * 36K beyond start of superblock |
| 1930 | * beyond end of badblocks |
| 1931 | * beyond write-intent bitmap |
| 1932 | */ |
| 1933 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1934 | return 0; |
| 1935 | bitmap = rdev->mddev->bitmap; |
| 1936 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1937 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1938 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1939 | return 0; |
| 1940 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1941 | return 0; |
| 1942 | |
| 1943 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1944 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1946 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | [0] = { |
| 1948 | .name = "0.90.0", |
| 1949 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1950 | .load_super = super_90_load, |
| 1951 | .validate_super = super_90_validate, |
| 1952 | .sync_super = super_90_sync, |
| 1953 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1954 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | }, |
| 1956 | [1] = { |
| 1957 | .name = "md-1", |
| 1958 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1959 | .load_super = super_1_load, |
| 1960 | .validate_super = super_1_validate, |
| 1961 | .sync_super = super_1_sync, |
| 1962 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1963 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | }, |
| 1965 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1967 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1968 | { |
| 1969 | if (mddev->sync_super) { |
| 1970 | mddev->sync_super(mddev, rdev); |
| 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1975 | |
| 1976 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1977 | } |
| 1978 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1979 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1981 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1983 | rcu_read_lock(); |
| 1984 | rdev_for_each_rcu(rdev, mddev1) |
| 1985 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1986 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1987 | rdev2->bdev->bd_contains) { |
| 1988 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1989 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1990 | } |
| 1991 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | return 0; |
| 1993 | } |
| 1994 | |
| 1995 | static LIST_HEAD(pending_raid_disks); |
| 1996 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1997 | /* |
| 1998 | * Try to register data integrity profile for an mddev |
| 1999 | * |
| 2000 | * This is called when an array is started and after a disk has been kicked |
| 2001 | * from the array. It only succeeds if all working and active component devices |
| 2002 | * are integrity capable with matching profiles. |
| 2003 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2004 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2005 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2006 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2007 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2008 | if (list_empty(&mddev->disks)) |
| 2009 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2010 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2011 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2012 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2013 | /* skip spares and non-functional disks */ |
| 2014 | if (test_bit(Faulty, &rdev->flags)) |
| 2015 | continue; |
| 2016 | if (rdev->raid_disk < 0) |
| 2017 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2018 | if (!reference) { |
| 2019 | /* Use the first rdev as the reference */ |
| 2020 | reference = rdev; |
| 2021 | continue; |
| 2022 | } |
| 2023 | /* does this rdev's profile match the reference profile? */ |
| 2024 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2025 | rdev->bdev->bd_disk) < 0) |
| 2026 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2027 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2028 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2029 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2030 | /* |
| 2031 | * All component devices are integrity capable and have matching |
| 2032 | * profiles, register the common profile for the md device. |
| 2033 | */ |
| 2034 | if (blk_integrity_register(mddev->gendisk, |
| 2035 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2036 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2037 | mdname(mddev)); |
| 2038 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2039 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2040 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2041 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2042 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2043 | mdname(mddev)); |
| 2044 | return -EINVAL; |
| 2045 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2046 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2047 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2048 | EXPORT_SYMBOL(md_integrity_register); |
| 2049 | |
| 2050 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2051 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2052 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2053 | struct blk_integrity *bi_rdev; |
| 2054 | struct blk_integrity *bi_mddev; |
| 2055 | |
| 2056 | if (!mddev->gendisk) |
| 2057 | return; |
| 2058 | |
| 2059 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2060 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2061 | |
| 2062 | if (!bi_mddev) /* nothing to do */ |
| 2063 | return; |
| 2064 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2065 | return; |
| 2066 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2067 | rdev->bdev->bd_disk) >= 0) |
| 2068 | return; |
| 2069 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2070 | blk_integrity_unregister(mddev->gendisk); |
| 2071 | } |
| 2072 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2073 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2074 | 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] | 2075 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2076 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2077 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2078 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2079 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
| 2081 | if (rdev->mddev) { |
| 2082 | MD_BUG(); |
| 2083 | return -EINVAL; |
| 2084 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2085 | |
| 2086 | /* prevent duplicates */ |
| 2087 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2088 | return -EEXIST; |
| 2089 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2090 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2091 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2092 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2093 | if (mddev->pers) { |
| 2094 | /* Cannot change size, so fail |
| 2095 | * If mddev->level <= 0, then we don't care |
| 2096 | * about aligning sizes (e.g. linear) |
| 2097 | */ |
| 2098 | if (mddev->level > 0) |
| 2099 | return -ENOSPC; |
| 2100 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2101 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
| 2104 | /* Verify rdev->desc_nr is unique. |
| 2105 | * If it is -1, assign a free number, else |
| 2106 | * check number is not in use |
| 2107 | */ |
| 2108 | if (rdev->desc_nr < 0) { |
| 2109 | int choice = 0; |
| 2110 | if (mddev->pers) choice = mddev->raid_disks; |
| 2111 | while (find_rdev_nr(mddev, choice)) |
| 2112 | choice++; |
| 2113 | rdev->desc_nr = choice; |
| 2114 | } else { |
| 2115 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2116 | return -EBUSY; |
| 2117 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2118 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2119 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2120 | mdname(mddev), mddev->max_disks); |
| 2121 | return -EBUSY; |
| 2122 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2123 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2124 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2125 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2128 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2129 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2130 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2131 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2132 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2133 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2134 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2135 | /* failure here is OK */; |
| 2136 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2137 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2138 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2139 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2140 | |
| 2141 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2142 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2145 | |
| 2146 | fail: |
| 2147 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2148 | b, mdname(mddev)); |
| 2149 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2152 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2153 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2154 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2155 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2156 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2159 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | { |
| 2161 | char b[BDEVNAME_SIZE]; |
| 2162 | if (!rdev->mddev) { |
| 2163 | MD_BUG(); |
| 2164 | return; |
| 2165 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2166 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2167 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2169 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2170 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2171 | sysfs_put(rdev->sysfs_state); |
| 2172 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2173 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2174 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2175 | * writing to 'remove' to "dev/state". We also need |
| 2176 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2177 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2178 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2179 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2180 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2181 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | * prevent the device from being mounted, repartitioned or |
| 2186 | * otherwise reused by a RAID array (or any other kernel |
| 2187 | * subsystem), by bd_claiming the device. |
| 2188 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2189 | 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] | 2190 | { |
| 2191 | int err = 0; |
| 2192 | struct block_device *bdev; |
| 2193 | char b[BDEVNAME_SIZE]; |
| 2194 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2195 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2196 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | if (IS_ERR(bdev)) { |
| 2198 | printk(KERN_ERR "md: could not open %s.\n", |
| 2199 | __bdevname(dev, b)); |
| 2200 | return PTR_ERR(bdev); |
| 2201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | rdev->bdev = bdev; |
| 2203 | return err; |
| 2204 | } |
| 2205 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2206 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | { |
| 2208 | struct block_device *bdev = rdev->bdev; |
| 2209 | rdev->bdev = NULL; |
| 2210 | if (!bdev) |
| 2211 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2212 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | void md_autodetect_dev(dev_t dev); |
| 2216 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2217 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | char b[BDEVNAME_SIZE]; |
| 2220 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2221 | bdevname(rdev->bdev,b)); |
| 2222 | if (rdev->mddev) |
| 2223 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2224 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2226 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2227 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | #endif |
| 2229 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2230 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2233 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
| 2235 | unbind_rdev_from_array(rdev); |
| 2236 | export_rdev(rdev); |
| 2237 | } |
| 2238 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2239 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2241 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2243 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | if (!rdev->mddev) { |
| 2245 | MD_BUG(); |
| 2246 | continue; |
| 2247 | } |
| 2248 | kick_rdev_from_array(rdev); |
| 2249 | } |
| 2250 | if (!list_empty(&mddev->disks)) |
| 2251 | MD_BUG(); |
| 2252 | mddev->raid_disks = 0; |
| 2253 | mddev->major_version = 0; |
| 2254 | } |
| 2255 | |
| 2256 | static void print_desc(mdp_disk_t *desc) |
| 2257 | { |
| 2258 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2259 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2260 | } |
| 2261 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2262 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | { |
| 2264 | int i; |
| 2265 | |
| 2266 | printk(KERN_INFO |
| 2267 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2268 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2269 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2270 | sb->ctime); |
| 2271 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2272 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2273 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2274 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2275 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2276 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2277 | sb->failed_disks, sb->spare_disks, |
| 2278 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2279 | |
| 2280 | printk(KERN_INFO); |
| 2281 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2282 | mdp_disk_t *desc; |
| 2283 | |
| 2284 | desc = sb->disks + i; |
| 2285 | if (desc->number || desc->major || desc->minor || |
| 2286 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2287 | printk(" D %2d: ", i); |
| 2288 | print_desc(desc); |
| 2289 | } |
| 2290 | } |
| 2291 | printk(KERN_INFO "md: THIS: "); |
| 2292 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2295 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2296 | { |
| 2297 | __u8 *uuid; |
| 2298 | |
| 2299 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2300 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2301 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2302 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2303 | le32_to_cpu(sb->major_version), |
| 2304 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2305 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2306 | sb->set_name, |
| 2307 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2308 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2309 | |
| 2310 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2311 | printk(KERN_INFO |
| 2312 | "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] | 2313 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2314 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2315 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2316 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2317 | le32_to_cpu(sb->level), |
| 2318 | (unsigned long long)le64_to_cpu(sb->size), |
| 2319 | le32_to_cpu(sb->raid_disks), |
| 2320 | le32_to_cpu(sb->layout), |
| 2321 | le32_to_cpu(sb->chunksize), |
| 2322 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2323 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2324 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2325 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2326 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2327 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2328 | sb->devflags, |
| 2329 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2330 | (unsigned long long)le64_to_cpu(sb->events), |
| 2331 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2332 | le32_to_cpu(sb->sb_csum), |
| 2333 | le32_to_cpu(sb->max_dev) |
| 2334 | ); |
| 2335 | } |
| 2336 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2337 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | { |
| 2339 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2340 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2341 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2342 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2343 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2345 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2346 | switch (major_version) { |
| 2347 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2348 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2349 | break; |
| 2350 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2351 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2352 | break; |
| 2353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | } else |
| 2355 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2356 | } |
| 2357 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2358 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2360 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2361 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2362 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | char b[BDEVNAME_SIZE]; |
| 2364 | |
| 2365 | printk("\n"); |
| 2366 | printk("md: **********************************\n"); |
| 2367 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2368 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2369 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2371 | if (mddev->bitmap) |
| 2372 | bitmap_print_sb(mddev->bitmap); |
| 2373 | else |
| 2374 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2375 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2377 | printk("\n"); |
| 2378 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2379 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2380 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | } |
| 2382 | printk("md: **********************************\n"); |
| 2383 | printk("\n"); |
| 2384 | } |
| 2385 | |
| 2386 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2387 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2389 | /* Update each superblock (in-memory image), but |
| 2390 | * if we are allowed to, skip spares which already |
| 2391 | * have the right event counter, or have one earlier |
| 2392 | * (which would mean they aren't being marked as dirty |
| 2393 | * with the rest of the array) |
| 2394 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2395 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2396 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2397 | if (rdev->sb_events == mddev->events || |
| 2398 | (nospares && |
| 2399 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2400 | rdev->sb_events+1 == mddev->events)) { |
| 2401 | /* Don't update this superblock */ |
| 2402 | rdev->sb_loaded = 2; |
| 2403 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2404 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2405 | rdev->sb_loaded = 1; |
| 2406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2410 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2412 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2413 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2414 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2415 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2417 | if (mddev->ro) { |
| 2418 | if (force_change) |
| 2419 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2420 | return; |
| 2421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2423 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2424 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2425 | if (rdev->raid_disk >= 0 && |
| 2426 | mddev->delta_disks >= 0 && |
| 2427 | !test_bit(In_sync, &rdev->flags) && |
| 2428 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2429 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2430 | |
| 2431 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2432 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2433 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2434 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2435 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2436 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2437 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2438 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2439 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2440 | md_ack_all_badblocks(&rdev->badblocks); |
| 2441 | md_error(mddev, rdev); |
| 2442 | } |
| 2443 | clear_bit(Blocked, &rdev->flags); |
| 2444 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2445 | wake_up(&rdev->blocked_wait); |
| 2446 | } |
| 2447 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2448 | wake_up(&mddev->sb_wait); |
| 2449 | return; |
| 2450 | } |
| 2451 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2452 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2453 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2454 | mddev->utime = get_seconds(); |
| 2455 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2456 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2457 | force_change = 1; |
| 2458 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2459 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2460 | * though if events isn't the right even/odd, we will have to do |
| 2461 | * spares after all |
| 2462 | */ |
| 2463 | nospares = 1; |
| 2464 | if (force_change) |
| 2465 | nospares = 0; |
| 2466 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2467 | /* If the array is degraded, then skipping spares is both |
| 2468 | * dangerous and fairly pointless. |
| 2469 | * Dangerous because a device that was removed from the array |
| 2470 | * might have a event_count that still looks up-to-date, |
| 2471 | * so it can be re-added without a resync. |
| 2472 | * Pointless because if there are any spares to skip, |
| 2473 | * then a recovery will happen and soon that array won't |
| 2474 | * be degraded any more and the spare can go back to sleep then. |
| 2475 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2476 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2477 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2478 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2479 | |
| 2480 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2481 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2482 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2483 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2484 | && mddev->can_decrease_events |
| 2485 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2486 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2487 | mddev->can_decrease_events = 0; |
| 2488 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2489 | /* otherwise we have to go forward and ... */ |
| 2490 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2491 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
| 2494 | if (!mddev->events) { |
| 2495 | /* |
| 2496 | * oops, this 64-bit counter should never wrap. |
| 2497 | * Either we are in around ~1 trillion A.C., assuming |
| 2498 | * 1 reboot per second, or we have a bug: |
| 2499 | */ |
| 2500 | MD_BUG(); |
| 2501 | mddev->events --; |
| 2502 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2503 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2504 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2505 | if (rdev->badblocks.changed) |
| 2506 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2507 | if (test_bit(Faulty, &rdev->flags)) |
| 2508 | set_bit(FaultRecorded, &rdev->flags); |
| 2509 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2510 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2511 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2512 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2514 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2515 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2517 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2518 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2520 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2521 | if (rdev->sb_loaded != 1) |
| 2522 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2524 | if (!test_bit(Faulty, &rdev->flags) && |
| 2525 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2526 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2527 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2528 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2529 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2530 | bdevname(rdev->bdev, b), |
| 2531 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2532 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2533 | if (rdev->badblocks.size) { |
| 2534 | md_super_write(mddev, rdev, |
| 2535 | rdev->badblocks.sector, |
| 2536 | rdev->badblocks.size << 9, |
| 2537 | rdev->bb_page); |
| 2538 | rdev->badblocks.size = 0; |
| 2539 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2540 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2541 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2542 | pr_debug("md: %s (skipping faulty)\n", |
| 2543 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2544 | else |
| 2545 | pr_debug("(skipping incremental s/r "); |
| 2546 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2547 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | /* only need to write one superblock... */ |
| 2549 | break; |
| 2550 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2551 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2552 | /* 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] | 2553 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2554 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2555 | if (mddev->in_sync != sync_req || |
| 2556 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2557 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2558 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2559 | goto repeat; |
| 2560 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2561 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2562 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2563 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2564 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2565 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2566 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2567 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2568 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2569 | clear_bit(Blocked, &rdev->flags); |
| 2570 | |
| 2571 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2572 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2573 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2574 | wake_up(&rdev->blocked_wait); |
| 2575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | } |
| 2577 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2578 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2579 | * We want to accept with case. For this we use cmd_match. |
| 2580 | */ |
| 2581 | static int cmd_match(const char *cmd, const char *str) |
| 2582 | { |
| 2583 | /* See if cmd, written into a sysfs file, matches |
| 2584 | * str. They must either be the same, or cmd can |
| 2585 | * have a trailing newline |
| 2586 | */ |
| 2587 | while (*cmd && *str && *cmd == *str) { |
| 2588 | cmd++; |
| 2589 | str++; |
| 2590 | } |
| 2591 | if (*cmd == '\n') |
| 2592 | cmd++; |
| 2593 | if (*str || *cmd) |
| 2594 | return 0; |
| 2595 | return 1; |
| 2596 | } |
| 2597 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2598 | struct rdev_sysfs_entry { |
| 2599 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2600 | ssize_t (*show)(struct md_rdev *, char *); |
| 2601 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2602 | }; |
| 2603 | |
| 2604 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2605 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2606 | { |
| 2607 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2608 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2609 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2610 | if (test_bit(Faulty, &rdev->flags) || |
| 2611 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2612 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2613 | sep = ","; |
| 2614 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2615 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2616 | len += sprintf(page+len, "%sin_sync",sep); |
| 2617 | sep = ","; |
| 2618 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2619 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2620 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2621 | sep = ","; |
| 2622 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2623 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2624 | (rdev->badblocks.unacked_exist |
| 2625 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2626 | len += sprintf(page+len, "%sblocked", sep); |
| 2627 | sep = ","; |
| 2628 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2629 | if (!test_bit(Faulty, &rdev->flags) && |
| 2630 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2631 | len += sprintf(page+len, "%sspare", sep); |
| 2632 | sep = ","; |
| 2633 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2634 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2635 | len += sprintf(page+len, "%swrite_error", sep); |
| 2636 | sep = ","; |
| 2637 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2638 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2639 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2640 | sep = ","; |
| 2641 | } |
| 2642 | if (test_bit(Replacement, &rdev->flags)) { |
| 2643 | len += sprintf(page+len, "%sreplacement", sep); |
| 2644 | sep = ","; |
| 2645 | } |
| 2646 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2647 | return len+sprintf(page+len, "\n"); |
| 2648 | } |
| 2649 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2650 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2651 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2652 | { |
| 2653 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2654 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2655 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2656 | * writemostly - sets write_mostly |
| 2657 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2658 | * blocked - sets the Blocked flags |
| 2659 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2660 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2661 | * write_error - sets WriteErrorSeen |
| 2662 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2663 | */ |
| 2664 | int err = -EINVAL; |
| 2665 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2666 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2667 | if (test_bit(Faulty, &rdev->flags)) |
| 2668 | err = 0; |
| 2669 | else |
| 2670 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2671 | } else if (cmd_match(buf, "remove")) { |
| 2672 | if (rdev->raid_disk >= 0) |
| 2673 | err = -EBUSY; |
| 2674 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2675 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2676 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2677 | if (mddev->pers) |
| 2678 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2679 | md_new_event(mddev); |
| 2680 | err = 0; |
| 2681 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2682 | } else if (cmd_match(buf, "writemostly")) { |
| 2683 | set_bit(WriteMostly, &rdev->flags); |
| 2684 | err = 0; |
| 2685 | } else if (cmd_match(buf, "-writemostly")) { |
| 2686 | clear_bit(WriteMostly, &rdev->flags); |
| 2687 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2688 | } else if (cmd_match(buf, "blocked")) { |
| 2689 | set_bit(Blocked, &rdev->flags); |
| 2690 | err = 0; |
| 2691 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2692 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2693 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2694 | /* metadata handler doesn't understand badblocks, |
| 2695 | * so we need to fail the device |
| 2696 | */ |
| 2697 | md_error(rdev->mddev, rdev); |
| 2698 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2699 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2700 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2701 | wake_up(&rdev->blocked_wait); |
| 2702 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2703 | md_wakeup_thread(rdev->mddev->thread); |
| 2704 | |
| 2705 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2706 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2707 | set_bit(In_sync, &rdev->flags); |
| 2708 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2709 | } else if (cmd_match(buf, "write_error")) { |
| 2710 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2711 | err = 0; |
| 2712 | } else if (cmd_match(buf, "-write_error")) { |
| 2713 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2714 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2715 | } else if (cmd_match(buf, "want_replacement")) { |
| 2716 | /* Any non-spare device that is not a replacement can |
| 2717 | * become want_replacement at any time, but we then need to |
| 2718 | * check if recovery is needed. |
| 2719 | */ |
| 2720 | if (rdev->raid_disk >= 0 && |
| 2721 | !test_bit(Replacement, &rdev->flags)) |
| 2722 | set_bit(WantReplacement, &rdev->flags); |
| 2723 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2724 | md_wakeup_thread(rdev->mddev->thread); |
| 2725 | err = 0; |
| 2726 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2727 | /* Clearing 'want_replacement' is always allowed. |
| 2728 | * Once replacements starts it is too late though. |
| 2729 | */ |
| 2730 | err = 0; |
| 2731 | clear_bit(WantReplacement, &rdev->flags); |
| 2732 | } else if (cmd_match(buf, "replacement")) { |
| 2733 | /* Can only set a device as a replacement when array has not |
| 2734 | * yet been started. Once running, replacement is automatic |
| 2735 | * from spares, or by assigning 'slot'. |
| 2736 | */ |
| 2737 | if (rdev->mddev->pers) |
| 2738 | err = -EBUSY; |
| 2739 | else { |
| 2740 | set_bit(Replacement, &rdev->flags); |
| 2741 | err = 0; |
| 2742 | } |
| 2743 | } else if (cmd_match(buf, "-replacement")) { |
| 2744 | /* Similarly, can only clear Replacement before start */ |
| 2745 | if (rdev->mddev->pers) |
| 2746 | err = -EBUSY; |
| 2747 | else { |
| 2748 | clear_bit(Replacement, &rdev->flags); |
| 2749 | err = 0; |
| 2750 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2751 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2752 | if (!err) |
| 2753 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2754 | return err ? err : len; |
| 2755 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2756 | static struct rdev_sysfs_entry rdev_state = |
| 2757 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2758 | |
| 2759 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2760 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2761 | { |
| 2762 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2763 | } |
| 2764 | |
| 2765 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2766 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2767 | { |
| 2768 | char *e; |
| 2769 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2770 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2771 | atomic_set(&rdev->corrected_errors, n); |
| 2772 | return len; |
| 2773 | } |
| 2774 | return -EINVAL; |
| 2775 | } |
| 2776 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2777 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2778 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2779 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2780 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2781 | { |
| 2782 | if (rdev->raid_disk < 0) |
| 2783 | return sprintf(page, "none\n"); |
| 2784 | else |
| 2785 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2786 | } |
| 2787 | |
| 2788 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2789 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2790 | { |
| 2791 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2792 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2793 | int slot = simple_strtoul(buf, &e, 10); |
| 2794 | if (strncmp(buf, "none", 4)==0) |
| 2795 | slot = -1; |
| 2796 | else if (e==buf || (*e && *e!= '\n')) |
| 2797 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2798 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2799 | /* Setting 'slot' on an active array requires also |
| 2800 | * updating the 'rd%d' link, and communicating |
| 2801 | * with the personality with ->hot_*_disk. |
| 2802 | * For now we only support removing |
| 2803 | * failed/spare devices. This normally happens automatically, |
| 2804 | * but not when the metadata is externally managed. |
| 2805 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2806 | if (rdev->raid_disk == -1) |
| 2807 | return -EEXIST; |
| 2808 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2809 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2810 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2811 | clear_bit(Blocked, &rdev->flags); |
| 2812 | remove_and_add_spares(rdev->mddev, rdev); |
| 2813 | if (rdev->raid_disk >= 0) |
| 2814 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2815 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2816 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2817 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2818 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2819 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2820 | */ |
| 2821 | |
| 2822 | if (rdev->raid_disk != -1) |
| 2823 | return -EBUSY; |
| 2824 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2825 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2826 | return -EBUSY; |
| 2827 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2828 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2829 | return -EINVAL; |
| 2830 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2831 | if (slot >= rdev->mddev->raid_disks && |
| 2832 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2833 | return -ENOSPC; |
| 2834 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2835 | rdev->raid_disk = slot; |
| 2836 | if (test_bit(In_sync, &rdev->flags)) |
| 2837 | rdev->saved_raid_disk = slot; |
| 2838 | else |
| 2839 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2840 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2841 | err = rdev->mddev->pers-> |
| 2842 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2843 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2844 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2845 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2846 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2847 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2848 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2849 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2850 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2851 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2852 | if (slot >= rdev->mddev->raid_disks && |
| 2853 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2854 | return -ENOSPC; |
| 2855 | rdev->raid_disk = slot; |
| 2856 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2857 | clear_bit(Faulty, &rdev->flags); |
| 2858 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2859 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2860 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2861 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2862 | return len; |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2867 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2868 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2869 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2870 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2871 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2872 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2876 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2877 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2878 | unsigned long long offset; |
| 2879 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2880 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2881 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2882 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2883 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2884 | /* Must set offset before size, so overlap checks |
| 2885 | * can be sane */ |
| 2886 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2887 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2888 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2889 | return len; |
| 2890 | } |
| 2891 | |
| 2892 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2893 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2894 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2895 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2896 | { |
| 2897 | return sprintf(page, "%llu\n", |
| 2898 | (unsigned long long)rdev->new_data_offset); |
| 2899 | } |
| 2900 | |
| 2901 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2902 | const char *buf, size_t len) |
| 2903 | { |
| 2904 | unsigned long long new_offset; |
| 2905 | struct mddev *mddev = rdev->mddev; |
| 2906 | |
| 2907 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2908 | return -EINVAL; |
| 2909 | |
| 2910 | if (mddev->sync_thread) |
| 2911 | return -EBUSY; |
| 2912 | if (new_offset == rdev->data_offset) |
| 2913 | /* reset is always permitted */ |
| 2914 | ; |
| 2915 | else if (new_offset > rdev->data_offset) { |
| 2916 | /* must not push array size beyond rdev_sectors */ |
| 2917 | if (new_offset - rdev->data_offset |
| 2918 | + mddev->dev_sectors > rdev->sectors) |
| 2919 | return -E2BIG; |
| 2920 | } |
| 2921 | /* Metadata worries about other space details. */ |
| 2922 | |
| 2923 | /* decreasing the offset is inconsistent with a backwards |
| 2924 | * reshape. |
| 2925 | */ |
| 2926 | if (new_offset < rdev->data_offset && |
| 2927 | mddev->reshape_backwards) |
| 2928 | return -EINVAL; |
| 2929 | /* Increasing offset is inconsistent with forwards |
| 2930 | * reshape. reshape_direction should be set to |
| 2931 | * 'backwards' first. |
| 2932 | */ |
| 2933 | if (new_offset > rdev->data_offset && |
| 2934 | !mddev->reshape_backwards) |
| 2935 | return -EINVAL; |
| 2936 | |
| 2937 | if (mddev->pers && mddev->persistent && |
| 2938 | !super_types[mddev->major_version] |
| 2939 | .allow_new_offset(rdev, new_offset)) |
| 2940 | return -E2BIG; |
| 2941 | rdev->new_data_offset = new_offset; |
| 2942 | if (new_offset > rdev->data_offset) |
| 2943 | mddev->reshape_backwards = 1; |
| 2944 | else if (new_offset < rdev->data_offset) |
| 2945 | mddev->reshape_backwards = 0; |
| 2946 | |
| 2947 | return len; |
| 2948 | } |
| 2949 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2950 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2951 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2952 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2953 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2954 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2955 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2958 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2959 | { |
| 2960 | /* check if two start/length pairs overlap */ |
| 2961 | if (s1+l1 <= s2) |
| 2962 | return 0; |
| 2963 | if (s2+l2 <= s1) |
| 2964 | return 0; |
| 2965 | return 1; |
| 2966 | } |
| 2967 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2968 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2969 | { |
| 2970 | unsigned long long blocks; |
| 2971 | sector_t new; |
| 2972 | |
| 2973 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2974 | return -EINVAL; |
| 2975 | |
| 2976 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2977 | return -EINVAL; /* sector conversion overflow */ |
| 2978 | |
| 2979 | new = blocks * 2; |
| 2980 | if (new != blocks * 2) |
| 2981 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2982 | |
| 2983 | *sectors = new; |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2987 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2988 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2989 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2990 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2991 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2992 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2993 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2994 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2995 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2996 | if (rdev->data_offset != rdev->new_data_offset) |
| 2997 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2998 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2999 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3000 | sectors = super_types[my_mddev->major_version]. |
| 3001 | rdev_size_change(rdev, sectors); |
| 3002 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3003 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3004 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3005 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3006 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3007 | if (!my_mddev->pers->resize) |
| 3008 | /* Cannot change size for RAID0 or Linear etc */ |
| 3009 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3010 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3011 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3012 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3013 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3014 | rdev->sectors = sectors; |
| 3015 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3016 | /* need to check that all other rdevs with the same ->bdev |
| 3017 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3018 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3019 | * we have to change it back, we will have the lock again. |
| 3020 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3021 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3022 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3023 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3024 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3025 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3026 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3027 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3028 | |
| 3029 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3030 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3031 | if (rdev->bdev == rdev2->bdev && |
| 3032 | rdev != rdev2 && |
| 3033 | overlaps(rdev->data_offset, rdev->sectors, |
| 3034 | rdev2->data_offset, |
| 3035 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3036 | overlap = 1; |
| 3037 | break; |
| 3038 | } |
| 3039 | mddev_unlock(mddev); |
| 3040 | if (overlap) { |
| 3041 | mddev_put(mddev); |
| 3042 | break; |
| 3043 | } |
| 3044 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3045 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3046 | if (overlap) { |
| 3047 | /* Someone else could have slipped in a size |
| 3048 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3049 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3050 | * safe, and trust userspace not to race with |
| 3051 | * itself |
| 3052 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3053 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3054 | return -EBUSY; |
| 3055 | } |
| 3056 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3057 | return len; |
| 3058 | } |
| 3059 | |
| 3060 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3061 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3062 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3063 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3064 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3065 | { |
| 3066 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3067 | |
| 3068 | if (test_bit(In_sync, &rdev->flags) || |
| 3069 | recovery_start == MaxSector) |
| 3070 | return sprintf(page, "none\n"); |
| 3071 | |
| 3072 | return sprintf(page, "%llu\n", recovery_start); |
| 3073 | } |
| 3074 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3075 | 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] | 3076 | { |
| 3077 | unsigned long long recovery_start; |
| 3078 | |
| 3079 | if (cmd_match(buf, "none")) |
| 3080 | recovery_start = MaxSector; |
| 3081 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3082 | return -EINVAL; |
| 3083 | |
| 3084 | if (rdev->mddev->pers && |
| 3085 | rdev->raid_disk >= 0) |
| 3086 | return -EBUSY; |
| 3087 | |
| 3088 | rdev->recovery_offset = recovery_start; |
| 3089 | if (recovery_start == MaxSector) |
| 3090 | set_bit(In_sync, &rdev->flags); |
| 3091 | else |
| 3092 | clear_bit(In_sync, &rdev->flags); |
| 3093 | return len; |
| 3094 | } |
| 3095 | |
| 3096 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3097 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3098 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3099 | |
| 3100 | static ssize_t |
| 3101 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3102 | static ssize_t |
| 3103 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3104 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3105 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3106 | { |
| 3107 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3108 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3109 | 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] | 3110 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3111 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3112 | /* Maybe that ack was all we needed */ |
| 3113 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3114 | wake_up(&rdev->blocked_wait); |
| 3115 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3116 | } |
| 3117 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3118 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3119 | |
| 3120 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3121 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3122 | { |
| 3123 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3124 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3125 | 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] | 3126 | { |
| 3127 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3128 | } |
| 3129 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3130 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3131 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3132 | static struct attribute *rdev_default_attrs[] = { |
| 3133 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3134 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3135 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3136 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3137 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3138 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3139 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3140 | &rdev_bad_blocks.attr, |
| 3141 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3142 | NULL, |
| 3143 | }; |
| 3144 | static ssize_t |
| 3145 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3146 | { |
| 3147 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3148 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3149 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3150 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3151 | |
| 3152 | if (!entry->show) |
| 3153 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3154 | |
| 3155 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3156 | if (!rv) { |
| 3157 | if (rdev->mddev == NULL) |
| 3158 | rv = -EBUSY; |
| 3159 | else |
| 3160 | rv = entry->show(rdev, page); |
| 3161 | mddev_unlock(mddev); |
| 3162 | } |
| 3163 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | static ssize_t |
| 3167 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3168 | const char *page, size_t length) |
| 3169 | { |
| 3170 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3171 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3172 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3173 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3174 | |
| 3175 | if (!entry->store) |
| 3176 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3177 | if (!capable(CAP_SYS_ADMIN)) |
| 3178 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3179 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3180 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3181 | if (rdev->mddev == NULL) |
| 3182 | rv = -EBUSY; |
| 3183 | else |
| 3184 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3185 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3186 | } |
| 3187 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | static void rdev_free(struct kobject *ko) |
| 3191 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3192 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3193 | kfree(rdev); |
| 3194 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3195 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3196 | .show = rdev_attr_show, |
| 3197 | .store = rdev_attr_store, |
| 3198 | }; |
| 3199 | static struct kobj_type rdev_ktype = { |
| 3200 | .release = rdev_free, |
| 3201 | .sysfs_ops = &rdev_sysfs_ops, |
| 3202 | .default_attrs = rdev_default_attrs, |
| 3203 | }; |
| 3204 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3205 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3206 | { |
| 3207 | rdev->desc_nr = -1; |
| 3208 | rdev->saved_raid_disk = -1; |
| 3209 | rdev->raid_disk = -1; |
| 3210 | rdev->flags = 0; |
| 3211 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3212 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3213 | rdev->sb_events = 0; |
| 3214 | rdev->last_read_error.tv_sec = 0; |
| 3215 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3216 | rdev->sb_loaded = 0; |
| 3217 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3218 | atomic_set(&rdev->nr_pending, 0); |
| 3219 | atomic_set(&rdev->read_errors, 0); |
| 3220 | atomic_set(&rdev->corrected_errors, 0); |
| 3221 | |
| 3222 | INIT_LIST_HEAD(&rdev->same_set); |
| 3223 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3224 | |
| 3225 | /* Add space to store bad block list. |
| 3226 | * This reserves the space even on arrays where it cannot |
| 3227 | * be used - I wonder if that matters |
| 3228 | */ |
| 3229 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3230 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3231 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3232 | seqlock_init(&rdev->badblocks.lock); |
| 3233 | if (rdev->badblocks.page == NULL) |
| 3234 | return -ENOMEM; |
| 3235 | |
| 3236 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3237 | } |
| 3238 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | /* |
| 3240 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3241 | * |
| 3242 | * mark the device faulty if: |
| 3243 | * |
| 3244 | * - the device is nonexistent (zero size) |
| 3245 | * - the device has no valid superblock |
| 3246 | * |
| 3247 | * a faulty rdev _never_ has rdev->sb set. |
| 3248 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3249 | 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] | 3250 | { |
| 3251 | char b[BDEVNAME_SIZE]; |
| 3252 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3253 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | sector_t size; |
| 3255 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3256 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | if (!rdev) { |
| 3258 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3259 | return ERR_PTR(-ENOMEM); |
| 3260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3262 | err = md_rdev_init(rdev); |
| 3263 | if (err) |
| 3264 | goto abort_free; |
| 3265 | err = alloc_disk_sb(rdev); |
| 3266 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | goto abort_free; |
| 3268 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3269 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | if (err) |
| 3271 | goto abort_free; |
| 3272 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3273 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3274 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3275 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | if (!size) { |
| 3277 | printk(KERN_WARNING |
| 3278 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3279 | bdevname(rdev->bdev,b)); |
| 3280 | err = -EINVAL; |
| 3281 | goto abort_free; |
| 3282 | } |
| 3283 | |
| 3284 | if (super_format >= 0) { |
| 3285 | err = super_types[super_format]. |
| 3286 | load_super(rdev, NULL, super_minor); |
| 3287 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3288 | printk(KERN_WARNING |
| 3289 | "md: %s does not have a valid v%d.%d " |
| 3290 | "superblock, not importing!\n", |
| 3291 | bdevname(rdev->bdev,b), |
| 3292 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | goto abort_free; |
| 3294 | } |
| 3295 | if (err < 0) { |
| 3296 | printk(KERN_WARNING |
| 3297 | "md: could not read %s's sb, not importing!\n", |
| 3298 | bdevname(rdev->bdev,b)); |
| 3299 | goto abort_free; |
| 3300 | } |
| 3301 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | return rdev; |
| 3304 | |
| 3305 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3306 | if (rdev->bdev) |
| 3307 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3308 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | kfree(rdev); |
| 3310 | return ERR_PTR(err); |
| 3311 | } |
| 3312 | |
| 3313 | /* |
| 3314 | * Check a full RAID array for plausibility |
| 3315 | */ |
| 3316 | |
| 3317 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3318 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | { |
| 3320 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3321 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | char b[BDEVNAME_SIZE]; |
| 3323 | |
| 3324 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3325 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | switch (super_types[mddev->major_version]. |
| 3327 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3328 | case 1: |
| 3329 | freshest = rdev; |
| 3330 | break; |
| 3331 | case 0: |
| 3332 | break; |
| 3333 | default: |
| 3334 | printk( KERN_ERR \ |
| 3335 | "md: fatal superblock inconsistency in %s" |
| 3336 | " -- removing from array\n", |
| 3337 | bdevname(rdev->bdev,b)); |
| 3338 | kick_rdev_from_array(rdev); |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | super_types[mddev->major_version]. |
| 3343 | validate_super(mddev, freshest); |
| 3344 | |
| 3345 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3346 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3347 | if (mddev->max_disks && |
| 3348 | (rdev->desc_nr >= mddev->max_disks || |
| 3349 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3350 | printk(KERN_WARNING |
| 3351 | "md: %s: %s: only %d devices permitted\n", |
| 3352 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3353 | mddev->max_disks); |
| 3354 | kick_rdev_from_array(rdev); |
| 3355 | continue; |
| 3356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | if (rdev != freshest) |
| 3358 | if (super_types[mddev->major_version]. |
| 3359 | validate_super(mddev, rdev)) { |
| 3360 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3361 | " from array!\n", |
| 3362 | bdevname(rdev->bdev,b)); |
| 3363 | kick_rdev_from_array(rdev); |
| 3364 | continue; |
| 3365 | } |
| 3366 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3367 | rdev->desc_nr = i++; |
| 3368 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3369 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3370 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3371 | rdev->raid_disk = -1; |
| 3372 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
| 3374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3377 | /* Read a fixed-point number. |
| 3378 | * Numbers in sysfs attributes should be in "standard" units where |
| 3379 | * possible, so time should be in seconds. |
| 3380 | * However we internally use a a much smaller unit such as |
| 3381 | * milliseconds or jiffies. |
| 3382 | * This function takes a decimal number with a possible fractional |
| 3383 | * component, and produces an integer which is the result of |
| 3384 | * multiplying that number by 10^'scale'. |
| 3385 | * all without any floating-point arithmetic. |
| 3386 | */ |
| 3387 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3388 | { |
| 3389 | unsigned long result = 0; |
| 3390 | long decimals = -1; |
| 3391 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3392 | if (*cp == '.') |
| 3393 | decimals = 0; |
| 3394 | else if (decimals < scale) { |
| 3395 | unsigned int value; |
| 3396 | value = *cp - '0'; |
| 3397 | result = result * 10 + value; |
| 3398 | if (decimals >= 0) |
| 3399 | decimals++; |
| 3400 | } |
| 3401 | cp++; |
| 3402 | } |
| 3403 | if (*cp == '\n') |
| 3404 | cp++; |
| 3405 | if (*cp) |
| 3406 | return -EINVAL; |
| 3407 | if (decimals < 0) |
| 3408 | decimals = 0; |
| 3409 | while (decimals < scale) { |
| 3410 | result *= 10; |
| 3411 | decimals ++; |
| 3412 | } |
| 3413 | *res = result; |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3418 | static void md_safemode_timeout(unsigned long data); |
| 3419 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3421 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3422 | { |
| 3423 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3424 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3425 | } |
| 3426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3427 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3428 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3429 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3430 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3431 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3432 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3433 | if (msec == 0) |
| 3434 | mddev->safemode_delay = 0; |
| 3435 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3436 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3437 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3438 | if (mddev->safemode_delay == 0) |
| 3439 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3440 | if (mddev->safemode_delay < old_delay) |
| 3441 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3442 | } |
| 3443 | return len; |
| 3444 | } |
| 3445 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3446 | __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] | 3447 | |
| 3448 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3449 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3450 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3451 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3452 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3453 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3454 | else if (mddev->clevel[0]) |
| 3455 | return sprintf(page, "%s\n", mddev->clevel); |
| 3456 | else if (mddev->level != LEVEL_NONE) |
| 3457 | return sprintf(page, "%d\n", mddev->level); |
| 3458 | else |
| 3459 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3462 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3463 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3464 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3465 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3466 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3467 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3468 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3469 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3470 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3471 | |
| 3472 | if (mddev->pers == NULL) { |
| 3473 | if (len == 0) |
| 3474 | return 0; |
| 3475 | if (len >= sizeof(mddev->clevel)) |
| 3476 | return -ENOSPC; |
| 3477 | strncpy(mddev->clevel, buf, len); |
| 3478 | if (mddev->clevel[len-1] == '\n') |
| 3479 | len--; |
| 3480 | mddev->clevel[len] = 0; |
| 3481 | mddev->level = LEVEL_NONE; |
| 3482 | return rv; |
| 3483 | } |
| 3484 | |
| 3485 | /* request to change the personality. Need to ensure: |
| 3486 | * - array is not engaged in resync/recovery/reshape |
| 3487 | * - old personality can be suspended |
| 3488 | * - new personality will access other array. |
| 3489 | */ |
| 3490 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3491 | if (mddev->sync_thread || |
| 3492 | mddev->reshape_position != MaxSector || |
| 3493 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3494 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3495 | |
| 3496 | if (!mddev->pers->quiesce) { |
| 3497 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3498 | mdname(mddev), mddev->pers->name); |
| 3499 | return -EINVAL; |
| 3500 | } |
| 3501 | |
| 3502 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3503 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3504 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3505 | strncpy(clevel, buf, len); |
| 3506 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3507 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3508 | clevel[len] = 0; |
| 3509 | if (strict_strtol(clevel, 10, &level)) |
| 3510 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3511 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3512 | if (request_module("md-%s", clevel) != 0) |
| 3513 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3514 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3515 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3516 | if (!pers || !try_module_get(pers->owner)) { |
| 3517 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3518 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3519 | return -EINVAL; |
| 3520 | } |
| 3521 | spin_unlock(&pers_lock); |
| 3522 | |
| 3523 | if (pers == mddev->pers) { |
| 3524 | /* Nothing to do! */ |
| 3525 | module_put(pers->owner); |
| 3526 | return rv; |
| 3527 | } |
| 3528 | if (!pers->takeover) { |
| 3529 | module_put(pers->owner); |
| 3530 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3531 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3532 | return -EINVAL; |
| 3533 | } |
| 3534 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3535 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3536 | rdev->new_raid_disk = rdev->raid_disk; |
| 3537 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3538 | /* ->takeover must set new_* and/or delta_disks |
| 3539 | * if it succeeds, and may set them when it fails. |
| 3540 | */ |
| 3541 | priv = pers->takeover(mddev); |
| 3542 | if (IS_ERR(priv)) { |
| 3543 | mddev->new_level = mddev->level; |
| 3544 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3545 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3546 | mddev->raid_disks -= mddev->delta_disks; |
| 3547 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3548 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3549 | module_put(pers->owner); |
| 3550 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3551 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3552 | return PTR_ERR(priv); |
| 3553 | } |
| 3554 | |
| 3555 | /* Looks like we have a winner */ |
| 3556 | mddev_suspend(mddev); |
| 3557 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3558 | |
| 3559 | if (mddev->pers->sync_request == NULL && |
| 3560 | pers->sync_request != NULL) { |
| 3561 | /* need to add the md_redundancy_group */ |
| 3562 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3563 | printk(KERN_WARNING |
| 3564 | "md: cannot register extra attributes for %s\n", |
| 3565 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3566 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3567 | } |
| 3568 | if (mddev->pers->sync_request != NULL && |
| 3569 | pers->sync_request == NULL) { |
| 3570 | /* need to remove the md_redundancy_group */ |
| 3571 | if (mddev->to_remove == NULL) |
| 3572 | mddev->to_remove = &md_redundancy_group; |
| 3573 | } |
| 3574 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3575 | if (mddev->pers->sync_request == NULL && |
| 3576 | mddev->external) { |
| 3577 | /* We are converting from a no-redundancy array |
| 3578 | * to a redundancy array and metadata is managed |
| 3579 | * externally so we need to be sure that writes |
| 3580 | * won't block due to a need to transition |
| 3581 | * clean->dirty |
| 3582 | * until external management is started. |
| 3583 | */ |
| 3584 | mddev->in_sync = 0; |
| 3585 | mddev->safemode_delay = 0; |
| 3586 | mddev->safemode = 0; |
| 3587 | } |
| 3588 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3589 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3590 | if (rdev->raid_disk < 0) |
| 3591 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3592 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3593 | rdev->new_raid_disk = -1; |
| 3594 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3595 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3596 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3597 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3598 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3599 | if (rdev->raid_disk < 0) |
| 3600 | continue; |
| 3601 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3602 | continue; |
| 3603 | rdev->raid_disk = rdev->new_raid_disk; |
| 3604 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3605 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3606 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3607 | if (sysfs_link_rdev(mddev, rdev)) |
| 3608 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3609 | " for %s after level change\n", |
| 3610 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3611 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3615 | mddev->pers = pers; |
| 3616 | mddev->private = priv; |
| 3617 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3618 | mddev->level = mddev->new_level; |
| 3619 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3620 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3621 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3622 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3623 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3624 | if (mddev->pers->sync_request == NULL) { |
| 3625 | /* this is now an array without redundancy, so |
| 3626 | * it must always be in_sync |
| 3627 | */ |
| 3628 | mddev->in_sync = 1; |
| 3629 | del_timer_sync(&mddev->safemode_timer); |
| 3630 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3631 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3632 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3633 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3634 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3635 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3636 | return rv; |
| 3637 | } |
| 3638 | |
| 3639 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3640 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3641 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3642 | |
| 3643 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3644 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3645 | { |
| 3646 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3647 | if (mddev->reshape_position != MaxSector && |
| 3648 | mddev->layout != mddev->new_layout) |
| 3649 | return sprintf(page, "%d (%d)\n", |
| 3650 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3651 | return sprintf(page, "%d\n", mddev->layout); |
| 3652 | } |
| 3653 | |
| 3654 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3655 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3656 | { |
| 3657 | char *e; |
| 3658 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3659 | |
| 3660 | if (!*buf || (*e && *e != '\n')) |
| 3661 | return -EINVAL; |
| 3662 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3663 | if (mddev->pers) { |
| 3664 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3665 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3666 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3667 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3668 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3669 | if (err) { |
| 3670 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3671 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3672 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3673 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3674 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3675 | if (mddev->reshape_position == MaxSector) |
| 3676 | mddev->layout = n; |
| 3677 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3678 | return len; |
| 3679 | } |
| 3680 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3681 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3682 | |
| 3683 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3684 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3685 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3686 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3687 | if (mddev->raid_disks == 0) |
| 3688 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3689 | if (mddev->reshape_position != MaxSector && |
| 3690 | mddev->delta_disks != 0) |
| 3691 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3692 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3693 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3694 | } |
| 3695 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3696 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3697 | |
| 3698 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3699 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3700 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3701 | char *e; |
| 3702 | int rv = 0; |
| 3703 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3704 | |
| 3705 | if (!*buf || (*e && *e != '\n')) |
| 3706 | return -EINVAL; |
| 3707 | |
| 3708 | if (mddev->pers) |
| 3709 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3710 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3711 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3712 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3713 | |
| 3714 | rdev_for_each(rdev, mddev) { |
| 3715 | if (olddisks < n && |
| 3716 | rdev->data_offset < rdev->new_data_offset) |
| 3717 | return -EINVAL; |
| 3718 | if (olddisks > n && |
| 3719 | rdev->data_offset > rdev->new_data_offset) |
| 3720 | return -EINVAL; |
| 3721 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3722 | mddev->delta_disks = n - olddisks; |
| 3723 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3724 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3725 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3726 | mddev->raid_disks = n; |
| 3727 | return rv ? rv : len; |
| 3728 | } |
| 3729 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3730 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3731 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3732 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3733 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3734 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3735 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3736 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3737 | return sprintf(page, "%d (%d)\n", |
| 3738 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3739 | mddev->chunk_sectors << 9); |
| 3740 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3745 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3746 | char *e; |
| 3747 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3748 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3749 | if (!*buf || (*e && *e != '\n')) |
| 3750 | return -EINVAL; |
| 3751 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3752 | if (mddev->pers) { |
| 3753 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3754 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3755 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3756 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3757 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3758 | if (err) { |
| 3759 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3760 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3761 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3762 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3763 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3764 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3765 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3766 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | return len; |
| 3768 | } |
| 3769 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3770 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3771 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3772 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3773 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3774 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3775 | if (mddev->recovery_cp == MaxSector) |
| 3776 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3777 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3778 | } |
| 3779 | |
| 3780 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3781 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3782 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3783 | char *e; |
| 3784 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3785 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3786 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3787 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3788 | if (cmd_match(buf, "none")) |
| 3789 | n = MaxSector; |
| 3790 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3791 | return -EINVAL; |
| 3792 | |
| 3793 | mddev->recovery_cp = n; |
NeilBrown | db07d85 | 2012-10-11 14:22:17 +1100 | [diff] [blame] | 3794 | if (mddev->pers) |
| 3795 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3796 | return len; |
| 3797 | } |
| 3798 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3799 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3800 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3801 | /* |
| 3802 | * The array state can be: |
| 3803 | * |
| 3804 | * clear |
| 3805 | * No devices, no size, no level |
| 3806 | * Equivalent to STOP_ARRAY ioctl |
| 3807 | * inactive |
| 3808 | * May have some settings, but array is not active |
| 3809 | * all IO results in error |
| 3810 | * When written, doesn't tear down array, but just stops it |
| 3811 | * suspended (not supported yet) |
| 3812 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3813 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3814 | * readonly |
| 3815 | * no resync can happen. no superblocks get written. |
| 3816 | * write requests fail |
| 3817 | * read-auto |
| 3818 | * like readonly, but behaves like 'clean' on a write request. |
| 3819 | * |
| 3820 | * clean - no pending writes, but otherwise active. |
| 3821 | * When written to inactive array, starts without resync |
| 3822 | * If a write request arrives then |
| 3823 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3824 | * if not known, block and switch to write-pending |
| 3825 | * If written to an active array that has pending writes, then fails. |
| 3826 | * active |
| 3827 | * fully active: IO and resync can be happening. |
| 3828 | * When written to inactive array, starts with resync |
| 3829 | * |
| 3830 | * write-pending |
| 3831 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3832 | * |
| 3833 | * active-idle |
| 3834 | * like active, but no writes have been seen for a while (100msec). |
| 3835 | * |
| 3836 | */ |
| 3837 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3838 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3839 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3840 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3841 | "write-pending", "active-idle", NULL }; |
| 3842 | |
| 3843 | static int match_word(const char *word, char **list) |
| 3844 | { |
| 3845 | int n; |
| 3846 | for (n=0; list[n]; n++) |
| 3847 | if (cmd_match(word, list[n])) |
| 3848 | break; |
| 3849 | return n; |
| 3850 | } |
| 3851 | |
| 3852 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3853 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3854 | { |
| 3855 | enum array_state st = inactive; |
| 3856 | |
| 3857 | if (mddev->pers) |
| 3858 | switch(mddev->ro) { |
| 3859 | case 1: |
| 3860 | st = readonly; |
| 3861 | break; |
| 3862 | case 2: |
| 3863 | st = read_auto; |
| 3864 | break; |
| 3865 | case 0: |
| 3866 | if (mddev->in_sync) |
| 3867 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3868 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3869 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3870 | else if (mddev->safemode) |
| 3871 | st = active_idle; |
| 3872 | else |
| 3873 | st = active; |
| 3874 | } |
| 3875 | else { |
| 3876 | if (list_empty(&mddev->disks) && |
| 3877 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3878 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3879 | st = clear; |
| 3880 | else |
| 3881 | st = inactive; |
| 3882 | } |
| 3883 | return sprintf(page, "%s\n", array_states[st]); |
| 3884 | } |
| 3885 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3886 | static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev); |
| 3887 | static int md_set_readonly(struct mddev * mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3888 | static int do_md_run(struct mddev * mddev); |
| 3889 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3890 | |
| 3891 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3892 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3893 | { |
| 3894 | int err = -EINVAL; |
| 3895 | enum array_state st = match_word(buf, array_states); |
| 3896 | switch(st) { |
| 3897 | case bad_word: |
| 3898 | break; |
| 3899 | case clear: |
| 3900 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3901 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3902 | break; |
| 3903 | case inactive: |
| 3904 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3905 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3906 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3907 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3908 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3909 | break; |
| 3910 | case suspended: |
| 3911 | break; /* not supported yet */ |
| 3912 | case readonly: |
| 3913 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3914 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3915 | else { |
| 3916 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3917 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3918 | err = do_md_run(mddev); |
| 3919 | } |
| 3920 | break; |
| 3921 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3922 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3923 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3924 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3925 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3926 | err = restart_array(mddev); |
| 3927 | if (err == 0) { |
| 3928 | mddev->ro = 2; |
| 3929 | set_disk_ro(mddev->gendisk, 0); |
| 3930 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3931 | } else { |
| 3932 | mddev->ro = 2; |
| 3933 | err = do_md_run(mddev); |
| 3934 | } |
| 3935 | break; |
| 3936 | case clean: |
| 3937 | if (mddev->pers) { |
| 3938 | restart_array(mddev); |
| 3939 | spin_lock_irq(&mddev->write_lock); |
| 3940 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3941 | if (mddev->in_sync == 0) { |
| 3942 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3943 | if (mddev->safemode == 1) |
| 3944 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3945 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3946 | } |
| 3947 | err = 0; |
| 3948 | } else |
| 3949 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3950 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3951 | } else |
| 3952 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3953 | break; |
| 3954 | case active: |
| 3955 | if (mddev->pers) { |
| 3956 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3957 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3958 | wake_up(&mddev->sb_wait); |
| 3959 | err = 0; |
| 3960 | } else { |
| 3961 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3962 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3963 | err = do_md_run(mddev); |
| 3964 | } |
| 3965 | break; |
| 3966 | case write_pending: |
| 3967 | case active_idle: |
| 3968 | /* these cannot be set */ |
| 3969 | break; |
| 3970 | } |
| 3971 | if (err) |
| 3972 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3973 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3974 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3975 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3976 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3978 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3979 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3980 | static struct md_sysfs_entry md_array_state = |
| 3981 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3982 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3983 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3984 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3985 | return sprintf(page, "%d\n", |
| 3986 | atomic_read(&mddev->max_corr_read_errors)); |
| 3987 | } |
| 3988 | |
| 3989 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3990 | 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] | 3991 | { |
| 3992 | char *e; |
| 3993 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3994 | |
| 3995 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3996 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3997 | return len; |
| 3998 | } |
| 3999 | return -EINVAL; |
| 4000 | } |
| 4001 | |
| 4002 | static struct md_sysfs_entry max_corr_read_errors = |
| 4003 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4004 | max_corrected_read_errors_store); |
| 4005 | |
| 4006 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4007 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4008 | { |
| 4009 | return -EINVAL; |
| 4010 | } |
| 4011 | |
| 4012 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4013 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4014 | { |
| 4015 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4016 | /* The new device is added to the array. |
| 4017 | * If the array has a persistent superblock, we read the |
| 4018 | * superblock to initialise info and check validity. |
| 4019 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4020 | * which mainly checks size. |
| 4021 | */ |
| 4022 | char *e; |
| 4023 | int major = simple_strtoul(buf, &e, 10); |
| 4024 | int minor; |
| 4025 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4026 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4027 | int err; |
| 4028 | |
| 4029 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4030 | return -EINVAL; |
| 4031 | minor = simple_strtoul(e+1, &e, 10); |
| 4032 | if (*e && *e != '\n') |
| 4033 | return -EINVAL; |
| 4034 | dev = MKDEV(major, minor); |
| 4035 | if (major != MAJOR(dev) || |
| 4036 | minor != MINOR(dev)) |
| 4037 | return -EOVERFLOW; |
| 4038 | |
| 4039 | |
| 4040 | if (mddev->persistent) { |
| 4041 | rdev = md_import_device(dev, mddev->major_version, |
| 4042 | mddev->minor_version); |
| 4043 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4044 | struct md_rdev *rdev0 |
| 4045 | = list_entry(mddev->disks.next, |
| 4046 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4047 | err = super_types[mddev->major_version] |
| 4048 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4049 | if (err < 0) |
| 4050 | goto out; |
| 4051 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4052 | } else if (mddev->external) |
| 4053 | rdev = md_import_device(dev, -2, -1); |
| 4054 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4055 | rdev = md_import_device(dev, -1, -1); |
| 4056 | |
| 4057 | if (IS_ERR(rdev)) |
| 4058 | return PTR_ERR(rdev); |
| 4059 | err = bind_rdev_to_array(rdev, mddev); |
| 4060 | out: |
| 4061 | if (err) |
| 4062 | export_rdev(rdev); |
| 4063 | return err ? err : len; |
| 4064 | } |
| 4065 | |
| 4066 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4067 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4068 | |
| 4069 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4070 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4071 | { |
| 4072 | char *end; |
| 4073 | unsigned long chunk, end_chunk; |
| 4074 | |
| 4075 | if (!mddev->bitmap) |
| 4076 | goto out; |
| 4077 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4078 | while (*buf) { |
| 4079 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4080 | if (buf == end) break; |
| 4081 | if (*end == '-') { /* range */ |
| 4082 | buf = end + 1; |
| 4083 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4084 | if (buf == end) break; |
| 4085 | } |
| 4086 | if (*end && !isspace(*end)) break; |
| 4087 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4088 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4089 | } |
| 4090 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4091 | out: |
| 4092 | return len; |
| 4093 | } |
| 4094 | |
| 4095 | static struct md_sysfs_entry md_bitmap = |
| 4096 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4097 | |
| 4098 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4099 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4100 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4101 | return sprintf(page, "%llu\n", |
| 4102 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4103 | } |
| 4104 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4105 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4106 | |
| 4107 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4108 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4109 | { |
| 4110 | /* If array is inactive, we can reduce the component size, but |
| 4111 | * not increase it (except from 0). |
| 4112 | * If array is active, we can try an on-line resize |
| 4113 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4114 | sector_t sectors; |
| 4115 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4116 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4117 | if (err < 0) |
| 4118 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4119 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4120 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4121 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4122 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4123 | if (mddev->dev_sectors == 0 || |
| 4124 | mddev->dev_sectors > sectors) |
| 4125 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4126 | else |
| 4127 | err = -ENOSPC; |
| 4128 | } |
| 4129 | return err ? err : len; |
| 4130 | } |
| 4131 | |
| 4132 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4133 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4134 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4135 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4136 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4137 | * This is one of |
| 4138 | * 'none' for arrays with no metadata (good luck...) |
| 4139 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4140 | * or N.M for internally known formats |
| 4141 | */ |
| 4142 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4143 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4144 | { |
| 4145 | if (mddev->persistent) |
| 4146 | return sprintf(page, "%d.%d\n", |
| 4147 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4148 | else if (mddev->external) |
| 4149 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4150 | else |
| 4151 | return sprintf(page, "none\n"); |
| 4152 | } |
| 4153 | |
| 4154 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4155 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4156 | { |
| 4157 | int major, minor; |
| 4158 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4159 | /* Changing the details of 'external' metadata is |
| 4160 | * always permitted. Otherwise there must be |
| 4161 | * no devices attached to the array. |
| 4162 | */ |
| 4163 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4164 | ; |
| 4165 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4166 | return -EBUSY; |
| 4167 | |
| 4168 | if (cmd_match(buf, "none")) { |
| 4169 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4170 | mddev->external = 0; |
| 4171 | mddev->major_version = 0; |
| 4172 | mddev->minor_version = 90; |
| 4173 | return len; |
| 4174 | } |
| 4175 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4176 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4177 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4178 | namelen = sizeof(mddev->metadata_type)-1; |
| 4179 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4180 | mddev->metadata_type[namelen] = 0; |
| 4181 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4182 | mddev->metadata_type[--namelen] = 0; |
| 4183 | mddev->persistent = 0; |
| 4184 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4185 | mddev->major_version = 0; |
| 4186 | mddev->minor_version = 90; |
| 4187 | return len; |
| 4188 | } |
| 4189 | major = simple_strtoul(buf, &e, 10); |
| 4190 | if (e==buf || *e != '.') |
| 4191 | return -EINVAL; |
| 4192 | buf = e+1; |
| 4193 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4194 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4195 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4196 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4197 | return -ENOENT; |
| 4198 | mddev->major_version = major; |
| 4199 | mddev->minor_version = minor; |
| 4200 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4201 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4202 | return len; |
| 4203 | } |
| 4204 | |
| 4205 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4206 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4207 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4208 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4209 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4210 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4211 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4212 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4213 | type = "frozen"; |
| 4214 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4215 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4216 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4217 | type = "reshape"; |
| 4218 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4219 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4220 | type = "resync"; |
| 4221 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4222 | type = "check"; |
| 4223 | else |
| 4224 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4225 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4226 | type = "recover"; |
| 4227 | } |
| 4228 | return sprintf(page, "%s\n", type); |
| 4229 | } |
| 4230 | |
| 4231 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4232 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4233 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4234 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4235 | return -EINVAL; |
| 4236 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4237 | if (cmd_match(page, "frozen")) |
| 4238 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4239 | else |
| 4240 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4241 | |
| 4242 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4243 | if (mddev->sync_thread) { |
| 4244 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 4245 | md_reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4246 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4247 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4248 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4249 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4250 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4251 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4252 | else if (cmd_match(page, "recover")) { |
| 4253 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4254 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4255 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4256 | int err; |
| 4257 | if (mddev->pers->start_reshape == NULL) |
| 4258 | return -EINVAL; |
| 4259 | err = mddev->pers->start_reshape(mddev); |
| 4260 | if (err) |
| 4261 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4262 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4263 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4264 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4265 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4266 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4267 | return -EINVAL; |
| 4268 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4269 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4270 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4271 | if (mddev->ro == 2) { |
| 4272 | /* A write to sync_action is enough to justify |
| 4273 | * canceling read-auto mode |
| 4274 | */ |
| 4275 | mddev->ro = 0; |
| 4276 | md_wakeup_thread(mddev->sync_thread); |
| 4277 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4278 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4279 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4280 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4281 | return len; |
| 4282 | } |
| 4283 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4284 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4285 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4286 | { |
| 4287 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4288 | (unsigned long long) |
| 4289 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4290 | } |
| 4291 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4292 | static struct md_sysfs_entry md_scan_mode = |
| 4293 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4294 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4295 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4296 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4297 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4298 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4299 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4300 | { |
| 4301 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4302 | mddev->sync_speed_min ? "local": "system"); |
| 4303 | } |
| 4304 | |
| 4305 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4306 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4307 | { |
| 4308 | int min; |
| 4309 | char *e; |
| 4310 | if (strncmp(buf, "system", 6)==0) { |
| 4311 | mddev->sync_speed_min = 0; |
| 4312 | return len; |
| 4313 | } |
| 4314 | min = simple_strtoul(buf, &e, 10); |
| 4315 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4316 | return -EINVAL; |
| 4317 | mddev->sync_speed_min = min; |
| 4318 | return len; |
| 4319 | } |
| 4320 | |
| 4321 | static struct md_sysfs_entry md_sync_min = |
| 4322 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4323 | |
| 4324 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4325 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4326 | { |
| 4327 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4328 | mddev->sync_speed_max ? "local": "system"); |
| 4329 | } |
| 4330 | |
| 4331 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4332 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4333 | { |
| 4334 | int max; |
| 4335 | char *e; |
| 4336 | if (strncmp(buf, "system", 6)==0) { |
| 4337 | mddev->sync_speed_max = 0; |
| 4338 | return len; |
| 4339 | } |
| 4340 | max = simple_strtoul(buf, &e, 10); |
| 4341 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4342 | return -EINVAL; |
| 4343 | mddev->sync_speed_max = max; |
| 4344 | return len; |
| 4345 | } |
| 4346 | |
| 4347 | static struct md_sysfs_entry md_sync_max = |
| 4348 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4349 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4350 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4351 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4352 | { |
| 4353 | return sprintf(page, "%d\n", mddev->degraded); |
| 4354 | } |
| 4355 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4356 | |
| 4357 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4358 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4359 | { |
| 4360 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4361 | } |
| 4362 | |
| 4363 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4364 | 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] | 4365 | { |
| 4366 | long n; |
| 4367 | |
| 4368 | if (strict_strtol(buf, 10, &n)) |
| 4369 | return -EINVAL; |
| 4370 | |
| 4371 | if (n != 0 && n != 1) |
| 4372 | return -EINVAL; |
| 4373 | |
| 4374 | mddev->parallel_resync = n; |
| 4375 | |
| 4376 | if (mddev->sync_thread) |
| 4377 | wake_up(&resync_wait); |
| 4378 | |
| 4379 | return len; |
| 4380 | } |
| 4381 | |
| 4382 | /* force parallel resync, even with shared block devices */ |
| 4383 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4384 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4385 | sync_force_parallel_show, sync_force_parallel_store); |
| 4386 | |
| 4387 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4388 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4389 | { |
| 4390 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4391 | if (mddev->curr_resync == 0) |
| 4392 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4393 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4394 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4395 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4396 | db = resync - mddev->resync_mark_cnt; |
| 4397 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4398 | } |
| 4399 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4400 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4401 | |
| 4402 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4403 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4404 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4405 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4406 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4407 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4408 | return sprintf(page, "none\n"); |
| 4409 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4410 | if (mddev->curr_resync == 1 || |
| 4411 | mddev->curr_resync == 2) |
| 4412 | return sprintf(page, "delayed\n"); |
| 4413 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4414 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4415 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4416 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4417 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4418 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4419 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4420 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4421 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4422 | } |
| 4423 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4424 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4425 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4427 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4428 | { |
| 4429 | return sprintf(page, "%llu\n", |
| 4430 | (unsigned long long)mddev->resync_min); |
| 4431 | } |
| 4432 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4433 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4434 | { |
| 4435 | unsigned long long min; |
| 4436 | if (strict_strtoull(buf, 10, &min)) |
| 4437 | return -EINVAL; |
| 4438 | if (min > mddev->resync_max) |
| 4439 | return -EINVAL; |
| 4440 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4441 | return -EBUSY; |
| 4442 | |
| 4443 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4444 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4445 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4446 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4447 | return -EINVAL; |
| 4448 | } |
| 4449 | mddev->resync_min = min; |
| 4450 | |
| 4451 | return len; |
| 4452 | } |
| 4453 | |
| 4454 | static struct md_sysfs_entry md_min_sync = |
| 4455 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4456 | |
| 4457 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4458 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4459 | { |
| 4460 | if (mddev->resync_max == MaxSector) |
| 4461 | return sprintf(page, "max\n"); |
| 4462 | else |
| 4463 | return sprintf(page, "%llu\n", |
| 4464 | (unsigned long long)mddev->resync_max); |
| 4465 | } |
| 4466 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4467 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4468 | { |
| 4469 | if (strncmp(buf, "max", 3) == 0) |
| 4470 | mddev->resync_max = MaxSector; |
| 4471 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4472 | unsigned long long max; |
| 4473 | if (strict_strtoull(buf, 10, &max)) |
| 4474 | return -EINVAL; |
| 4475 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4476 | return -EINVAL; |
| 4477 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4478 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4479 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4480 | return -EBUSY; |
| 4481 | |
| 4482 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4483 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4484 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4485 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4486 | return -EINVAL; |
| 4487 | } |
| 4488 | mddev->resync_max = max; |
| 4489 | } |
| 4490 | wake_up(&mddev->recovery_wait); |
| 4491 | return len; |
| 4492 | } |
| 4493 | |
| 4494 | static struct md_sysfs_entry md_max_sync = |
| 4495 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4496 | |
| 4497 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4498 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4499 | { |
| 4500 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4501 | } |
| 4502 | |
| 4503 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4504 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4505 | { |
| 4506 | char *e; |
| 4507 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4508 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4509 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4510 | if (mddev->pers == NULL || |
| 4511 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4512 | return -EINVAL; |
| 4513 | if (buf == e || (*e && *e != '\n')) |
| 4514 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4515 | |
| 4516 | mddev->suspend_lo = new; |
| 4517 | if (new >= old) |
| 4518 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4519 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4520 | else { |
| 4521 | /* Expanding suspended region - need to wait */ |
| 4522 | mddev->pers->quiesce(mddev, 1); |
| 4523 | mddev->pers->quiesce(mddev, 0); |
| 4524 | } |
| 4525 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4526 | } |
| 4527 | static struct md_sysfs_entry md_suspend_lo = |
| 4528 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4529 | |
| 4530 | |
| 4531 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4532 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4533 | { |
| 4534 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4535 | } |
| 4536 | |
| 4537 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4538 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4539 | { |
| 4540 | char *e; |
| 4541 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4542 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4543 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4544 | if (mddev->pers == NULL || |
| 4545 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4546 | return -EINVAL; |
| 4547 | if (buf == e || (*e && *e != '\n')) |
| 4548 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4549 | |
| 4550 | mddev->suspend_hi = new; |
| 4551 | if (new <= old) |
| 4552 | /* Shrinking suspended region */ |
| 4553 | mddev->pers->quiesce(mddev, 2); |
| 4554 | else { |
| 4555 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4556 | mddev->pers->quiesce(mddev, 1); |
| 4557 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4558 | } |
| 4559 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4560 | } |
| 4561 | static struct md_sysfs_entry md_suspend_hi = |
| 4562 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4563 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4564 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4565 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4566 | { |
| 4567 | if (mddev->reshape_position != MaxSector) |
| 4568 | return sprintf(page, "%llu\n", |
| 4569 | (unsigned long long)mddev->reshape_position); |
| 4570 | strcpy(page, "none\n"); |
| 4571 | return 5; |
| 4572 | } |
| 4573 | |
| 4574 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4575 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4576 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4577 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4578 | char *e; |
| 4579 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4580 | if (mddev->pers) |
| 4581 | return -EBUSY; |
| 4582 | if (buf == e || (*e && *e != '\n')) |
| 4583 | return -EINVAL; |
| 4584 | mddev->reshape_position = new; |
| 4585 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4586 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4587 | mddev->new_level = mddev->level; |
| 4588 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4589 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4590 | rdev_for_each(rdev, mddev) |
| 4591 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4592 | return len; |
| 4593 | } |
| 4594 | |
| 4595 | static struct md_sysfs_entry md_reshape_position = |
| 4596 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4597 | reshape_position_store); |
| 4598 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4599 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4600 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4601 | { |
| 4602 | return sprintf(page, "%s\n", |
| 4603 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4604 | } |
| 4605 | |
| 4606 | static ssize_t |
| 4607 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4608 | { |
| 4609 | int backwards = 0; |
| 4610 | if (cmd_match(buf, "forwards")) |
| 4611 | backwards = 0; |
| 4612 | else if (cmd_match(buf, "backwards")) |
| 4613 | backwards = 1; |
| 4614 | else |
| 4615 | return -EINVAL; |
| 4616 | if (mddev->reshape_backwards == backwards) |
| 4617 | return len; |
| 4618 | |
| 4619 | /* check if we are allowed to change */ |
| 4620 | if (mddev->delta_disks) |
| 4621 | return -EBUSY; |
| 4622 | |
| 4623 | if (mddev->persistent && |
| 4624 | mddev->major_version == 0) |
| 4625 | return -EINVAL; |
| 4626 | |
| 4627 | mddev->reshape_backwards = backwards; |
| 4628 | return len; |
| 4629 | } |
| 4630 | |
| 4631 | static struct md_sysfs_entry md_reshape_direction = |
| 4632 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4633 | reshape_direction_store); |
| 4634 | |
| 4635 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4636 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4637 | { |
| 4638 | if (mddev->external_size) |
| 4639 | return sprintf(page, "%llu\n", |
| 4640 | (unsigned long long)mddev->array_sectors/2); |
| 4641 | else |
| 4642 | return sprintf(page, "default\n"); |
| 4643 | } |
| 4644 | |
| 4645 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4646 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4647 | { |
| 4648 | sector_t sectors; |
| 4649 | |
| 4650 | if (strncmp(buf, "default", 7) == 0) { |
| 4651 | if (mddev->pers) |
| 4652 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4653 | else |
| 4654 | sectors = mddev->array_sectors; |
| 4655 | |
| 4656 | mddev->external_size = 0; |
| 4657 | } else { |
| 4658 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4659 | return -EINVAL; |
| 4660 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4661 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4662 | |
| 4663 | mddev->external_size = 1; |
| 4664 | } |
| 4665 | |
| 4666 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4667 | if (mddev->pers) { |
| 4668 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4669 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4670 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4671 | return len; |
| 4672 | } |
| 4673 | |
| 4674 | static struct md_sysfs_entry md_array_size = |
| 4675 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4676 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4677 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4678 | static struct attribute *md_default_attrs[] = { |
| 4679 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4680 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4681 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4682 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4683 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4684 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4685 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4686 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4687 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4688 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4689 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4690 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4691 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4692 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4693 | NULL, |
| 4694 | }; |
| 4695 | |
| 4696 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4697 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4698 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4699 | &md_sync_min.attr, |
| 4700 | &md_sync_max.attr, |
| 4701 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4702 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4703 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4704 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4705 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4706 | &md_suspend_lo.attr, |
| 4707 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4708 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4709 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4710 | NULL, |
| 4711 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4712 | static struct attribute_group md_redundancy_group = { |
| 4713 | .name = NULL, |
| 4714 | .attrs = md_redundancy_attrs, |
| 4715 | }; |
| 4716 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4717 | |
| 4718 | static ssize_t |
| 4719 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4720 | { |
| 4721 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4722 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4723 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4724 | |
| 4725 | if (!entry->show) |
| 4726 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4727 | spin_lock(&all_mddevs_lock); |
| 4728 | if (list_empty(&mddev->all_mddevs)) { |
| 4729 | spin_unlock(&all_mddevs_lock); |
| 4730 | return -EBUSY; |
| 4731 | } |
| 4732 | mddev_get(mddev); |
| 4733 | spin_unlock(&all_mddevs_lock); |
| 4734 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4735 | rv = mddev_lock(mddev); |
| 4736 | if (!rv) { |
| 4737 | rv = entry->show(mddev, page); |
| 4738 | mddev_unlock(mddev); |
| 4739 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4740 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4741 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | static ssize_t |
| 4745 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4746 | const char *page, size_t length) |
| 4747 | { |
| 4748 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4749 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4750 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4751 | |
| 4752 | if (!entry->store) |
| 4753 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4754 | if (!capable(CAP_SYS_ADMIN)) |
| 4755 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4756 | spin_lock(&all_mddevs_lock); |
| 4757 | if (list_empty(&mddev->all_mddevs)) { |
| 4758 | spin_unlock(&all_mddevs_lock); |
| 4759 | return -EBUSY; |
| 4760 | } |
| 4761 | mddev_get(mddev); |
| 4762 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 4763 | if (entry->store == new_dev_store) |
| 4764 | flush_workqueue(md_misc_wq); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4765 | rv = mddev_lock(mddev); |
| 4766 | if (!rv) { |
| 4767 | rv = entry->store(mddev, page, length); |
| 4768 | mddev_unlock(mddev); |
| 4769 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4770 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4771 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static void md_free(struct kobject *ko) |
| 4775 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4776 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4777 | |
| 4778 | if (mddev->sysfs_state) |
| 4779 | sysfs_put(mddev->sysfs_state); |
| 4780 | |
| 4781 | if (mddev->gendisk) { |
| 4782 | del_gendisk(mddev->gendisk); |
| 4783 | put_disk(mddev->gendisk); |
| 4784 | } |
| 4785 | if (mddev->queue) |
| 4786 | blk_cleanup_queue(mddev->queue); |
| 4787 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4788 | kfree(mddev); |
| 4789 | } |
| 4790 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4791 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4792 | .show = md_attr_show, |
| 4793 | .store = md_attr_store, |
| 4794 | }; |
| 4795 | static struct kobj_type md_ktype = { |
| 4796 | .release = md_free, |
| 4797 | .sysfs_ops = &md_sysfs_ops, |
| 4798 | .default_attrs = md_default_attrs, |
| 4799 | }; |
| 4800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | int mdp_major = 0; |
| 4802 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4803 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4804 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4805 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4806 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4807 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4808 | kobject_del(&mddev->kobj); |
| 4809 | kobject_put(&mddev->kobj); |
| 4810 | } |
| 4811 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4812 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4814 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4815 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4817 | int partitioned; |
| 4818 | int shift; |
| 4819 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4820 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
| 4822 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4823 | return -ENODEV; |
| 4824 | |
| 4825 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4826 | shift = partitioned ? MdpMinorShift : 0; |
| 4827 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4829 | /* wait for any previous instance of this device to be |
| 4830 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4831 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4832 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4833 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4834 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4835 | error = -EEXIST; |
| 4836 | if (mddev->gendisk) |
| 4837 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4838 | |
| 4839 | if (name) { |
| 4840 | /* Need to ensure that 'name' is not a duplicate. |
| 4841 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4842 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4843 | spin_lock(&all_mddevs_lock); |
| 4844 | |
| 4845 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4846 | if (mddev2->gendisk && |
| 4847 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4848 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4849 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4850 | } |
| 4851 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4853 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4854 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4855 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4856 | if (!mddev->queue) |
| 4857 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4858 | mddev->queue->queuedata = mddev; |
| 4859 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4860 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4861 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | disk = alloc_disk(1 << shift); |
| 4864 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4865 | blk_cleanup_queue(mddev->queue); |
| 4866 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4867 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4869 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4871 | if (name) |
| 4872 | strcpy(disk->disk_name, name); |
| 4873 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4875 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | disk->fops = &md_fops; |
| 4878 | disk->private_data = mddev; |
| 4879 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4880 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4881 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4882 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4883 | * remove it now. |
| 4884 | */ |
| 4885 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4886 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4887 | /* As soon as we call add_disk(), another thread could get |
| 4888 | * through to md_open, so make sure it doesn't get too far |
| 4889 | */ |
| 4890 | mutex_lock(&mddev->open_mutex); |
| 4891 | add_disk(disk); |
| 4892 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4893 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4894 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4895 | if (error) { |
| 4896 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4897 | * __must_check, we must do something with the result |
| 4898 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4899 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4900 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4901 | error = 0; |
| 4902 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4903 | if (mddev->kobj.sd && |
| 4904 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4905 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4906 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4907 | abort: |
| 4908 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4909 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4910 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4911 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4912 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4913 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4914 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4918 | { |
| 4919 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | return NULL; |
| 4921 | } |
| 4922 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4923 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4924 | { |
| 4925 | /* val must be "md_*" where * is not all digits. |
| 4926 | * We allocate an array with a large free minor number, and |
| 4927 | * set the name to val. val must not already be an active name. |
| 4928 | */ |
| 4929 | int len = strlen(val); |
| 4930 | char buf[DISK_NAME_LEN]; |
| 4931 | |
| 4932 | while (len && val[len-1] == '\n') |
| 4933 | len--; |
| 4934 | if (len >= DISK_NAME_LEN) |
| 4935 | return -E2BIG; |
| 4936 | strlcpy(buf, val, len+1); |
| 4937 | if (strncmp(buf, "md_", 3) != 0) |
| 4938 | return -EINVAL; |
| 4939 | return md_alloc(0, buf); |
| 4940 | } |
| 4941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | static void md_safemode_timeout(unsigned long data) |
| 4943 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4944 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4946 | if (!atomic_read(&mddev->writes_pending)) { |
| 4947 | mddev->safemode = 1; |
| 4948 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4949 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | md_wakeup_thread(mddev->thread); |
| 4952 | } |
| 4953 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4954 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4956 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4958 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4959 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4960 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4962 | if (list_empty(&mddev->disks)) |
| 4963 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | |
| 4966 | if (mddev->pers) |
| 4967 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4968 | /* Cannot run until previous stop completes properly */ |
| 4969 | if (mddev->sysfs_active) |
| 4970 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | /* |
| 4973 | * Analyze all RAID superblock(s) |
| 4974 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4975 | if (!mddev->raid_disks) { |
| 4976 | if (!mddev->persistent) |
| 4977 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4978 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4981 | if (mddev->level != LEVEL_NONE) |
| 4982 | request_module("md-level-%d", mddev->level); |
| 4983 | else if (mddev->clevel[0]) |
| 4984 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | |
| 4986 | /* |
| 4987 | * Drop all container device buffers, from now on |
| 4988 | * the only valid external interface is through the md |
| 4989 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4991 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4992 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | continue; |
| 4994 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4995 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4996 | |
| 4997 | /* perform some consistency tests on the device. |
| 4998 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4999 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5000 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5001 | if (rdev->meta_bdev) { |
| 5002 | /* Nothing to check */; |
| 5003 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5004 | if (mddev->dev_sectors && |
| 5005 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5006 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5007 | printk("md: %s: data overlaps metadata\n", |
| 5008 | mdname(mddev)); |
| 5009 | return -EINVAL; |
| 5010 | } |
| 5011 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5012 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5013 | > rdev->data_offset) { |
| 5014 | printk("md: %s: metadata overlaps data\n", |
| 5015 | mdname(mddev)); |
| 5016 | return -EINVAL; |
| 5017 | } |
| 5018 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5019 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | } |
| 5021 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5022 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5023 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5026 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5027 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5029 | if (mddev->level != LEVEL_NONE) |
| 5030 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5031 | mddev->level); |
| 5032 | else |
| 5033 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5034 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | return -EINVAL; |
| 5036 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5037 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5039 | if (mddev->level != pers->level) { |
| 5040 | mddev->level = pers->level; |
| 5041 | mddev->new_level = pers->level; |
| 5042 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5043 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5045 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5046 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5047 | /* This personality cannot handle reshaping... */ |
| 5048 | mddev->pers = NULL; |
| 5049 | module_put(pers->owner); |
| 5050 | return -EINVAL; |
| 5051 | } |
| 5052 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5053 | if (pers->sync_request) { |
| 5054 | /* Warn if this is a potentially silly |
| 5055 | * configuration. |
| 5056 | */ |
| 5057 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5058 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5059 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5060 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5061 | rdev_for_each(rdev, mddev) |
| 5062 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5063 | if (rdev < rdev2 && |
| 5064 | rdev->bdev->bd_contains == |
| 5065 | rdev2->bdev->bd_contains) { |
| 5066 | printk(KERN_WARNING |
| 5067 | "%s: WARNING: %s appears to be" |
| 5068 | " on the same physical disk as" |
| 5069 | " %s.\n", |
| 5070 | mdname(mddev), |
| 5071 | bdevname(rdev->bdev,b), |
| 5072 | bdevname(rdev2->bdev,b2)); |
| 5073 | warned = 1; |
| 5074 | } |
| 5075 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5076 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5077 | if (warned) |
| 5078 | printk(KERN_WARNING |
| 5079 | "True protection against single-disk" |
| 5080 | " failure might be compromised.\n"); |
| 5081 | } |
| 5082 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5083 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5084 | /* may be over-ridden by personality */ |
| 5085 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5086 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5087 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5089 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5090 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5091 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5092 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5093 | if (err) |
| 5094 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5095 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5096 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5097 | " but 'external_size' not in effect?\n", __func__); |
| 5098 | printk(KERN_ERR |
| 5099 | "md: invalid array_size %llu > default size %llu\n", |
| 5100 | (unsigned long long)mddev->array_sectors / 2, |
| 5101 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5102 | err = -EINVAL; |
| 5103 | mddev->pers->stop(mddev); |
| 5104 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5105 | if (err == 0 && mddev->pers->sync_request && |
| 5106 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5107 | err = bitmap_create(mddev); |
| 5108 | if (err) { |
| 5109 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5110 | mdname(mddev), err); |
| 5111 | mddev->pers->stop(mddev); |
| 5112 | } |
| 5113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | module_put(mddev->pers->owner); |
| 5116 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5117 | bitmap_destroy(mddev); |
| 5118 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5120 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5121 | if (mddev->kobj.sd && |
| 5122 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5123 | printk(KERN_WARNING |
| 5124 | "md: cannot register extra attributes for %s\n", |
| 5125 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5126 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5127 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5128 | mddev->ro = 0; |
| 5129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5131 | atomic_set(&mddev->max_corr_read_errors, |
| 5132 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | mddev->safemode = 0; |
| 5134 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5135 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5136 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5138 | smp_wmb(); |
| 5139 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5140 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5141 | if (rdev->raid_disk >= 0) |
| 5142 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5143 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | |
| 5145 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5146 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5147 | if (mddev->flags) |
| 5148 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | |
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); |
| 5152 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5153 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | return 0; |
| 5155 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5156 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5158 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5159 | { |
| 5160 | int err; |
| 5161 | |
| 5162 | err = md_run(mddev); |
| 5163 | if (err) |
| 5164 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5165 | err = bitmap_load(mddev); |
| 5166 | if (err) { |
| 5167 | bitmap_destroy(mddev); |
| 5168 | goto out; |
| 5169 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5170 | |
| 5171 | md_wakeup_thread(mddev->thread); |
| 5172 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5173 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5174 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5175 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5176 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5177 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5178 | out: |
| 5179 | return err; |
| 5180 | } |
| 5181 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5182 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | { |
| 5184 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5186 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5188 | return -ENXIO; |
| 5189 | if (!mddev->pers) |
| 5190 | return -EINVAL; |
| 5191 | if (!mddev->ro) |
| 5192 | return -EBUSY; |
| 5193 | mddev->safemode = 0; |
| 5194 | mddev->ro = 0; |
| 5195 | set_disk_ro(disk, 0); |
| 5196 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5197 | mdname(mddev)); |
| 5198 | /* Kick recovery or resync if necessary */ |
| 5199 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5200 | md_wakeup_thread(mddev->thread); |
| 5201 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5202 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5203 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5206 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5207 | * to the file ourselves */ |
| 5208 | static int deny_bitmap_write_access(struct file * file) |
| 5209 | { |
| 5210 | struct inode *inode = file->f_mapping->host; |
| 5211 | |
| 5212 | spin_lock(&inode->i_lock); |
| 5213 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5214 | spin_unlock(&inode->i_lock); |
| 5215 | return -ETXTBSY; |
| 5216 | } |
| 5217 | atomic_set(&inode->i_writecount, -1); |
| 5218 | spin_unlock(&inode->i_lock); |
| 5219 | |
| 5220 | return 0; |
| 5221 | } |
| 5222 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5223 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5224 | { |
| 5225 | struct inode *inode = file->f_mapping->host; |
| 5226 | |
| 5227 | spin_lock(&inode->i_lock); |
| 5228 | atomic_set(&inode->i_writecount, 1); |
| 5229 | spin_unlock(&inode->i_lock); |
| 5230 | } |
| 5231 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5232 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5233 | { |
| 5234 | mddev->array_sectors = 0; |
| 5235 | mddev->external_size = 0; |
| 5236 | mddev->dev_sectors = 0; |
| 5237 | mddev->raid_disks = 0; |
| 5238 | mddev->recovery_cp = 0; |
| 5239 | mddev->resync_min = 0; |
| 5240 | mddev->resync_max = MaxSector; |
| 5241 | mddev->reshape_position = MaxSector; |
| 5242 | mddev->external = 0; |
| 5243 | mddev->persistent = 0; |
| 5244 | mddev->level = LEVEL_NONE; |
| 5245 | mddev->clevel[0] = 0; |
| 5246 | mddev->flags = 0; |
| 5247 | mddev->ro = 0; |
| 5248 | mddev->metadata_type[0] = 0; |
| 5249 | mddev->chunk_sectors = 0; |
| 5250 | mddev->ctime = mddev->utime = 0; |
| 5251 | mddev->layout = 0; |
| 5252 | mddev->max_disks = 0; |
| 5253 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5254 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5255 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5256 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5257 | mddev->new_level = LEVEL_NONE; |
| 5258 | mddev->new_layout = 0; |
| 5259 | mddev->new_chunk_sectors = 0; |
| 5260 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5261 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5262 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5263 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5264 | mddev->recovery = 0; |
| 5265 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5266 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5267 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5268 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5269 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5270 | mddev->bitmap_info.offset = 0; |
| 5271 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5272 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5273 | mddev->bitmap_info.chunksize = 0; |
| 5274 | mddev->bitmap_info.daemon_sleep = 0; |
| 5275 | mddev->bitmap_info.max_write_behind = 0; |
| 5276 | } |
| 5277 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5278 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5279 | { |
| 5280 | if (mddev->sync_thread) { |
| 5281 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5282 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5283 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | del_timer_sync(&mddev->safemode_timer); |
| 5287 | |
| 5288 | bitmap_flush(mddev); |
| 5289 | md_super_wait(mddev); |
| 5290 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5291 | if (mddev->ro == 0 && |
| 5292 | (!mddev->in_sync || mddev->flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5293 | /* mark array as shutdown cleanly */ |
| 5294 | mddev->in_sync = 1; |
| 5295 | md_update_sb(mddev, 1); |
| 5296 | } |
| 5297 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5298 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5299 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5300 | { |
| 5301 | mddev_lock(mddev); |
| 5302 | __md_stop_writes(mddev); |
| 5303 | mddev_unlock(mddev); |
| 5304 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5305 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5306 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5307 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5308 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5309 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5310 | mddev->pers->stop(mddev); |
| 5311 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5312 | mddev->to_remove = &md_redundancy_group; |
| 5313 | module_put(mddev->pers->owner); |
| 5314 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5315 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5316 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5317 | |
| 5318 | void md_stop(struct mddev *mddev) |
| 5319 | { |
| 5320 | /* stop the array and free an attached data structures. |
| 5321 | * This is called from dm-raid |
| 5322 | */ |
| 5323 | __md_stop(mddev); |
| 5324 | bitmap_destroy(mddev); |
| 5325 | if (mddev->bio_set) |
| 5326 | bioset_free(mddev->bio_set); |
| 5327 | } |
| 5328 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5329 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5330 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5331 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5332 | { |
| 5333 | int err = 0; |
| 5334 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5335 | if (atomic_read(&mddev->openers) > !!bdev) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5336 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5337 | err = -EBUSY; |
| 5338 | goto out; |
| 5339 | } |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5340 | if (bdev) |
| 5341 | sync_blockdev(bdev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5342 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5343 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5344 | |
| 5345 | err = -ENXIO; |
| 5346 | if (mddev->ro==1) |
| 5347 | goto out; |
| 5348 | mddev->ro = 1; |
| 5349 | set_disk_ro(mddev->gendisk, 1); |
| 5350 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5351 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5352 | err = 0; |
| 5353 | } |
| 5354 | out: |
| 5355 | mutex_unlock(&mddev->open_mutex); |
| 5356 | return err; |
| 5357 | } |
| 5358 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5359 | /* mode: |
| 5360 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5361 | * 2 - stop but do not disassemble array |
| 5362 | */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5363 | static int do_md_stop(struct mddev * mddev, int mode, |
| 5364 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5367 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5369 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5370 | if (atomic_read(&mddev->openers) > !!bdev || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5371 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5372 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5373 | mutex_unlock(&mddev->open_mutex); |
| 5374 | return -EBUSY; |
| 5375 | } |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5376 | if (bdev) |
| 5377 | /* It is possible IO was issued on some other |
| 5378 | * open file which was closed before we took ->open_mutex. |
| 5379 | * As that was not the last close __blkdev_put will not |
| 5380 | * have called sync_blockdev, so we must. |
| 5381 | */ |
| 5382 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5384 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5385 | if (mddev->ro) |
| 5386 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5387 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5388 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5389 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5390 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5391 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5392 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5393 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5394 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5395 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5396 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5397 | if (rdev->raid_disk >= 0) |
| 5398 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5399 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5400 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5401 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5402 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5403 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5404 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5405 | if (mddev->ro) |
| 5406 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5407 | } else |
| 5408 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | /* |
| 5410 | * Free resources if final stop |
| 5411 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5412 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5414 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5415 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5416 | if (mddev->bitmap_info.file) { |
| 5417 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5418 | fput(mddev->bitmap_info.file); |
| 5419 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5420 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5421 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | export_array(mddev); |
| 5424 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5425 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5426 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5427 | if (mddev->hold_active == UNTIL_STOP) |
| 5428 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5429 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5430 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5431 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5432 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5433 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5436 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5437 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5439 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | int err; |
| 5441 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5442 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | |
| 5445 | printk(KERN_INFO "md: running: "); |
| 5446 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5447 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | char b[BDEVNAME_SIZE]; |
| 5449 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5450 | } |
| 5451 | printk("\n"); |
| 5452 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5453 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | if (err) { |
| 5455 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5456 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | /* |
| 5461 | * lets try to run arrays based on all disks that have arrived |
| 5462 | * until now. (those are in pending_raid_disks) |
| 5463 | * |
| 5464 | * the method: pick the first pending disk, collect all disks with |
| 5465 | * the same UUID, remove all from the pending list and put them into |
| 5466 | * the 'same_array' list. Then order this list based on superblock |
| 5467 | * update time (freshest comes first), kick out 'old' disks and |
| 5468 | * compare superblocks. If everything's fine then run it. |
| 5469 | * |
| 5470 | * If "unit" is allocated, then bump its reference count |
| 5471 | */ |
| 5472 | static void autorun_devices(int part) |
| 5473 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5474 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5475 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | char b[BDEVNAME_SIZE]; |
| 5477 | |
| 5478 | printk(KERN_INFO "md: autorun ...\n"); |
| 5479 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5480 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5482 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5484 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
| 5486 | printk(KERN_INFO "md: considering %s ...\n", |
| 5487 | bdevname(rdev0->bdev,b)); |
| 5488 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5489 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5491 | printk(KERN_INFO "md: adding %s ...\n", |
| 5492 | bdevname(rdev->bdev,b)); |
| 5493 | list_move(&rdev->same_set, &candidates); |
| 5494 | } |
| 5495 | /* |
| 5496 | * now we have a set of devices, with all of them having |
| 5497 | * mostly sane superblocks. It's time to allocate the |
| 5498 | * mddev. |
| 5499 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5500 | if (part) { |
| 5501 | dev = MKDEV(mdp_major, |
| 5502 | rdev0->preferred_minor << MdpMinorShift); |
| 5503 | unit = MINOR(dev) >> MdpMinorShift; |
| 5504 | } else { |
| 5505 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5506 | unit = MINOR(dev); |
| 5507 | } |
| 5508 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5510 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5511 | break; |
| 5512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | |
| 5514 | md_probe(dev, NULL, NULL); |
| 5515 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5516 | if (!mddev || !mddev->gendisk) { |
| 5517 | if (mddev) |
| 5518 | mddev_put(mddev); |
| 5519 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | "md: cannot allocate memory for md drive.\n"); |
| 5521 | break; |
| 5522 | } |
| 5523 | if (mddev_lock(mddev)) |
| 5524 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5525 | mdname(mddev)); |
| 5526 | else if (mddev->raid_disks || mddev->major_version |
| 5527 | || !list_empty(&mddev->disks)) { |
| 5528 | printk(KERN_WARNING |
| 5529 | "md: %s already running, cannot run %s\n", |
| 5530 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5531 | mddev_unlock(mddev); |
| 5532 | } else { |
| 5533 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5534 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5535 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | list_del_init(&rdev->same_set); |
| 5537 | if (bind_rdev_to_array(rdev, mddev)) |
| 5538 | export_rdev(rdev); |
| 5539 | } |
| 5540 | autorun_array(mddev); |
| 5541 | mddev_unlock(mddev); |
| 5542 | } |
| 5543 | /* on success, candidates will be empty, on error |
| 5544 | * it won't... |
| 5545 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5546 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5547 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | mddev_put(mddev); |
| 5551 | } |
| 5552 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5553 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5554 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | static int get_version(void __user * arg) |
| 5557 | { |
| 5558 | mdu_version_t ver; |
| 5559 | |
| 5560 | ver.major = MD_MAJOR_VERSION; |
| 5561 | ver.minor = MD_MINOR_VERSION; |
| 5562 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5563 | |
| 5564 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5565 | return -EFAULT; |
| 5566 | |
| 5567 | return 0; |
| 5568 | } |
| 5569 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5570 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | { |
| 5572 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5573 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5574 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5576 | nr = working = insync = failed = spare = 0; |
| 5577 | rcu_read_lock(); |
| 5578 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5580 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | failed++; |
| 5582 | else { |
| 5583 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5584 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5585 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | else |
| 5587 | spare++; |
| 5588 | } |
| 5589 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5590 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | |
| 5592 | info.major_version = mddev->major_version; |
| 5593 | info.minor_version = mddev->minor_version; |
| 5594 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5595 | info.ctime = mddev->ctime; |
| 5596 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5597 | info.size = mddev->dev_sectors / 2; |
| 5598 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5599 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | info.nr_disks = nr; |
| 5601 | info.raid_disks = mddev->raid_disks; |
| 5602 | info.md_minor = mddev->md_minor; |
| 5603 | info.not_persistent= !mddev->persistent; |
| 5604 | |
| 5605 | info.utime = mddev->utime; |
| 5606 | info.state = 0; |
| 5607 | if (mddev->in_sync) |
| 5608 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5609 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5610 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5611 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | info.working_disks = working; |
| 5613 | info.failed_disks = failed; |
| 5614 | info.spare_disks = spare; |
| 5615 | |
| 5616 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5617 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | |
| 5619 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5620 | return -EFAULT; |
| 5621 | |
| 5622 | return 0; |
| 5623 | } |
| 5624 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5625 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5626 | { |
| 5627 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5628 | char *ptr, *buf = NULL; |
| 5629 | int err = -ENOMEM; |
| 5630 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5631 | if (md_allow_write(mddev)) |
| 5632 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5633 | else |
| 5634 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5635 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5636 | if (!file) |
| 5637 | goto out; |
| 5638 | |
| 5639 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5640 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5641 | file->pathname[0] = '\0'; |
| 5642 | goto copy_out; |
| 5643 | } |
| 5644 | |
| 5645 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5646 | if (!buf) |
| 5647 | goto out; |
| 5648 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5649 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5650 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5651 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5652 | goto out; |
| 5653 | |
| 5654 | strcpy(file->pathname, ptr); |
| 5655 | |
| 5656 | copy_out: |
| 5657 | err = 0; |
| 5658 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5659 | err = -EFAULT; |
| 5660 | out: |
| 5661 | kfree(buf); |
| 5662 | kfree(file); |
| 5663 | return err; |
| 5664 | } |
| 5665 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5666 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | { |
| 5668 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5669 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
| 5671 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5672 | return -EFAULT; |
| 5673 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5674 | rcu_read_lock(); |
| 5675 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | if (rdev) { |
| 5677 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5678 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5679 | info.raid_disk = rdev->raid_disk; |
| 5680 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5681 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5683 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5685 | info.state |= (1<<MD_DISK_SYNC); |
| 5686 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5687 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5688 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | } else { |
| 5690 | info.major = info.minor = 0; |
| 5691 | info.raid_disk = -1; |
| 5692 | info.state = (1<<MD_DISK_REMOVED); |
| 5693 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5694 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
| 5696 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5697 | return -EFAULT; |
| 5698 | |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5702 | 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] | 5703 | { |
| 5704 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5705 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | dev_t dev = MKDEV(info->major,info->minor); |
| 5707 | |
| 5708 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5709 | return -EOVERFLOW; |
| 5710 | |
| 5711 | if (!mddev->raid_disks) { |
| 5712 | int err; |
| 5713 | /* expecting a device which has a superblock */ |
| 5714 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5715 | if (IS_ERR(rdev)) { |
| 5716 | printk(KERN_WARNING |
| 5717 | "md: md_import_device returned %ld\n", |
| 5718 | PTR_ERR(rdev)); |
| 5719 | return PTR_ERR(rdev); |
| 5720 | } |
| 5721 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5722 | struct md_rdev *rdev0 |
| 5723 | = list_entry(mddev->disks.next, |
| 5724 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5725 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5727 | if (err < 0) { |
| 5728 | printk(KERN_WARNING |
| 5729 | "md: %s has different UUID to %s\n", |
| 5730 | bdevname(rdev->bdev,b), |
| 5731 | bdevname(rdev0->bdev,b2)); |
| 5732 | export_rdev(rdev); |
| 5733 | return -EINVAL; |
| 5734 | } |
| 5735 | } |
| 5736 | err = bind_rdev_to_array(rdev, mddev); |
| 5737 | if (err) |
| 5738 | export_rdev(rdev); |
| 5739 | return err; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * add_new_disk can be used once the array is assembled |
| 5744 | * to add "hot spares". They must already have a superblock |
| 5745 | * written |
| 5746 | */ |
| 5747 | if (mddev->pers) { |
| 5748 | int err; |
| 5749 | if (!mddev->pers->hot_add_disk) { |
| 5750 | printk(KERN_WARNING |
| 5751 | "%s: personality does not support diskops!\n", |
| 5752 | mdname(mddev)); |
| 5753 | return -EINVAL; |
| 5754 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5755 | if (mddev->persistent) |
| 5756 | rdev = md_import_device(dev, mddev->major_version, |
| 5757 | mddev->minor_version); |
| 5758 | else |
| 5759 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | if (IS_ERR(rdev)) { |
| 5761 | printk(KERN_WARNING |
| 5762 | "md: md_import_device returned %ld\n", |
| 5763 | PTR_ERR(rdev)); |
| 5764 | return PTR_ERR(rdev); |
| 5765 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5766 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5767 | if (!mddev->persistent) { |
| 5768 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5769 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5770 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5771 | set_bit(In_sync, &rdev->flags); |
| 5772 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5773 | rdev->raid_disk = -1; |
| 5774 | } else |
| 5775 | super_types[mddev->major_version]. |
| 5776 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5777 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5778 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5779 | /* This was a hot-add request, but events doesn't |
| 5780 | * match, so reject it. |
| 5781 | */ |
| 5782 | export_rdev(rdev); |
| 5783 | return -EINVAL; |
| 5784 | } |
| 5785 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5786 | if (test_bit(In_sync, &rdev->flags)) |
| 5787 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5788 | else |
| 5789 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5790 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5791 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5792 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5793 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5794 | else |
| 5795 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | rdev->raid_disk = -1; |
| 5798 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5799 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5800 | /* If there is hot_add_disk but no hot_remove_disk |
| 5801 | * then added disks for geometry changes, |
| 5802 | * and should be added immediately. |
| 5803 | */ |
| 5804 | super_types[mddev->major_version]. |
| 5805 | validate_super(mddev, rdev); |
| 5806 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5807 | if (err) |
| 5808 | unbind_rdev_from_array(rdev); |
| 5809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | if (err) |
| 5811 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5812 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5813 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5814 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5815 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5816 | if (mddev->degraded) |
| 5817 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5818 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5819 | if (!err) |
| 5820 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5821 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | return err; |
| 5823 | } |
| 5824 | |
| 5825 | /* otherwise, add_new_disk is only allowed |
| 5826 | * for major_version==0 superblocks |
| 5827 | */ |
| 5828 | if (mddev->major_version != 0) { |
| 5829 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5830 | mdname(mddev)); |
| 5831 | return -EINVAL; |
| 5832 | } |
| 5833 | |
| 5834 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5835 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5836 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | if (IS_ERR(rdev)) { |
| 5838 | printk(KERN_WARNING |
| 5839 | "md: error, md_import_device() returned %ld\n", |
| 5840 | PTR_ERR(rdev)); |
| 5841 | return PTR_ERR(rdev); |
| 5842 | } |
| 5843 | rdev->desc_nr = info->number; |
| 5844 | if (info->raid_disk < mddev->raid_disks) |
| 5845 | rdev->raid_disk = info->raid_disk; |
| 5846 | else |
| 5847 | rdev->raid_disk = -1; |
| 5848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5850 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5851 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5853 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5854 | set_bit(WriteMostly, &rdev->flags); |
| 5855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | if (!mddev->persistent) { |
| 5857 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5858 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5859 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5860 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5861 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5863 | err = bind_rdev_to_array(rdev, mddev); |
| 5864 | if (err) { |
| 5865 | export_rdev(rdev); |
| 5866 | return err; |
| 5867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | return 0; |
| 5871 | } |
| 5872 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5873 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | { |
| 5875 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5876 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | rdev = find_rdev(mddev, dev); |
| 5879 | if (!rdev) |
| 5880 | return -ENXIO; |
| 5881 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5882 | clear_bit(Blocked, &rdev->flags); |
| 5883 | remove_and_add_spares(mddev, rdev); |
| 5884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | if (rdev->raid_disk >= 0) |
| 5886 | goto busy; |
| 5887 | |
| 5888 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5889 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5890 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | return 0; |
| 5893 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5894 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5896 | return -EBUSY; |
| 5897 | } |
| 5898 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5899 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | { |
| 5901 | char b[BDEVNAME_SIZE]; |
| 5902 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5903 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | |
| 5905 | if (!mddev->pers) |
| 5906 | return -ENODEV; |
| 5907 | |
| 5908 | if (mddev->major_version != 0) { |
| 5909 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5910 | " version-0 superblocks.\n", |
| 5911 | mdname(mddev)); |
| 5912 | return -EINVAL; |
| 5913 | } |
| 5914 | if (!mddev->pers->hot_add_disk) { |
| 5915 | printk(KERN_WARNING |
| 5916 | "%s: personality does not support diskops!\n", |
| 5917 | mdname(mddev)); |
| 5918 | return -EINVAL; |
| 5919 | } |
| 5920 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5921 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | if (IS_ERR(rdev)) { |
| 5923 | printk(KERN_WARNING |
| 5924 | "md: error, md_import_device() returned %ld\n", |
| 5925 | PTR_ERR(rdev)); |
| 5926 | return -EINVAL; |
| 5927 | } |
| 5928 | |
| 5929 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5930 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5932 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5934 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5936 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | printk(KERN_WARNING |
| 5938 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5939 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5940 | err = -EINVAL; |
| 5941 | goto abort_export; |
| 5942 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5943 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5945 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5946 | err = bind_rdev_to_array(rdev, mddev); |
| 5947 | if (err) |
| 5948 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | |
| 5950 | /* |
| 5951 | * The rest should better be atomic, we can have disk failures |
| 5952 | * noticed in interrupt contexts ... |
| 5953 | */ |
| 5954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | rdev->raid_disk = -1; |
| 5956 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5957 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | |
| 5959 | /* |
| 5960 | * Kick recovery, maybe this spare has to be added to the |
| 5961 | * array immediately. |
| 5962 | */ |
| 5963 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5964 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5965 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | return 0; |
| 5967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | abort_export: |
| 5969 | export_rdev(rdev); |
| 5970 | return err; |
| 5971 | } |
| 5972 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5973 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5974 | { |
| 5975 | int err; |
| 5976 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5977 | if (mddev->pers) { |
| 5978 | if (!mddev->pers->quiesce) |
| 5979 | return -EBUSY; |
| 5980 | if (mddev->recovery || mddev->sync_thread) |
| 5981 | return -EBUSY; |
| 5982 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5983 | } |
| 5984 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5985 | |
| 5986 | if (fd >= 0) { |
| 5987 | if (mddev->bitmap) |
| 5988 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5989 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5990 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5991 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5992 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5993 | mdname(mddev)); |
| 5994 | return -EBADF; |
| 5995 | } |
| 5996 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5997 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5998 | if (err) { |
| 5999 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6000 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6001 | fput(mddev->bitmap_info.file); |
| 6002 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6003 | return err; |
| 6004 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6005 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6006 | } else if (mddev->bitmap == NULL) |
| 6007 | return -ENOENT; /* cannot remove what isn't there */ |
| 6008 | err = 0; |
| 6009 | if (mddev->pers) { |
| 6010 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6011 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6012 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6013 | if (!err) |
| 6014 | err = bitmap_load(mddev); |
| 6015 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6016 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6017 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6018 | fd = -1; /* make sure to put the file */ |
| 6019 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6020 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6021 | } |
| 6022 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6023 | if (mddev->bitmap_info.file) { |
| 6024 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 6025 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 6026 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6027 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6028 | } |
| 6029 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6030 | return err; |
| 6031 | } |
| 6032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | /* |
| 6034 | * set_array_info is used two different ways |
| 6035 | * The original usage is when creating a new array. |
| 6036 | * In this usage, raid_disks is > 0 and it together with |
| 6037 | * level, size, not_persistent,layout,chunksize determine the |
| 6038 | * shape of the array. |
| 6039 | * This will always create an array with a type-0.90.0 superblock. |
| 6040 | * The newer usage is when assembling an array. |
| 6041 | * In this case raid_disks will be 0, and the major_version field is |
| 6042 | * use to determine which style super-blocks are to be found on the devices. |
| 6043 | * The minor and patch _version numbers are also kept incase the |
| 6044 | * super_block handler wishes to interpret them. |
| 6045 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6046 | 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] | 6047 | { |
| 6048 | |
| 6049 | if (info->raid_disks == 0) { |
| 6050 | /* just setting version number for superblock loading */ |
| 6051 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6052 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | super_types[info->major_version].name == NULL) { |
| 6054 | /* maybe try to auto-load a module? */ |
| 6055 | printk(KERN_INFO |
| 6056 | "md: superblock version %d not known\n", |
| 6057 | info->major_version); |
| 6058 | return -EINVAL; |
| 6059 | } |
| 6060 | mddev->major_version = info->major_version; |
| 6061 | mddev->minor_version = info->minor_version; |
| 6062 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6063 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6064 | /* ensure mddev_put doesn't delete this now that there |
| 6065 | * is some minimal configuration. |
| 6066 | */ |
| 6067 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | return 0; |
| 6069 | } |
| 6070 | mddev->major_version = MD_MAJOR_VERSION; |
| 6071 | mddev->minor_version = MD_MINOR_VERSION; |
| 6072 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6073 | mddev->ctime = get_seconds(); |
| 6074 | |
| 6075 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6076 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6077 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | mddev->raid_disks = info->raid_disks; |
| 6079 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6080 | * openned |
| 6081 | */ |
| 6082 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6083 | mddev->recovery_cp = MaxSector; |
| 6084 | else |
| 6085 | mddev->recovery_cp = 0; |
| 6086 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6087 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | |
| 6089 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6090 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
| 6092 | mddev->max_disks = MD_SB_DISKS; |
| 6093 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6094 | if (mddev->persistent) |
| 6095 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6096 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6098 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6099 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6100 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6101 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6102 | mddev->reshape_position = MaxSector; |
| 6103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | /* |
| 6105 | * Generate a 128 bit UUID |
| 6106 | */ |
| 6107 | get_random_bytes(mddev->uuid, 16); |
| 6108 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6109 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6110 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6111 | mddev->new_layout = mddev->layout; |
| 6112 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6113 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | return 0; |
| 6116 | } |
| 6117 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6118 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6119 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6120 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6121 | |
| 6122 | if (mddev->external_size) |
| 6123 | return; |
| 6124 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6125 | mddev->array_sectors = array_sectors; |
| 6126 | } |
| 6127 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6128 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6129 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6130 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6131 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6132 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6133 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6134 | |
| 6135 | if (mddev->pers->resize == NULL) |
| 6136 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6137 | /* The "num_sectors" is the number of sectors of each device that |
| 6138 | * is used. This can only make sense for arrays with redundancy. |
| 6139 | * linear and raid0 always use whatever space is available. We can only |
| 6140 | * consider changing this number if no resync or reconstruction is |
| 6141 | * 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] | 6142 | * 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] | 6143 | * of each device. If num_sectors is zero, we find the largest size |
| 6144 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6145 | */ |
| 6146 | if (mddev->sync_thread) |
| 6147 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6148 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6149 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6150 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6151 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6152 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6153 | num_sectors = avail; |
| 6154 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6155 | return -ENOSPC; |
| 6156 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6157 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6158 | if (!rv) |
| 6159 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6160 | return rv; |
| 6161 | } |
| 6162 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6163 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6164 | { |
| 6165 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6166 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6167 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6168 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6169 | return -EINVAL; |
| 6170 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6171 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6172 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6173 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6174 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6175 | |
| 6176 | rdev_for_each(rdev, mddev) { |
| 6177 | if (mddev->raid_disks < raid_disks && |
| 6178 | rdev->data_offset < rdev->new_data_offset) |
| 6179 | return -EINVAL; |
| 6180 | if (mddev->raid_disks > raid_disks && |
| 6181 | rdev->data_offset > rdev->new_data_offset) |
| 6182 | return -EINVAL; |
| 6183 | } |
| 6184 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6185 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6186 | if (mddev->delta_disks < 0) |
| 6187 | mddev->reshape_backwards = 1; |
| 6188 | else if (mddev->delta_disks > 0) |
| 6189 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6190 | |
| 6191 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6192 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6193 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6194 | mddev->reshape_backwards = 0; |
| 6195 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6196 | return rv; |
| 6197 | } |
| 6198 | |
| 6199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | /* |
| 6201 | * update_array_info is used to change the configuration of an |
| 6202 | * on-line array. |
| 6203 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6204 | * fields in the info are checked against the array. |
| 6205 | * Any differences that cannot be handled will cause an error. |
| 6206 | * Normally, only one change can be managed at a time. |
| 6207 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6208 | 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] | 6209 | { |
| 6210 | int rv = 0; |
| 6211 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6212 | int state = 0; |
| 6213 | |
| 6214 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6215 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6216 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | |
| 6218 | if (mddev->major_version != info->major_version || |
| 6219 | mddev->minor_version != info->minor_version || |
| 6220 | /* mddev->patch_version != info->patch_version || */ |
| 6221 | mddev->ctime != info->ctime || |
| 6222 | mddev->level != info->level || |
| 6223 | /* mddev->layout != info->layout || */ |
| 6224 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6225 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6226 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6227 | ((state^info->state) & 0xfffffe00) |
| 6228 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | return -EINVAL; |
| 6230 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6231 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6232 | cnt++; |
| 6233 | if (mddev->raid_disks != info->raid_disks) |
| 6234 | cnt++; |
| 6235 | if (mddev->layout != info->layout) |
| 6236 | cnt++; |
| 6237 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6238 | cnt++; |
| 6239 | if (cnt == 0) |
| 6240 | return 0; |
| 6241 | if (cnt > 1) |
| 6242 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | |
| 6244 | if (mddev->layout != info->layout) { |
| 6245 | /* Change layout |
| 6246 | * we don't need to do anything at the md level, the |
| 6247 | * personality will take care of it all. |
| 6248 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6249 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6251 | else { |
| 6252 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6253 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6254 | if (rv) |
| 6255 | mddev->new_layout = mddev->layout; |
| 6256 | return rv; |
| 6257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6259 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6260 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6261 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6262 | if (mddev->raid_disks != info->raid_disks) |
| 6263 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6265 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6266 | if (mddev->pers->quiesce == NULL) |
| 6267 | return -EINVAL; |
| 6268 | if (mddev->recovery || mddev->sync_thread) |
| 6269 | return -EBUSY; |
| 6270 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6271 | /* add the bitmap */ |
| 6272 | if (mddev->bitmap) |
| 6273 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6274 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6275 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6276 | mddev->bitmap_info.offset = |
| 6277 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6278 | mddev->bitmap_info.space = |
| 6279 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6280 | mddev->pers->quiesce(mddev, 1); |
| 6281 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6282 | if (!rv) |
| 6283 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6284 | if (rv) |
| 6285 | bitmap_destroy(mddev); |
| 6286 | mddev->pers->quiesce(mddev, 0); |
| 6287 | } else { |
| 6288 | /* remove the bitmap */ |
| 6289 | if (!mddev->bitmap) |
| 6290 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6291 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6292 | return -EINVAL; |
| 6293 | mddev->pers->quiesce(mddev, 1); |
| 6294 | bitmap_destroy(mddev); |
| 6295 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6296 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6297 | } |
| 6298 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6299 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | return rv; |
| 6301 | } |
| 6302 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6303 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6305 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6306 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | |
| 6308 | if (mddev->pers == NULL) |
| 6309 | return -ENODEV; |
| 6310 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6311 | rcu_read_lock(); |
| 6312 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6314 | err = -ENODEV; |
| 6315 | else { |
| 6316 | md_error(mddev, rdev); |
| 6317 | if (!test_bit(Faulty, &rdev->flags)) |
| 6318 | err = -EBUSY; |
| 6319 | } |
| 6320 | rcu_read_unlock(); |
| 6321 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | } |
| 6323 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6324 | /* |
| 6325 | * We have a problem here : there is no easy way to give a CHS |
| 6326 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6327 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6328 | * dosfs just mad... ;-) |
| 6329 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6330 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6331 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6332 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6333 | |
| 6334 | geo->heads = 2; |
| 6335 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6336 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
| 6339 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6340 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | unsigned int cmd, unsigned long arg) |
| 6342 | { |
| 6343 | int err = 0; |
| 6344 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6345 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6346 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6348 | switch (cmd) { |
| 6349 | case RAID_VERSION: |
| 6350 | case GET_ARRAY_INFO: |
| 6351 | case GET_DISK_INFO: |
| 6352 | break; |
| 6353 | default: |
| 6354 | if (!capable(CAP_SYS_ADMIN)) |
| 6355 | return -EACCES; |
| 6356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | |
| 6358 | /* |
| 6359 | * Commands dealing with the RAID driver but not any |
| 6360 | * particular array: |
| 6361 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6362 | switch (cmd) { |
| 6363 | case RAID_VERSION: |
| 6364 | err = get_version(argp); |
| 6365 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6367 | case PRINT_RAID_DEBUG: |
| 6368 | err = 0; |
| 6369 | md_print_devices(); |
| 6370 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
| 6372 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6373 | case RAID_AUTORUN: |
| 6374 | err = 0; |
| 6375 | autostart_arrays(arg); |
| 6376 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6378 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | /* |
| 6382 | * Commands creating/starting a new array: |
| 6383 | */ |
| 6384 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6385 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | |
| 6387 | if (!mddev) { |
| 6388 | BUG(); |
| 6389 | goto abort; |
| 6390 | } |
| 6391 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6392 | /* Some actions do not requires the mutex */ |
| 6393 | switch (cmd) { |
| 6394 | case GET_ARRAY_INFO: |
| 6395 | if (!mddev->raid_disks && !mddev->external) |
| 6396 | err = -ENODEV; |
| 6397 | else |
| 6398 | err = get_array_info(mddev, argp); |
| 6399 | goto abort; |
| 6400 | |
| 6401 | case GET_DISK_INFO: |
| 6402 | if (!mddev->raid_disks && !mddev->external) |
| 6403 | err = -ENODEV; |
| 6404 | else |
| 6405 | err = get_disk_info(mddev, argp); |
| 6406 | goto abort; |
| 6407 | |
| 6408 | case SET_DISK_FAULTY: |
| 6409 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6410 | goto abort; |
| 6411 | } |
| 6412 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6413 | if (cmd == ADD_NEW_DISK) |
| 6414 | /* need to ensure md_delayed_delete() has completed */ |
| 6415 | flush_workqueue(md_misc_wq); |
| 6416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | err = mddev_lock(mddev); |
| 6418 | if (err) { |
| 6419 | printk(KERN_INFO |
| 6420 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6421 | err, cmd); |
| 6422 | goto abort; |
| 6423 | } |
| 6424 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6425 | if (cmd == SET_ARRAY_INFO) { |
| 6426 | mdu_array_info_t info; |
| 6427 | if (!arg) |
| 6428 | memset(&info, 0, sizeof(info)); |
| 6429 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6430 | err = -EFAULT; |
| 6431 | goto abort_unlock; |
| 6432 | } |
| 6433 | if (mddev->pers) { |
| 6434 | err = update_array_info(mddev, &info); |
| 6435 | if (err) { |
| 6436 | printk(KERN_WARNING "md: couldn't update" |
| 6437 | " array info. %d\n", err); |
| 6438 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | } |
| 6440 | goto done_unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6441 | } |
| 6442 | if (!list_empty(&mddev->disks)) { |
| 6443 | printk(KERN_WARNING |
| 6444 | "md: array %s already has disks!\n", |
| 6445 | mdname(mddev)); |
| 6446 | err = -EBUSY; |
| 6447 | goto abort_unlock; |
| 6448 | } |
| 6449 | if (mddev->raid_disks) { |
| 6450 | printk(KERN_WARNING |
| 6451 | "md: array %s already initialised!\n", |
| 6452 | mdname(mddev)); |
| 6453 | err = -EBUSY; |
| 6454 | goto abort_unlock; |
| 6455 | } |
| 6456 | err = set_array_info(mddev, &info); |
| 6457 | if (err) { |
| 6458 | printk(KERN_WARNING "md: couldn't set" |
| 6459 | " array info. %d\n", err); |
| 6460 | goto abort_unlock; |
| 6461 | } |
| 6462 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | /* |
| 6466 | * Commands querying/configuring an existing array: |
| 6467 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6468 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6469 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6470 | if ((!mddev->raid_disks && !mddev->external) |
| 6471 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6472 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6473 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | err = -ENODEV; |
| 6475 | goto abort_unlock; |
| 6476 | } |
| 6477 | |
| 6478 | /* |
| 6479 | * Commands even a read-only array can execute: |
| 6480 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6481 | switch (cmd) { |
| 6482 | case GET_BITMAP_FILE: |
| 6483 | err = get_bitmap_file(mddev, argp); |
| 6484 | goto done_unlock; |
| 6485 | |
| 6486 | case RESTART_ARRAY_RW: |
| 6487 | err = restart_array(mddev); |
| 6488 | goto done_unlock; |
| 6489 | |
| 6490 | case STOP_ARRAY: |
| 6491 | err = do_md_stop(mddev, 0, bdev); |
| 6492 | goto done_unlock; |
| 6493 | |
| 6494 | case STOP_ARRAY_RO: |
| 6495 | err = md_set_readonly(mddev, bdev); |
| 6496 | goto done_unlock; |
| 6497 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6498 | case HOT_REMOVE_DISK: |
| 6499 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6500 | goto done_unlock; |
| 6501 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6502 | case ADD_NEW_DISK: |
| 6503 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6504 | * on if we are re-adding a preexisting device. |
| 6505 | * So require mddev->pers and MD_DISK_SYNC. |
| 6506 | */ |
| 6507 | if (mddev->pers) { |
| 6508 | mdu_disk_info_t info; |
| 6509 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6510 | err = -EFAULT; |
| 6511 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6512 | /* Need to clear read-only for this */ |
| 6513 | break; |
| 6514 | else |
| 6515 | err = add_new_disk(mddev, &info); |
| 6516 | goto done_unlock; |
| 6517 | } |
| 6518 | break; |
| 6519 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6520 | case BLKROSET: |
| 6521 | if (get_user(ro, (int __user *)(arg))) { |
| 6522 | err = -EFAULT; |
| 6523 | goto done_unlock; |
| 6524 | } |
| 6525 | err = -EINVAL; |
| 6526 | |
| 6527 | /* if the bdev is going readonly the value of mddev->ro |
| 6528 | * does not matter, no writes are coming |
| 6529 | */ |
| 6530 | if (ro) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6531 | goto done_unlock; |
| 6532 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6533 | /* are we are already prepared for writes? */ |
| 6534 | if (mddev->ro != 1) |
| 6535 | goto done_unlock; |
| 6536 | |
| 6537 | /* transitioning to readauto need only happen for |
| 6538 | * arrays that call md_write_start |
| 6539 | */ |
| 6540 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6542 | if (err == 0) { |
| 6543 | mddev->ro = 2; |
| 6544 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6545 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6546 | } |
| 6547 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | /* |
| 6551 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6552 | * superblock, so we do not allow them on read-only arrays. |
| 6553 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6554 | * here and hit the 'default' below, so only disallow |
| 6555 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6557 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6558 | if (mddev->ro == 2) { |
| 6559 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6560 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6561 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6562 | /* mddev_unlock will wake thread */ |
| 6563 | /* If a device failed while we were read-only, we |
| 6564 | * need to make sure the metadata is updated now. |
| 6565 | */ |
| 6566 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6567 | mddev_unlock(mddev); |
| 6568 | wait_event(mddev->sb_wait, |
| 6569 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6570 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
| 6571 | mddev_lock(mddev); |
| 6572 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6573 | } else { |
| 6574 | err = -EROFS; |
| 6575 | goto abort_unlock; |
| 6576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | } |
| 6578 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6579 | switch (cmd) { |
| 6580 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6582 | mdu_disk_info_t info; |
| 6583 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6584 | err = -EFAULT; |
| 6585 | else |
| 6586 | err = add_new_disk(mddev, &info); |
| 6587 | goto done_unlock; |
| 6588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6590 | case HOT_ADD_DISK: |
| 6591 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6592 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6594 | case RUN_ARRAY: |
| 6595 | err = do_md_run(mddev); |
| 6596 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6598 | case SET_BITMAP_FILE: |
| 6599 | err = set_bitmap_file(mddev, (int)arg); |
| 6600 | goto done_unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6601 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6602 | default: |
| 6603 | err = -EINVAL; |
| 6604 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | done_unlock: |
| 6608 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6609 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6610 | err != -EINVAL) |
| 6611 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | mddev_unlock(mddev); |
| 6613 | |
| 6614 | return err; |
| 6615 | done: |
| 6616 | if (err) |
| 6617 | MD_BUG(); |
| 6618 | abort: |
| 6619 | return err; |
| 6620 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6621 | #ifdef CONFIG_COMPAT |
| 6622 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6623 | unsigned int cmd, unsigned long arg) |
| 6624 | { |
| 6625 | switch (cmd) { |
| 6626 | case HOT_REMOVE_DISK: |
| 6627 | case HOT_ADD_DISK: |
| 6628 | case SET_DISK_FAULTY: |
| 6629 | case SET_BITMAP_FILE: |
| 6630 | /* These take in integer arg, do not convert */ |
| 6631 | break; |
| 6632 | default: |
| 6633 | arg = (unsigned long)compat_ptr(arg); |
| 6634 | break; |
| 6635 | } |
| 6636 | |
| 6637 | return md_ioctl(bdev, mode, cmd, arg); |
| 6638 | } |
| 6639 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6641 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | { |
| 6643 | /* |
| 6644 | * Succeed if we can lock the mddev, which confirms that |
| 6645 | * it isn't being stopped right now. |
| 6646 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6647 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | int err; |
| 6649 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6650 | if (!mddev) |
| 6651 | return -ENODEV; |
| 6652 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6653 | if (mddev->gendisk != bdev->bd_disk) { |
| 6654 | /* we are racing with mddev_put which is discarding this |
| 6655 | * bd_disk. |
| 6656 | */ |
| 6657 | mddev_put(mddev); |
| 6658 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6659 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6660 | /* Then retry the open from the top */ |
| 6661 | return -ERESTARTSYS; |
| 6662 | } |
| 6663 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6664 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6665 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6666 | goto out; |
| 6667 | |
| 6668 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6669 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6670 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6672 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | out: |
| 6674 | return err; |
| 6675 | } |
| 6676 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6677 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6679 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6681 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6682 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | mddev_put(mddev); |
| 6684 | |
| 6685 | return 0; |
| 6686 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6687 | |
| 6688 | static int md_media_changed(struct gendisk *disk) |
| 6689 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6690 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6691 | |
| 6692 | return mddev->changed; |
| 6693 | } |
| 6694 | |
| 6695 | static int md_revalidate(struct gendisk *disk) |
| 6696 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6697 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6698 | |
| 6699 | mddev->changed = 0; |
| 6700 | return 0; |
| 6701 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6702 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | { |
| 6704 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6705 | .open = md_open, |
| 6706 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6707 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6708 | #ifdef CONFIG_COMPAT |
| 6709 | .compat_ioctl = md_compat_ioctl, |
| 6710 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6711 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6712 | .media_changed = md_media_changed, |
| 6713 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | }; |
| 6715 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6716 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6718 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | /* |
| 6721 | * md_thread is a 'system-thread', it's priority should be very |
| 6722 | * high. We avoid resource deadlocks individually in each |
| 6723 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6724 | * the very same RT priority as kswapd, thus we will never get |
| 6725 | * into a priority inversion deadlock. |
| 6726 | * |
| 6727 | * we definitely have to have equal or higher priority than |
| 6728 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6729 | * many dirty RAID5 blocks. |
| 6730 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6732 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6733 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6735 | /* We need to wait INTERRUPTIBLE so that |
| 6736 | * we don't add to the load-average. |
| 6737 | * That means we need to be sure no signals are |
| 6738 | * pending |
| 6739 | */ |
| 6740 | if (signal_pending(current)) |
| 6741 | flush_signals(current); |
| 6742 | |
| 6743 | wait_event_interruptible_timeout |
| 6744 | (thread->wqueue, |
| 6745 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6746 | || kthread_should_stop(), |
| 6747 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6749 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6750 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6751 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | return 0; |
| 6755 | } |
| 6756 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6757 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | { |
| 6759 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6760 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6762 | wake_up(&thread->wqueue); |
| 6763 | } |
| 6764 | } |
| 6765 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6766 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6767 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6769 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6771 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | if (!thread) |
| 6773 | return NULL; |
| 6774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | init_waitqueue_head(&thread->wqueue); |
| 6776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | thread->run = run; |
| 6778 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6779 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6780 | thread->tsk = kthread_run(md_thread, thread, |
| 6781 | "%s_%s", |
| 6782 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6783 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6784 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | kfree(thread); |
| 6786 | return NULL; |
| 6787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | return thread; |
| 6789 | } |
| 6790 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6791 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6793 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6794 | if (!thread) |
| 6795 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6796 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6797 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6798 | * non-existent thread |
| 6799 | */ |
| 6800 | spin_lock(&pers_lock); |
| 6801 | *threadp = NULL; |
| 6802 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6803 | |
| 6804 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | kfree(thread); |
| 6806 | } |
| 6807 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6808 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | { |
| 6810 | if (!mddev) { |
| 6811 | MD_BUG(); |
| 6812 | return; |
| 6813 | } |
| 6814 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6815 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6817 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6818 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | return; |
| 6820 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6821 | if (mddev->degraded) |
| 6822 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6823 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6825 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6826 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6827 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6828 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6829 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | } |
| 6831 | |
| 6832 | /* seq_file implementation /proc/mdstat */ |
| 6833 | |
| 6834 | static void status_unused(struct seq_file *seq) |
| 6835 | { |
| 6836 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6837 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
| 6839 | seq_printf(seq, "unused devices: "); |
| 6840 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6841 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | char b[BDEVNAME_SIZE]; |
| 6843 | i++; |
| 6844 | seq_printf(seq, "%s ", |
| 6845 | bdevname(rdev->bdev,b)); |
| 6846 | } |
| 6847 | if (!i) |
| 6848 | seq_printf(seq, "<none>"); |
| 6849 | |
| 6850 | seq_printf(seq, "\n"); |
| 6851 | } |
| 6852 | |
| 6853 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6854 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6856 | sector_t max_sectors, resync, res; |
| 6857 | unsigned long dt, db; |
| 6858 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6859 | int scale; |
| 6860 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6862 | if (mddev->curr_resync <= 3) |
| 6863 | resync = 0; |
| 6864 | else |
| 6865 | resync = mddev->curr_resync |
| 6866 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6868 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6869 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6870 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6872 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | |
| 6874 | /* |
| 6875 | * Should not happen. |
| 6876 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6877 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | MD_BUG(); |
| 6879 | return; |
| 6880 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6881 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6882 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6883 | * u32, as those are the requirements for sector_div. |
| 6884 | * Thus 'scale' must be at least 10 |
| 6885 | */ |
| 6886 | scale = 10; |
| 6887 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6888 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6889 | scale++; |
| 6890 | } |
| 6891 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6892 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6893 | |
| 6894 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6896 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | seq_printf(seq, "["); |
| 6898 | for (i = 0; i < x; i++) |
| 6899 | seq_printf(seq, "="); |
| 6900 | seq_printf(seq, ">"); |
| 6901 | for (i = 0; i < y; i++) |
| 6902 | seq_printf(seq, "."); |
| 6903 | seq_printf(seq, "] "); |
| 6904 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6905 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6906 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6907 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6908 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6909 | "check" : |
| 6910 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6911 | "resync" : "recovery"))), |
| 6912 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6913 | (unsigned long long) resync/2, |
| 6914 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | |
| 6916 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | * dt: time from mark until now |
| 6918 | * db: blocks written from mark until now |
| 6919 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6920 | * |
| 6921 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6922 | * So we divide before multiply in case it is 32bit and close |
| 6923 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6924 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6925 | * near the end of resync when the number of remaining sectors |
| 6926 | * is close to 'db'. |
| 6927 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6928 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | */ |
| 6930 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6931 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6932 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6933 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6934 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6935 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6936 | sector_div(rt, db/32+1); |
| 6937 | rt *= dt; |
| 6938 | rt >>= 5; |
| 6939 | |
| 6940 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6941 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6943 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6947 | { |
| 6948 | struct list_head *tmp; |
| 6949 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6950 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | |
| 6952 | if (l >= 0x10000) |
| 6953 | return NULL; |
| 6954 | if (!l--) |
| 6955 | /* header */ |
| 6956 | return (void*)1; |
| 6957 | |
| 6958 | spin_lock(&all_mddevs_lock); |
| 6959 | list_for_each(tmp,&all_mddevs) |
| 6960 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6961 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6962 | mddev_get(mddev); |
| 6963 | spin_unlock(&all_mddevs_lock); |
| 6964 | return mddev; |
| 6965 | } |
| 6966 | spin_unlock(&all_mddevs_lock); |
| 6967 | if (!l--) |
| 6968 | return (void*)2;/* tail */ |
| 6969 | return NULL; |
| 6970 | } |
| 6971 | |
| 6972 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6973 | { |
| 6974 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6975 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | |
| 6977 | ++*pos; |
| 6978 | if (v == (void*)2) |
| 6979 | return NULL; |
| 6980 | |
| 6981 | spin_lock(&all_mddevs_lock); |
| 6982 | if (v == (void*)1) |
| 6983 | tmp = all_mddevs.next; |
| 6984 | else |
| 6985 | tmp = mddev->all_mddevs.next; |
| 6986 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6987 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | else { |
| 6989 | next_mddev = (void*)2; |
| 6990 | *pos = 0x10000; |
| 6991 | } |
| 6992 | spin_unlock(&all_mddevs_lock); |
| 6993 | |
| 6994 | if (v != (void*)1) |
| 6995 | mddev_put(mddev); |
| 6996 | return next_mddev; |
| 6997 | |
| 6998 | } |
| 6999 | |
| 7000 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7001 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7002 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | |
| 7004 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7005 | mddev_put(mddev); |
| 7006 | } |
| 7007 | |
| 7008 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7009 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7010 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7011 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7012 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | |
| 7014 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7015 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | seq_printf(seq, "Personalities : "); |
| 7017 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7018 | list_for_each_entry(pers, &pers_list, list) |
| 7019 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7020 | |
| 7021 | spin_unlock(&pers_lock); |
| 7022 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7023 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | return 0; |
| 7025 | } |
| 7026 | if (v == (void*)2) { |
| 7027 | status_unused(seq); |
| 7028 | return 0; |
| 7029 | } |
| 7030 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7031 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7035 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7036 | mddev->pers ? "" : "in"); |
| 7037 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7038 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7040 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7041 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | seq_printf(seq, " %s", mddev->pers->name); |
| 7043 | } |
| 7044 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7045 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7046 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7047 | char b[BDEVNAME_SIZE]; |
| 7048 | seq_printf(seq, " %s[%d]", |
| 7049 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7050 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7051 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7052 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | seq_printf(seq, "(F)"); |
| 7054 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7055 | } |
| 7056 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7057 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7058 | if (test_bit(Replacement, &rdev->flags)) |
| 7059 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7060 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | if (!list_empty(&mddev->disks)) { |
| 7064 | if (mddev->pers) |
| 7065 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7066 | (unsigned long long) |
| 7067 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | else |
| 7069 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7070 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7071 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7072 | if (mddev->persistent) { |
| 7073 | if (mddev->major_version != 0 || |
| 7074 | mddev->minor_version != 90) { |
| 7075 | seq_printf(seq," super %d.%d", |
| 7076 | mddev->major_version, |
| 7077 | mddev->minor_version); |
| 7078 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7079 | } else if (mddev->external) |
| 7080 | seq_printf(seq, " super external:%s", |
| 7081 | mddev->metadata_type); |
| 7082 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7083 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7084 | |
| 7085 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7086 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7088 | if (mddev->pers->sync_request) { |
| 7089 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7090 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7091 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7092 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7093 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7094 | else if (mddev->recovery_cp < MaxSector) |
| 7095 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7096 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7097 | } else |
| 7098 | seq_printf(seq, "\n "); |
| 7099 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7100 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | |
| 7102 | seq_printf(seq, "\n"); |
| 7103 | } |
| 7104 | mddev_unlock(mddev); |
| 7105 | |
| 7106 | return 0; |
| 7107 | } |
| 7108 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7109 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | .start = md_seq_start, |
| 7111 | .next = md_seq_next, |
| 7112 | .stop = md_seq_stop, |
| 7113 | .show = md_seq_show, |
| 7114 | }; |
| 7115 | |
| 7116 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7117 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7118 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | int error; |
| 7120 | |
| 7121 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7122 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7123 | return error; |
| 7124 | |
| 7125 | seq = file->private_data; |
| 7126 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | return error; |
| 7128 | } |
| 7129 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7130 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7131 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7132 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7133 | int mask; |
| 7134 | |
| 7135 | poll_wait(filp, &md_event_waiters, wait); |
| 7136 | |
| 7137 | /* always allow read */ |
| 7138 | mask = POLLIN | POLLRDNORM; |
| 7139 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7140 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7141 | mask |= POLLERR | POLLPRI; |
| 7142 | return mask; |
| 7143 | } |
| 7144 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7145 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7146 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | .open = md_seq_open, |
| 7148 | .read = seq_read, |
| 7149 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7150 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7151 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | }; |
| 7153 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7154 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7157 | list_add_tail(&p->list, &pers_list); |
| 7158 | 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] | 7159 | spin_unlock(&pers_lock); |
| 7160 | return 0; |
| 7161 | } |
| 7162 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7163 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7165 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7167 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | spin_unlock(&pers_lock); |
| 7169 | return 0; |
| 7170 | } |
| 7171 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7172 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7173 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7174 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7176 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | |
| 7178 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7179 | rcu_read_lock(); |
| 7180 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7182 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7183 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7184 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7185 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7186 | * as sync_io is counted when a request starts, and |
| 7187 | * disk_stats is counted when it completes. |
| 7188 | * So resync activity will cause curr_events to be smaller than |
| 7189 | * when there was no such activity. |
| 7190 | * non-sync IO will cause disk_stat to increase without |
| 7191 | * increasing sync_io so curr_events will (eventually) |
| 7192 | * be larger than it was before. Once it becomes |
| 7193 | * substantially larger, the test below will cause |
| 7194 | * the array to appear non-idle, and resync will slow |
| 7195 | * down. |
| 7196 | * If there is a lot of outstanding resync activity when |
| 7197 | * we set last_event to curr_events, then all that activity |
| 7198 | * completing might cause the array to appear non-idle |
| 7199 | * and resync will be slowed down even though there might |
| 7200 | * not have been non-resync activity. This will only |
| 7201 | * happen once though. 'last_events' will soon reflect |
| 7202 | * the state where there is little or no outstanding |
| 7203 | * resync requests, and further resync activity will |
| 7204 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7205 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7207 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7208 | rdev->last_events = curr_events; |
| 7209 | idle = 0; |
| 7210 | } |
| 7211 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7212 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | return idle; |
| 7214 | } |
| 7215 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7216 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | { |
| 7218 | /* another "blocks" (512byte) blocks have been synced */ |
| 7219 | atomic_sub(blocks, &mddev->recovery_active); |
| 7220 | wake_up(&mddev->recovery_wait); |
| 7221 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7222 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7223 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | md_wakeup_thread(mddev->thread); |
| 7225 | // stop recovery, signal do_sync .... |
| 7226 | } |
| 7227 | } |
| 7228 | |
| 7229 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7230 | /* md_write_start(mddev, bi) |
| 7231 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7232 | * in superblock) before writing, schedule a superblock update |
| 7233 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7234 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7235 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7237 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7238 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7239 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7240 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7241 | BUG_ON(mddev->ro == 1); |
| 7242 | if (mddev->ro == 2) { |
| 7243 | /* need to switch to read/write */ |
| 7244 | mddev->ro = 0; |
| 7245 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7246 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7247 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7248 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7249 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7250 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7251 | if (mddev->safemode == 1) |
| 7252 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7253 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7254 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7255 | if (mddev->in_sync) { |
| 7256 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7257 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7258 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7259 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7260 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7261 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7262 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7263 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7264 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7265 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7266 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7267 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | } |
| 7269 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7270 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7271 | { |
| 7272 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7273 | if (mddev->safemode == 2) |
| 7274 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7275 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7276 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7277 | } |
| 7278 | } |
| 7279 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7280 | /* md_allow_write(mddev) |
| 7281 | * Calling this ensures that the array is marked 'active' so that writes |
| 7282 | * may proceed without blocking. It is important to call this before |
| 7283 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7284 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7285 | * |
| 7286 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7287 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7288 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7289 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7290 | { |
| 7291 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7292 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7293 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7294 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7295 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7296 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7297 | |
| 7298 | spin_lock_irq(&mddev->write_lock); |
| 7299 | if (mddev->in_sync) { |
| 7300 | mddev->in_sync = 0; |
| 7301 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7302 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7303 | if (mddev->safemode_delay && |
| 7304 | mddev->safemode == 0) |
| 7305 | mddev->safemode = 1; |
| 7306 | spin_unlock_irq(&mddev->write_lock); |
| 7307 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7308 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7309 | } else |
| 7310 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7311 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7312 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7313 | return -EAGAIN; |
| 7314 | else |
| 7315 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7316 | } |
| 7317 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | #define SYNC_MARKS 10 |
| 7320 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7321 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7322 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7324 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7325 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | unsigned int currspeed = 0, |
| 7327 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7328 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7330 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | sector_t mark_cnt[SYNC_MARKS]; |
| 7332 | int last_mark,m; |
| 7333 | struct list_head *tmp; |
| 7334 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7335 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7336 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7337 | char *desc; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7338 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7339 | |
| 7340 | /* just incase thread restarts... */ |
| 7341 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7342 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7343 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7344 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7346 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7347 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7348 | desc = "data-check"; |
| 7349 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7350 | desc = "requested-resync"; |
| 7351 | else |
| 7352 | desc = "resync"; |
| 7353 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7354 | desc = "reshape"; |
| 7355 | else |
| 7356 | desc = "recovery"; |
| 7357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | /* we overload curr_resync somewhat here. |
| 7359 | * 0 == not engaged in resync at all |
| 7360 | * 2 == checking that there is no conflict with another sync |
| 7361 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7362 | * commense |
| 7363 | * other == active in resync - this many blocks |
| 7364 | * |
| 7365 | * Before starting a resync we must have set curr_resync to |
| 7366 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7367 | * less than ours. When we find one that is the same or higher |
| 7368 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7369 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7370 | * This will mean we have to start checking from the beginning again. |
| 7371 | * |
| 7372 | */ |
| 7373 | |
| 7374 | do { |
| 7375 | mddev->curr_resync = 2; |
| 7376 | |
| 7377 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7378 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7379 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7380 | |
| 7381 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7383 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | if (mddev2 == mddev) |
| 7385 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7386 | if (!mddev->parallel_resync |
| 7387 | && mddev2->curr_resync |
| 7388 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | DEFINE_WAIT(wq); |
| 7390 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7391 | /* arbitrarily yield */ |
| 7392 | mddev->curr_resync = 1; |
| 7393 | wake_up(&resync_wait); |
| 7394 | } |
| 7395 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7396 | /* no need to wait here, we can wait the next |
| 7397 | * time 'round when curr_resync == 2 |
| 7398 | */ |
| 7399 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7400 | /* We need to wait 'interruptible' so as not to |
| 7401 | * contribute to the load average, and not to |
| 7402 | * be caught by 'softlockup' |
| 7403 | */ |
| 7404 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7405 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7406 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7407 | printk(KERN_INFO "md: delaying %s of %s" |
| 7408 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7410 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7412 | if (signal_pending(current)) |
| 7413 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | schedule(); |
| 7415 | finish_wait(&resync_wait, &wq); |
| 7416 | goto try_again; |
| 7417 | } |
| 7418 | finish_wait(&resync_wait, &wq); |
| 7419 | } |
| 7420 | } |
| 7421 | } while (mddev->curr_resync < 2); |
| 7422 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7423 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7424 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7426 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | */ |
| 7428 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7429 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7430 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7431 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7432 | j = mddev->resync_min; |
| 7433 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7434 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7435 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7436 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7437 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7438 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7440 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7441 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7442 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7443 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7444 | if (rdev->raid_disk >= 0 && |
| 7445 | !test_bit(Faulty, &rdev->flags) && |
| 7446 | !test_bit(In_sync, &rdev->flags) && |
| 7447 | rdev->recovery_offset < j) |
| 7448 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7449 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7452 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7453 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7454 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7455 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7456 | "(but not more than %d KB/sec) for %s.\n", |
| 7457 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7459 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7460 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7461 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7463 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7464 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | } |
| 7466 | last_mark = 0; |
| 7467 | mddev->resync_mark = mark[last_mark]; |
| 7468 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7469 | |
| 7470 | /* |
| 7471 | * Tune reconstruction: |
| 7472 | */ |
| 7473 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7474 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7475 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | |
| 7477 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | last_check = 0; |
| 7479 | |
| 7480 | if (j>2) { |
| 7481 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7482 | "md: resuming %s of %s from checkpoint.\n", |
| 7483 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7485 | } else |
| 7486 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7487 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7488 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7489 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7490 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7491 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7492 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7493 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7494 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7495 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7496 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7497 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7498 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7499 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7500 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7501 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7502 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7503 | (j - mddev->curr_resync_completed)*2 |
| 7504 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7505 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7506 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7507 | wait_event(mddev->recovery_wait, |
| 7508 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7509 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7510 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7511 | j > mddev->recovery_cp) |
| 7512 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7513 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7514 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7515 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7516 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7517 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7518 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7519 | /* As this condition is controlled by user-space, |
| 7520 | * we can block indefinitely, so use '_interruptible' |
| 7521 | * to avoid triggering warnings. |
| 7522 | */ |
| 7523 | flush_signals(current); /* just in case */ |
| 7524 | wait_event_interruptible(mddev->recovery_wait, |
| 7525 | mddev->resync_max > j |
| 7526 | || kthread_should_stop()); |
| 7527 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7528 | |
| 7529 | if (kthread_should_stop()) |
| 7530 | goto interrupted; |
| 7531 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7532 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7533 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7534 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7535 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | goto out; |
| 7537 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7538 | |
| 7539 | if (!skipped) { /* actual IO requested */ |
| 7540 | io_sectors += sectors; |
| 7541 | atomic_add(sectors, &mddev->recovery_active); |
| 7542 | } |
| 7543 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7544 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7545 | break; |
| 7546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7548 | if (j > 2) |
| 7549 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7550 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7551 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7552 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7553 | * visible in /proc/mdstat |
| 7554 | */ |
| 7555 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7556 | |
| 7557 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7558 | continue; |
| 7559 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7560 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | repeat: |
| 7562 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7563 | /* step marks */ |
| 7564 | int next = (last_mark+1) % SYNC_MARKS; |
| 7565 | |
| 7566 | mddev->resync_mark = mark[next]; |
| 7567 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7568 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7569 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7570 | last_mark = next; |
| 7571 | } |
| 7572 | |
| 7573 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7574 | if (kthread_should_stop()) |
| 7575 | goto interrupted; |
| 7576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7577 | |
| 7578 | /* |
| 7579 | * this loop exits only if either when we are slower than |
| 7580 | * the 'hard' speed limit, or the system was IO-idle for |
| 7581 | * a jiffy. |
| 7582 | * the system might be non-idle CPU-wise, but we only care |
| 7583 | * about not overloading the IO subsystem. (things like an |
| 7584 | * e2fsck being done on the RAID array should execute fast) |
| 7585 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | cond_resched(); |
| 7587 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7588 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7589 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7590 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7591 | if (currspeed > speed_min(mddev)) { |
| 7592 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7593 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7594 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7595 | goto repeat; |
| 7596 | } |
| 7597 | } |
| 7598 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7599 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7600 | /* |
| 7601 | * this also signals 'finished resyncing' to md_stop |
| 7602 | */ |
| 7603 | out: |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7604 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7605 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7606 | |
| 7607 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7608 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7609 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7610 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7611 | mddev->curr_resync > 2) { |
| 7612 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7613 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7614 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7615 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7616 | "md: checkpointing %s of %s.\n", |
| 7617 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7618 | if (test_bit(MD_RECOVERY_ERROR, |
| 7619 | &mddev->recovery)) |
| 7620 | mddev->recovery_cp = |
| 7621 | mddev->curr_resync_completed; |
| 7622 | else |
| 7623 | mddev->recovery_cp = |
| 7624 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7625 | } |
| 7626 | } else |
| 7627 | mddev->recovery_cp = MaxSector; |
| 7628 | } else { |
| 7629 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7630 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7631 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7632 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7633 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7634 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7635 | !test_bit(Faulty, &rdev->flags) && |
| 7636 | !test_bit(In_sync, &rdev->flags) && |
| 7637 | rdev->recovery_offset < mddev->curr_resync) |
| 7638 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7639 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7641 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7642 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7643 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7644 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7645 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7646 | /* We completed so min/max setting can be forgotten if used. */ |
| 7647 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7648 | mddev->resync_min = 0; |
| 7649 | mddev->resync_max = MaxSector; |
| 7650 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7651 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7652 | mddev->curr_resync = 0; |
| 7653 | wake_up(&resync_wait); |
| 7654 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7655 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7656 | return; |
| 7657 | |
| 7658 | interrupted: |
| 7659 | /* |
| 7660 | * got a signal, exit. |
| 7661 | */ |
| 7662 | printk(KERN_INFO |
| 7663 | "md: md_do_sync() got signal ... exiting\n"); |
| 7664 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7665 | goto out; |
| 7666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7667 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7668 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7670 | static int remove_and_add_spares(struct mddev *mddev, |
| 7671 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7672 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7673 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7674 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7675 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7676 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7677 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7678 | if ((this == NULL || rdev == this) && |
| 7679 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7680 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7681 | (test_bit(Faulty, &rdev->flags) || |
| 7682 | ! test_bit(In_sync, &rdev->flags)) && |
| 7683 | atomic_read(&rdev->nr_pending)==0) { |
| 7684 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7685 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7686 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7687 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7688 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7689 | } |
| 7690 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7691 | if (removed && mddev->kobj.sd) |
| 7692 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7693 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7694 | if (this) |
| 7695 | goto no_add; |
| 7696 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7697 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7698 | if (rdev->raid_disk >= 0 && |
| 7699 | !test_bit(In_sync, &rdev->flags) && |
| 7700 | !test_bit(Faulty, &rdev->flags)) |
| 7701 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7702 | if (rdev->raid_disk >= 0) |
| 7703 | continue; |
| 7704 | if (test_bit(Faulty, &rdev->flags)) |
| 7705 | continue; |
| 7706 | if (mddev->ro && |
| 7707 | rdev->saved_raid_disk < 0) |
| 7708 | continue; |
| 7709 | |
| 7710 | rdev->recovery_offset = 0; |
| 7711 | if (rdev->saved_raid_disk >= 0 && mddev->in_sync) { |
| 7712 | spin_lock_irq(&mddev->write_lock); |
| 7713 | if (mddev->in_sync) |
| 7714 | /* OK, this device, which is in_sync, |
| 7715 | * will definitely be noticed before |
| 7716 | * the next write, so recovery isn't |
| 7717 | * needed. |
| 7718 | */ |
| 7719 | rdev->recovery_offset = mddev->recovery_cp; |
| 7720 | spin_unlock_irq(&mddev->write_lock); |
| 7721 | } |
| 7722 | if (mddev->ro && rdev->recovery_offset != MaxSector) |
| 7723 | /* not safe to add this disk now */ |
| 7724 | continue; |
| 7725 | if (mddev->pers-> |
| 7726 | hot_add_disk(mddev, rdev) == 0) { |
| 7727 | if (sysfs_link_rdev(mddev, rdev)) |
| 7728 | /* failure here is OK */; |
| 7729 | spares++; |
| 7730 | md_new_event(mddev); |
| 7731 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7732 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7733 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7734 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7735 | if (removed) |
| 7736 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7737 | return spares; |
| 7738 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7740 | /* |
| 7741 | * This routine is regularly called by all per-raid-array threads to |
| 7742 | * deal with generic issues like resync and super-block update. |
| 7743 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7744 | * need this as they never do any recovery or update the superblock. |
| 7745 | * |
| 7746 | * It does not do any resync itself, but rather "forks" off other threads |
| 7747 | * to do that as needed. |
| 7748 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7749 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7750 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7751 | * and wakeups up this thread which will reap the thread and finish up. |
| 7752 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7753 | * |
| 7754 | * The overall approach is: |
| 7755 | * 1/ if the superblock needs updating, update it. |
| 7756 | * 2/ If a recovery thread is running, don't do anything else. |
| 7757 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7758 | * 4/ If there are any faulty devices, remove them. |
| 7759 | * 5/ If array is degraded, try to add spares devices |
| 7760 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7761 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7762 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7763 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7764 | if (mddev->suspended) |
| 7765 | return; |
| 7766 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7767 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7768 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7769 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7770 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7771 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7772 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7773 | mdname(mddev)); |
| 7774 | mddev->safemode = 2; |
| 7775 | } |
| 7776 | flush_signals(current); |
| 7777 | } |
| 7778 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7779 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7780 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7781 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7782 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7783 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7784 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7785 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7786 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7787 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7788 | )) |
| 7789 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7790 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7791 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7792 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7793 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7794 | if (mddev->ro) { |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7795 | /* On a read-only array we can: |
| 7796 | * - remove failed devices |
| 7797 | * - add already-in_sync devices if the array itself |
| 7798 | * is in-sync. |
| 7799 | * As we only add devices that are already in-sync, |
| 7800 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7801 | */ |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7802 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7803 | remove_and_add_spares(mddev, NULL); |
| 7804 | mddev->pers->spare_active(mddev); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7805 | goto unlock; |
| 7806 | } |
| 7807 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7808 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7809 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7810 | spin_lock_irq(&mddev->write_lock); |
| 7811 | if (mddev->safemode && |
| 7812 | !atomic_read(&mddev->writes_pending) && |
| 7813 | !mddev->in_sync && |
| 7814 | mddev->recovery_cp == MaxSector) { |
| 7815 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7816 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7817 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7818 | } |
| 7819 | if (mddev->safemode == 1) |
| 7820 | mddev->safemode = 0; |
| 7821 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7822 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7823 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7824 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7825 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7826 | if (mddev->flags) |
| 7827 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7830 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7831 | /* resync/recovery still happening */ |
| 7832 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7833 | goto unlock; |
| 7834 | } |
| 7835 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7836 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7837 | goto unlock; |
| 7838 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7839 | /* Set RUNNING before clearing NEEDED to avoid |
| 7840 | * any transients in the value of "sync_action". |
| 7841 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7842 | mddev->curr_resync_completed = 0; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7843 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7844 | /* Clear some bits that don't mean anything, but |
| 7845 | * might be left set |
| 7846 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7847 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7848 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7850 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7851 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7852 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7853 | /* no recovery is running. |
| 7854 | * remove any failed drives, then |
| 7855 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7856 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | * the personality to fail the re-add. |
| 7858 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7859 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7860 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7861 | if (mddev->pers->check_reshape == NULL || |
| 7862 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7863 | /* Cannot proceed */ |
| 7864 | goto unlock; |
| 7865 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7866 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7867 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7868 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7869 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7870 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7871 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7872 | } else if (mddev->recovery_cp < MaxSector) { |
| 7873 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7874 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7875 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7876 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7879 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7880 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7881 | /* We are adding a device or devices to an array |
| 7882 | * which has the bitmap stored on all devices. |
| 7883 | * So make sure all bitmap pages get written |
| 7884 | */ |
| 7885 | bitmap_write_all(mddev->bitmap); |
| 7886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7887 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7888 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7889 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | if (!mddev->sync_thread) { |
| 7891 | printk(KERN_ERR "%s: could not start resync" |
| 7892 | " thread...\n", |
| 7893 | mdname(mddev)); |
| 7894 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7895 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7896 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7897 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7898 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7899 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7900 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7901 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7902 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7903 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7904 | } |
| 7905 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7906 | if (!mddev->sync_thread) { |
| 7907 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7908 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7909 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7910 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7911 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | mddev_unlock(mddev); |
| 7914 | } |
| 7915 | } |
| 7916 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7917 | void md_reap_sync_thread(struct mddev *mddev) |
| 7918 | { |
| 7919 | struct md_rdev *rdev; |
| 7920 | |
| 7921 | /* resync has finished, collect result */ |
| 7922 | md_unregister_thread(&mddev->sync_thread); |
| 7923 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7924 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7925 | /* success...*/ |
| 7926 | /* activate any spares */ |
| 7927 | if (mddev->pers->spare_active(mddev)) { |
| 7928 | sysfs_notify(&mddev->kobj, NULL, |
| 7929 | "degraded"); |
| 7930 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7931 | } |
| 7932 | } |
| 7933 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7934 | mddev->pers->finish_reshape) |
| 7935 | mddev->pers->finish_reshape(mddev); |
| 7936 | |
| 7937 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7938 | * information must be scrapped. Also if any device is now |
| 7939 | * In_sync we must scrape the saved_raid_disk for that device |
| 7940 | * do the superblock for an incrementally recovered device |
| 7941 | * written out. |
| 7942 | */ |
| 7943 | rdev_for_each(rdev, mddev) |
| 7944 | if (!mddev->degraded || |
| 7945 | test_bit(In_sync, &rdev->flags)) |
| 7946 | rdev->saved_raid_disk = -1; |
| 7947 | |
| 7948 | md_update_sb(mddev, 1); |
| 7949 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7950 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7951 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7952 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7953 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7954 | /* flag recovery needed just to double check */ |
| 7955 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7956 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7957 | md_new_event(mddev); |
| 7958 | if (mddev->event_work.func) |
| 7959 | queue_work(md_misc_wq, &mddev->event_work); |
| 7960 | } |
| 7961 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7962 | 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] | 7963 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7964 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7965 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7966 | !test_bit(Blocked, &rdev->flags) && |
| 7967 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7968 | msecs_to_jiffies(5000)); |
| 7969 | rdev_dec_pending(rdev, mddev); |
| 7970 | } |
| 7971 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7972 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7973 | void md_finish_reshape(struct mddev *mddev) |
| 7974 | { |
| 7975 | /* called be personality module when reshape completes. */ |
| 7976 | struct md_rdev *rdev; |
| 7977 | |
| 7978 | rdev_for_each(rdev, mddev) { |
| 7979 | if (rdev->data_offset > rdev->new_data_offset) |
| 7980 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7981 | else |
| 7982 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7983 | rdev->data_offset = rdev->new_data_offset; |
| 7984 | } |
| 7985 | } |
| 7986 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7987 | |
| 7988 | /* Bad block management. |
| 7989 | * We can record which blocks on each device are 'bad' and so just |
| 7990 | * fail those blocks, or that stripe, rather than the whole device. |
| 7991 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7992 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7993 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7994 | * A 'shift' can be set so that larger blocks are tracked and |
| 7995 | * consequently larger devices can be covered. |
| 7996 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7997 | * |
| 7998 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7999 | * might need to retry if it is very unlucky. |
| 8000 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8001 | * so we use the write_seqlock_irq variant. |
| 8002 | * |
| 8003 | * When looking for a bad block we specify a range and want to |
| 8004 | * know if any block in the range is bad. So we binary-search |
| 8005 | * to the last range that starts at-or-before the given endpoint, |
| 8006 | * (or "before the sector after the target range") |
| 8007 | * then see if it ends after the given start. |
| 8008 | * We return |
| 8009 | * 0 if there are no known bad blocks in the range |
| 8010 | * 1 if there are known bad block which are all acknowledged |
| 8011 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8012 | * plus the start/length of the first bad section we overlap. |
| 8013 | */ |
| 8014 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8015 | sector_t *first_bad, int *bad_sectors) |
| 8016 | { |
| 8017 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8018 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8019 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8020 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8021 | sector_t target = s + sectors; |
| 8022 | unsigned seq; |
| 8023 | |
| 8024 | if (bb->shift > 0) { |
| 8025 | /* round the start down, and the end up */ |
| 8026 | s >>= bb->shift; |
| 8027 | target += (1<<bb->shift) - 1; |
| 8028 | target >>= bb->shift; |
| 8029 | sectors = target - s; |
| 8030 | } |
| 8031 | /* 'target' is now the first block after the bad range */ |
| 8032 | |
| 8033 | retry: |
| 8034 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8035 | lo = 0; |
| 8036 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8037 | hi = bb->count; |
| 8038 | |
| 8039 | /* Binary search between lo and hi for 'target' |
| 8040 | * i.e. for the last range that starts before 'target' |
| 8041 | */ |
| 8042 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8043 | * are known not to be the last range before target. |
| 8044 | * VARIANT: hi-lo is the number of possible |
| 8045 | * ranges, and decreases until it reaches 1 |
| 8046 | */ |
| 8047 | while (hi - lo > 1) { |
| 8048 | int mid = (lo + hi) / 2; |
| 8049 | sector_t a = BB_OFFSET(p[mid]); |
| 8050 | if (a < target) |
| 8051 | /* This could still be the one, earlier ranges |
| 8052 | * could not. */ |
| 8053 | lo = mid; |
| 8054 | else |
| 8055 | /* This and later ranges are definitely out. */ |
| 8056 | hi = mid; |
| 8057 | } |
| 8058 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8059 | if (hi > lo) { |
| 8060 | /* need to check all range that end after 's' to see if |
| 8061 | * any are unacknowledged. |
| 8062 | */ |
| 8063 | while (lo >= 0 && |
| 8064 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8065 | if (BB_OFFSET(p[lo]) < target) { |
| 8066 | /* starts before the end, and finishes after |
| 8067 | * the start, so they must overlap |
| 8068 | */ |
| 8069 | if (rv != -1 && BB_ACK(p[lo])) |
| 8070 | rv = 1; |
| 8071 | else |
| 8072 | rv = -1; |
| 8073 | *first_bad = BB_OFFSET(p[lo]); |
| 8074 | *bad_sectors = BB_LEN(p[lo]); |
| 8075 | } |
| 8076 | lo--; |
| 8077 | } |
| 8078 | } |
| 8079 | |
| 8080 | if (read_seqretry(&bb->lock, seq)) |
| 8081 | goto retry; |
| 8082 | |
| 8083 | return rv; |
| 8084 | } |
| 8085 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8086 | |
| 8087 | /* |
| 8088 | * Add a range of bad blocks to the table. |
| 8089 | * This might extend the table, or might contract it |
| 8090 | * if two adjacent ranges can be merged. |
| 8091 | * We binary-search to find the 'insertion' point, then |
| 8092 | * decide how best to handle it. |
| 8093 | */ |
| 8094 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8095 | int acknowledged) |
| 8096 | { |
| 8097 | u64 *p; |
| 8098 | int lo, hi; |
| 8099 | int rv = 1; |
| 8100 | |
| 8101 | if (bb->shift < 0) |
| 8102 | /* badblocks are disabled */ |
| 8103 | return 0; |
| 8104 | |
| 8105 | if (bb->shift) { |
| 8106 | /* round the start down, and the end up */ |
| 8107 | sector_t next = s + sectors; |
| 8108 | s >>= bb->shift; |
| 8109 | next += (1<<bb->shift) - 1; |
| 8110 | next >>= bb->shift; |
| 8111 | sectors = next - s; |
| 8112 | } |
| 8113 | |
| 8114 | write_seqlock_irq(&bb->lock); |
| 8115 | |
| 8116 | p = bb->page; |
| 8117 | lo = 0; |
| 8118 | hi = bb->count; |
| 8119 | /* Find the last range that starts at-or-before 's' */ |
| 8120 | while (hi - lo > 1) { |
| 8121 | int mid = (lo + hi) / 2; |
| 8122 | sector_t a = BB_OFFSET(p[mid]); |
| 8123 | if (a <= s) |
| 8124 | lo = mid; |
| 8125 | else |
| 8126 | hi = mid; |
| 8127 | } |
| 8128 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8129 | hi = lo; |
| 8130 | |
| 8131 | if (hi > lo) { |
| 8132 | /* we found a range that might merge with the start |
| 8133 | * of our new range |
| 8134 | */ |
| 8135 | sector_t a = BB_OFFSET(p[lo]); |
| 8136 | sector_t e = a + BB_LEN(p[lo]); |
| 8137 | int ack = BB_ACK(p[lo]); |
| 8138 | if (e >= s) { |
| 8139 | /* Yes, we can merge with a previous range */ |
| 8140 | if (s == a && s + sectors >= e) |
| 8141 | /* new range covers old */ |
| 8142 | ack = acknowledged; |
| 8143 | else |
| 8144 | ack = ack && acknowledged; |
| 8145 | |
| 8146 | if (e < s + sectors) |
| 8147 | e = s + sectors; |
| 8148 | if (e - a <= BB_MAX_LEN) { |
| 8149 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8150 | s = e; |
| 8151 | } else { |
| 8152 | /* does not all fit in one range, |
| 8153 | * make p[lo] maximal |
| 8154 | */ |
| 8155 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8156 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8157 | s = a + BB_MAX_LEN; |
| 8158 | } |
| 8159 | sectors = e - s; |
| 8160 | } |
| 8161 | } |
| 8162 | if (sectors && hi < bb->count) { |
| 8163 | /* 'hi' points to the first range that starts after 's'. |
| 8164 | * Maybe we can merge with the start of that range */ |
| 8165 | sector_t a = BB_OFFSET(p[hi]); |
| 8166 | sector_t e = a + BB_LEN(p[hi]); |
| 8167 | int ack = BB_ACK(p[hi]); |
| 8168 | if (a <= s + sectors) { |
| 8169 | /* merging is possible */ |
| 8170 | if (e <= s + sectors) { |
| 8171 | /* full overlap */ |
| 8172 | e = s + sectors; |
| 8173 | ack = acknowledged; |
| 8174 | } else |
| 8175 | ack = ack && acknowledged; |
| 8176 | |
| 8177 | a = s; |
| 8178 | if (e - a <= BB_MAX_LEN) { |
| 8179 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8180 | s = e; |
| 8181 | } else { |
| 8182 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8183 | s = a + BB_MAX_LEN; |
| 8184 | } |
| 8185 | sectors = e - s; |
| 8186 | lo = hi; |
| 8187 | hi++; |
| 8188 | } |
| 8189 | } |
| 8190 | if (sectors == 0 && hi < bb->count) { |
| 8191 | /* we might be able to combine lo and hi */ |
| 8192 | /* Note: 's' is at the end of 'lo' */ |
| 8193 | sector_t a = BB_OFFSET(p[hi]); |
| 8194 | int lolen = BB_LEN(p[lo]); |
| 8195 | int hilen = BB_LEN(p[hi]); |
| 8196 | int newlen = lolen + hilen - (s - a); |
| 8197 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8198 | /* yes, we can combine them */ |
| 8199 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8200 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8201 | memmove(p + hi, p + hi + 1, |
| 8202 | (bb->count - hi - 1) * 8); |
| 8203 | bb->count--; |
| 8204 | } |
| 8205 | } |
| 8206 | while (sectors) { |
| 8207 | /* didn't merge (it all). |
| 8208 | * Need to add a range just before 'hi' */ |
| 8209 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8210 | /* No room for more */ |
| 8211 | rv = 0; |
| 8212 | break; |
| 8213 | } else { |
| 8214 | int this_sectors = sectors; |
| 8215 | memmove(p + hi + 1, p + hi, |
| 8216 | (bb->count - hi) * 8); |
| 8217 | bb->count++; |
| 8218 | |
| 8219 | if (this_sectors > BB_MAX_LEN) |
| 8220 | this_sectors = BB_MAX_LEN; |
| 8221 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8222 | sectors -= this_sectors; |
| 8223 | s += this_sectors; |
| 8224 | } |
| 8225 | } |
| 8226 | |
| 8227 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8228 | if (!acknowledged) |
| 8229 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8230 | write_sequnlock_irq(&bb->lock); |
| 8231 | |
| 8232 | return rv; |
| 8233 | } |
| 8234 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8235 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8236 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8237 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8238 | int rv; |
| 8239 | if (is_new) |
| 8240 | s += rdev->new_data_offset; |
| 8241 | else |
| 8242 | s += rdev->data_offset; |
| 8243 | rv = md_set_badblocks(&rdev->badblocks, |
| 8244 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8245 | if (rv) { |
| 8246 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8247 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8248 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8249 | md_wakeup_thread(rdev->mddev->thread); |
| 8250 | } |
| 8251 | return rv; |
| 8252 | } |
| 8253 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8254 | |
| 8255 | /* |
| 8256 | * Remove a range of bad blocks from the table. |
| 8257 | * This may involve extending the table if we spilt a region, |
| 8258 | * but it must not fail. So if the table becomes full, we just |
| 8259 | * drop the remove request. |
| 8260 | */ |
| 8261 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8262 | { |
| 8263 | u64 *p; |
| 8264 | int lo, hi; |
| 8265 | sector_t target = s + sectors; |
| 8266 | int rv = 0; |
| 8267 | |
| 8268 | if (bb->shift > 0) { |
| 8269 | /* When clearing we round the start up and the end down. |
| 8270 | * This should not matter as the shift should align with |
| 8271 | * the block size and no rounding should ever be needed. |
| 8272 | * However it is better the think a block is bad when it |
| 8273 | * isn't than to think a block is not bad when it is. |
| 8274 | */ |
| 8275 | s += (1<<bb->shift) - 1; |
| 8276 | s >>= bb->shift; |
| 8277 | target >>= bb->shift; |
| 8278 | sectors = target - s; |
| 8279 | } |
| 8280 | |
| 8281 | write_seqlock_irq(&bb->lock); |
| 8282 | |
| 8283 | p = bb->page; |
| 8284 | lo = 0; |
| 8285 | hi = bb->count; |
| 8286 | /* Find the last range that starts before 'target' */ |
| 8287 | while (hi - lo > 1) { |
| 8288 | int mid = (lo + hi) / 2; |
| 8289 | sector_t a = BB_OFFSET(p[mid]); |
| 8290 | if (a < target) |
| 8291 | lo = mid; |
| 8292 | else |
| 8293 | hi = mid; |
| 8294 | } |
| 8295 | if (hi > lo) { |
| 8296 | /* p[lo] is the last range that could overlap the |
| 8297 | * current range. Earlier ranges could also overlap, |
| 8298 | * but only this one can overlap the end of the range. |
| 8299 | */ |
| 8300 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8301 | /* Partial overlap, leave the tail of this range */ |
| 8302 | int ack = BB_ACK(p[lo]); |
| 8303 | sector_t a = BB_OFFSET(p[lo]); |
| 8304 | sector_t end = a + BB_LEN(p[lo]); |
| 8305 | |
| 8306 | if (a < s) { |
| 8307 | /* we need to split this range */ |
| 8308 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8309 | rv = 0; |
| 8310 | goto out; |
| 8311 | } |
| 8312 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8313 | bb->count++; |
| 8314 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8315 | lo++; |
| 8316 | } |
| 8317 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8318 | /* there is no longer an overlap */ |
| 8319 | hi = lo; |
| 8320 | lo--; |
| 8321 | } |
| 8322 | while (lo >= 0 && |
| 8323 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8324 | /* This range does overlap */ |
| 8325 | if (BB_OFFSET(p[lo]) < s) { |
| 8326 | /* Keep the early parts of this range. */ |
| 8327 | int ack = BB_ACK(p[lo]); |
| 8328 | sector_t start = BB_OFFSET(p[lo]); |
| 8329 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8330 | /* now low doesn't overlap, so.. */ |
| 8331 | break; |
| 8332 | } |
| 8333 | lo--; |
| 8334 | } |
| 8335 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8336 | * anything between needs to be discarded |
| 8337 | */ |
| 8338 | if (hi - lo > 1) { |
| 8339 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8340 | bb->count -= (hi - lo - 1); |
| 8341 | } |
| 8342 | } |
| 8343 | |
| 8344 | bb->changed = 1; |
| 8345 | out: |
| 8346 | write_sequnlock_irq(&bb->lock); |
| 8347 | return rv; |
| 8348 | } |
| 8349 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8350 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8351 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8352 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8353 | if (is_new) |
| 8354 | s += rdev->new_data_offset; |
| 8355 | else |
| 8356 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8357 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8358 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8359 | } |
| 8360 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8361 | |
| 8362 | /* |
| 8363 | * Acknowledge all bad blocks in a list. |
| 8364 | * This only succeeds if ->changed is clear. It is used by |
| 8365 | * in-kernel metadata updates |
| 8366 | */ |
| 8367 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8368 | { |
| 8369 | if (bb->page == NULL || bb->changed) |
| 8370 | /* no point even trying */ |
| 8371 | return; |
| 8372 | write_seqlock_irq(&bb->lock); |
| 8373 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8374 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8375 | u64 *p = bb->page; |
| 8376 | int i; |
| 8377 | for (i = 0; i < bb->count ; i++) { |
| 8378 | if (!BB_ACK(p[i])) { |
| 8379 | sector_t start = BB_OFFSET(p[i]); |
| 8380 | int len = BB_LEN(p[i]); |
| 8381 | p[i] = BB_MAKE(start, len, 1); |
| 8382 | } |
| 8383 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8384 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8385 | } |
| 8386 | write_sequnlock_irq(&bb->lock); |
| 8387 | } |
| 8388 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8389 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8390 | /* sysfs access to bad-blocks list. |
| 8391 | * We present two files. |
| 8392 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8393 | * are recorded as bad. The list is truncated to fit within |
| 8394 | * the one-page limit of sysfs. |
| 8395 | * Writing "sector length" to this file adds an acknowledged |
| 8396 | * bad block list. |
| 8397 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8398 | * been acknowledged. Writing to this file adds bad blocks |
| 8399 | * without acknowledging them. This is largely for testing. |
| 8400 | */ |
| 8401 | |
| 8402 | static ssize_t |
| 8403 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8404 | { |
| 8405 | size_t len; |
| 8406 | int i; |
| 8407 | u64 *p = bb->page; |
| 8408 | unsigned seq; |
| 8409 | |
| 8410 | if (bb->shift < 0) |
| 8411 | return 0; |
| 8412 | |
| 8413 | retry: |
| 8414 | seq = read_seqbegin(&bb->lock); |
| 8415 | |
| 8416 | len = 0; |
| 8417 | i = 0; |
| 8418 | |
| 8419 | while (len < PAGE_SIZE && i < bb->count) { |
| 8420 | sector_t s = BB_OFFSET(p[i]); |
| 8421 | unsigned int length = BB_LEN(p[i]); |
| 8422 | int ack = BB_ACK(p[i]); |
| 8423 | i++; |
| 8424 | |
| 8425 | if (unack && ack) |
| 8426 | continue; |
| 8427 | |
| 8428 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8429 | (unsigned long long)s << bb->shift, |
| 8430 | length << bb->shift); |
| 8431 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8432 | if (unack && len == 0) |
| 8433 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8434 | |
| 8435 | if (read_seqretry(&bb->lock, seq)) |
| 8436 | goto retry; |
| 8437 | |
| 8438 | return len; |
| 8439 | } |
| 8440 | |
| 8441 | #define DO_DEBUG 1 |
| 8442 | |
| 8443 | static ssize_t |
| 8444 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8445 | { |
| 8446 | unsigned long long sector; |
| 8447 | int length; |
| 8448 | char newline; |
| 8449 | #ifdef DO_DEBUG |
| 8450 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8451 | * Normally only a successful write may clear a badblock |
| 8452 | */ |
| 8453 | int clear = 0; |
| 8454 | if (page[0] == '-') { |
| 8455 | clear = 1; |
| 8456 | page++; |
| 8457 | } |
| 8458 | #endif /* DO_DEBUG */ |
| 8459 | |
| 8460 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8461 | case 3: |
| 8462 | if (newline != '\n') |
| 8463 | return -EINVAL; |
| 8464 | case 2: |
| 8465 | if (length <= 0) |
| 8466 | return -EINVAL; |
| 8467 | break; |
| 8468 | default: |
| 8469 | return -EINVAL; |
| 8470 | } |
| 8471 | |
| 8472 | #ifdef DO_DEBUG |
| 8473 | if (clear) { |
| 8474 | md_clear_badblocks(bb, sector, length); |
| 8475 | return len; |
| 8476 | } |
| 8477 | #endif /* DO_DEBUG */ |
| 8478 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8479 | return len; |
| 8480 | else |
| 8481 | return -ENOSPC; |
| 8482 | } |
| 8483 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8484 | static int md_notify_reboot(struct notifier_block *this, |
| 8485 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8486 | { |
| 8487 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8488 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8489 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8490 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8491 | for_each_mddev(mddev, tmp) { |
| 8492 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8493 | if (mddev->pers) |
| 8494 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8495 | mddev->safemode = 2; |
| 8496 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8497 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8498 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8499 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8500 | /* |
| 8501 | * certain more exotic SCSI devices are known to be |
| 8502 | * volatile wrt too early system reboots. While the |
| 8503 | * right place to handle this issue is the given |
| 8504 | * driver, we do want to have a safe RAID driver ... |
| 8505 | */ |
| 8506 | if (need_delay) |
| 8507 | mdelay(1000*1); |
| 8508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8509 | return NOTIFY_DONE; |
| 8510 | } |
| 8511 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8512 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8513 | .notifier_call = md_notify_reboot, |
| 8514 | .next = NULL, |
| 8515 | .priority = INT_MAX, /* before any real devices */ |
| 8516 | }; |
| 8517 | |
| 8518 | static void md_geninit(void) |
| 8519 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8520 | 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] | 8521 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8522 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8523 | } |
| 8524 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8525 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8526 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8527 | int ret = -ENOMEM; |
| 8528 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8529 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8530 | if (!md_wq) |
| 8531 | goto err_wq; |
| 8532 | |
| 8533 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8534 | if (!md_misc_wq) |
| 8535 | goto err_misc_wq; |
| 8536 | |
| 8537 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8538 | goto err_md; |
| 8539 | |
| 8540 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8541 | goto err_mdp; |
| 8542 | mdp_major = ret; |
| 8543 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8544 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8545 | md_probe, NULL, NULL); |
| 8546 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8547 | md_probe, NULL, NULL); |
| 8548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8549 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8550 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8551 | |
| 8552 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8553 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8554 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8555 | err_mdp: |
| 8556 | unregister_blkdev(MD_MAJOR, "md"); |
| 8557 | err_md: |
| 8558 | destroy_workqueue(md_misc_wq); |
| 8559 | err_misc_wq: |
| 8560 | destroy_workqueue(md_wq); |
| 8561 | err_wq: |
| 8562 | return ret; |
| 8563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8564 | |
| 8565 | #ifndef MODULE |
| 8566 | |
| 8567 | /* |
| 8568 | * Searches all registered partitions for autorun RAID arrays |
| 8569 | * at boot time. |
| 8570 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8571 | |
| 8572 | static LIST_HEAD(all_detected_devices); |
| 8573 | struct detected_devices_node { |
| 8574 | struct list_head list; |
| 8575 | dev_t dev; |
| 8576 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8577 | |
| 8578 | void md_autodetect_dev(dev_t dev) |
| 8579 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8580 | struct detected_devices_node *node_detected_dev; |
| 8581 | |
| 8582 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8583 | if (node_detected_dev) { |
| 8584 | node_detected_dev->dev = dev; |
| 8585 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8586 | } else { |
| 8587 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8588 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | } |
| 8591 | |
| 8592 | |
| 8593 | static void autostart_arrays(int part) |
| 8594 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8595 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8596 | struct detected_devices_node *node_detected_dev; |
| 8597 | dev_t dev; |
| 8598 | int i_scanned, i_passed; |
| 8599 | |
| 8600 | i_scanned = 0; |
| 8601 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8602 | |
| 8603 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8604 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8605 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8606 | i_scanned++; |
| 8607 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8608 | struct detected_devices_node, list); |
| 8609 | list_del(&node_detected_dev->list); |
| 8610 | dev = node_detected_dev->dev; |
| 8611 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8612 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8613 | if (IS_ERR(rdev)) |
| 8614 | continue; |
| 8615 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8616 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8617 | MD_BUG(); |
| 8618 | continue; |
| 8619 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8620 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8621 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8622 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8623 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8624 | |
| 8625 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8626 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8627 | |
| 8628 | autorun_devices(part); |
| 8629 | } |
| 8630 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8631 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8632 | |
| 8633 | static __exit void md_exit(void) |
| 8634 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8635 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8636 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8637 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8638 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8639 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8640 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8641 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8642 | unregister_blkdev(mdp_major, "mdp"); |
| 8643 | unregister_reboot_notifier(&md_notifier); |
| 8644 | unregister_sysctl_table(raid_table_header); |
| 8645 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8646 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8647 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8648 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8649 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8650 | destroy_workqueue(md_misc_wq); |
| 8651 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8652 | } |
| 8653 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8654 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8655 | module_exit(md_exit) |
| 8656 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8657 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8658 | { |
| 8659 | return sprintf(buffer, "%d", start_readonly); |
| 8660 | } |
| 8661 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8662 | { |
| 8663 | char *e; |
| 8664 | int num = simple_strtoul(val, &e, 10); |
| 8665 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8666 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8667 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8668 | } |
| 8669 | return -EINVAL; |
| 8670 | } |
| 8671 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8672 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8673 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8674 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8675 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8677 | EXPORT_SYMBOL(register_md_personality); |
| 8678 | EXPORT_SYMBOL(unregister_md_personality); |
| 8679 | EXPORT_SYMBOL(md_error); |
| 8680 | EXPORT_SYMBOL(md_done_sync); |
| 8681 | EXPORT_SYMBOL(md_write_start); |
| 8682 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8683 | EXPORT_SYMBOL(md_register_thread); |
| 8684 | EXPORT_SYMBOL(md_unregister_thread); |
| 8685 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8686 | EXPORT_SYMBOL(md_check_recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8687 | EXPORT_SYMBOL(md_reap_sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8688 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8689 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8690 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8691 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |