Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 61 | /* pers_list is a list of registered personalities protected |
| 62 | * by pers_lock. |
| 63 | * pers_lock does extra service to protect accesses to |
| 64 | * mddev->thread when the mutex cannot be held. |
| 65 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 66 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(pers_lock); |
| 68 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | static void md_print_devices(void); |
| 70 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 72 | static struct workqueue_struct *md_wq; |
| 73 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 78 | * Default number of read corrections we'll attempt on an rdev |
| 79 | * before ejecting it from the array. We divide the read error |
| 80 | * count by 2 for every hour elapsed between read errors. |
| 81 | */ |
| 82 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 85 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 86 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 87 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 89 | * speed limit - in case reconstruction slows down your system despite |
| 90 | * idle IO detection. |
| 91 | * |
| 92 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 93 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | |
| 96 | static int sysctl_speed_limit_min = 1000; |
| 97 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 98 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | { |
| 100 | return mddev->sync_speed_min ? |
| 101 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 102 | } |
| 103 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_max ? |
| 107 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | static struct ctl_table_header *raid_table_header; |
| 111 | |
| 112 | static ctl_table raid_table[] = { |
| 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .procname = "speed_limit_min", |
| 115 | .data = &sysctl_speed_limit_min, |
| 116 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 117 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 118 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }, |
| 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 125 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 127 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .procname = "raid", |
| 133 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 134 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .child = raid_table, |
| 136 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 137 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static ctl_table raid_root_table[] = { |
| 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .procname = "dev", |
| 143 | .maxlen = 0, |
| 144 | .mode = 0555, |
| 145 | .child = raid_dir_table, |
| 146 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 147 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 150 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 152 | static int start_readonly; |
| 153 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 154 | /* bio_clone_mddev |
| 155 | * like bio_clone, but with a local bio set |
| 156 | */ |
| 157 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 158 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 159 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | { |
| 161 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 162 | |
| 163 | if (!mddev || !mddev->bio_set) |
| 164 | return bio_alloc(gfp_mask, nr_iovecs); |
| 165 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 166 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 167 | if (!b) |
| 168 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 169 | return b; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 172 | |
| 173 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 174 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 176 | if (!mddev || !mddev->bio_set) |
| 177 | return bio_clone(bio, gfp_mask); |
| 178 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 179 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 182 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 183 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 184 | { |
| 185 | /* 'bio' is a cloned bio which we need to trim to match |
| 186 | * the given offset and size. |
| 187 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 188 | */ |
| 189 | int i; |
| 190 | struct bio_vec *bvec; |
| 191 | int sofar = 0; |
| 192 | |
| 193 | size <<= 9; |
| 194 | if (offset == 0 && size == bio->bi_size) |
| 195 | return; |
| 196 | |
| 197 | bio->bi_sector += offset; |
| 198 | bio->bi_size = size; |
| 199 | offset <<= 9; |
| 200 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 201 | |
| 202 | while (bio->bi_idx < bio->bi_vcnt && |
| 203 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 204 | /* remove this whole bio_vec */ |
| 205 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 206 | bio->bi_idx++; |
| 207 | } |
| 208 | if (bio->bi_idx < bio->bi_vcnt) { |
| 209 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 210 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 211 | } |
| 212 | /* avoid any complications with bi_idx being non-zero*/ |
| 213 | if (bio->bi_idx) { |
| 214 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 215 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 216 | bio->bi_vcnt -= bio->bi_idx; |
| 217 | bio->bi_idx = 0; |
| 218 | } |
| 219 | /* Make sure vcnt and last bv are not too big */ |
| 220 | bio_for_each_segment(bvec, bio, i) { |
| 221 | if (sofar + bvec->bv_len > size) |
| 222 | bvec->bv_len = size - sofar; |
| 223 | if (bvec->bv_len == 0) { |
| 224 | bio->bi_vcnt = i; |
| 225 | break; |
| 226 | } |
| 227 | sofar += bvec->bv_len; |
| 228 | } |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 233 | * We have a system wide 'event count' that is incremented |
| 234 | * on any 'interesting' event, and readers of /proc/mdstat |
| 235 | * can use 'poll' or 'select' to find out when the event |
| 236 | * count increases. |
| 237 | * |
| 238 | * Events are: |
| 239 | * start array, stop array, error, add device, remove device, |
| 240 | * start build, activate spare |
| 241 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 242 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 243 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 244 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 245 | { |
| 246 | atomic_inc(&md_event_count); |
| 247 | wake_up(&md_event_waiters); |
| 248 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 249 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 250 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 251 | /* Alternate version that can be called from interrupts |
| 252 | * when calling sysfs_notify isn't needed. |
| 253 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 254 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 255 | { |
| 256 | atomic_inc(&md_event_count); |
| 257 | wake_up(&md_event_waiters); |
| 258 | } |
| 259 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 260 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * Enables to iterate over all existing md arrays |
| 262 | * all_mddevs_lock protects this list. |
| 263 | */ |
| 264 | static LIST_HEAD(all_mddevs); |
| 265 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 266 | |
| 267 | |
| 268 | /* |
| 269 | * iterates through all used mddevs in the system. |
| 270 | * We take care to grab the all_mddevs_lock whenever navigating |
| 271 | * the list, and to always hold a refcount when unlocked. |
| 272 | * Any code which breaks out of this loop while own |
| 273 | * a reference to the current mddev and must mddev_put it. |
| 274 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 275 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | \ |
| 277 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 278 | _tmp = all_mddevs.next; \ |
| 279 | _mddev = NULL;}); \ |
| 280 | ({ if (_tmp != &all_mddevs) \ |
| 281 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 283 | if (_mddev) mddev_put(_mddev); \ |
| 284 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 285 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 287 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | ) |
| 289 | |
| 290 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 291 | /* Rather than calling directly into the personality make_request function, |
| 292 | * IO requests come here first so that we can check if the device is |
| 293 | * being suspended pending a reconfiguration. |
| 294 | * We hold a refcount over the call to ->make_request. By the time that |
| 295 | * call has finished, the bio has been linked into some internal structure |
| 296 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 297 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 298 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 300 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 301 | struct mddev *mddev = q->queuedata; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 302 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 303 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 304 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 305 | if (mddev == NULL || mddev->pers == NULL |
| 306 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 307 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 308 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 310 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 311 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 312 | return; |
| 313 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 314 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 315 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 316 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 317 | DEFINE_WAIT(__wait); |
| 318 | for (;;) { |
| 319 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 320 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 321 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 322 | break; |
| 323 | rcu_read_unlock(); |
| 324 | schedule(); |
| 325 | rcu_read_lock(); |
| 326 | } |
| 327 | finish_wait(&mddev->sb_wait, &__wait); |
| 328 | } |
| 329 | atomic_inc(&mddev->active_io); |
| 330 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 331 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 332 | /* |
| 333 | * save the sectors now since our bio can |
| 334 | * go away inside make_request |
| 335 | */ |
| 336 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 337 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 338 | |
| 339 | cpu = part_stat_lock(); |
| 340 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 341 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 342 | part_stat_unlock(); |
| 343 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 344 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 345 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 348 | /* mddev_suspend makes sure no new requests are submitted |
| 349 | * to the device, and that any requests that have been submitted |
| 350 | * are completely handled. |
| 351 | * Once ->stop is called and completes, the module will be completely |
| 352 | * unused. |
| 353 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 354 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 355 | { |
| 356 | BUG_ON(mddev->suspended); |
| 357 | mddev->suspended = 1; |
| 358 | synchronize_rcu(); |
| 359 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 360 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 361 | |
| 362 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 363 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 364 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 365 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 366 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 367 | { |
| 368 | mddev->suspended = 0; |
| 369 | wake_up(&mddev->sb_wait); |
| 370 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 371 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 372 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 373 | md_wakeup_thread(mddev->thread); |
| 374 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 375 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 376 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 377 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 378 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 379 | { |
| 380 | return mddev->suspended; |
| 381 | } |
| 382 | EXPORT_SYMBOL(mddev_congested); |
| 383 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 385 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | */ |
| 387 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 388 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 390 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 391 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 392 | |
| 393 | rdev_dec_pending(rdev, mddev); |
| 394 | |
| 395 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 396 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 397 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 398 | } |
| 399 | bio_put(bio); |
| 400 | } |
| 401 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 402 | static void md_submit_flush_data(struct work_struct *ws); |
| 403 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 404 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 405 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 406 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 407 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 409 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 410 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 412 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | if (rdev->raid_disk >= 0 && |
| 414 | !test_bit(Faulty, &rdev->flags)) { |
| 415 | /* Take two references, one is dropped |
| 416 | * when request finishes, one after |
| 417 | * we reclaim rcu_read_lock |
| 418 | */ |
| 419 | struct bio *bi; |
| 420 | atomic_inc(&rdev->nr_pending); |
| 421 | atomic_inc(&rdev->nr_pending); |
| 422 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 423 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | bi->bi_private = rdev; |
| 426 | bi->bi_bdev = rdev->bdev; |
| 427 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 429 | rcu_read_lock(); |
| 430 | rdev_dec_pending(rdev, mddev); |
| 431 | } |
| 432 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 433 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 434 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 437 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 439 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 440 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 442 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 443 | /* an empty barrier - all done */ |
| 444 | bio_endio(bio, 0); |
| 445 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 446 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 447 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 448 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 449 | |
| 450 | mddev->flush_bio = NULL; |
| 451 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 452 | } |
| 453 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 454 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 455 | { |
| 456 | spin_lock_irq(&mddev->write_lock); |
| 457 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 458 | !mddev->flush_bio, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 459 | mddev->write_lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 460 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 461 | spin_unlock_irq(&mddev->write_lock); |
| 462 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 463 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 464 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 465 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 466 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 467 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 468 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 469 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 470 | struct mddev *mddev = cb->data; |
| 471 | md_wakeup_thread(mddev->thread); |
| 472 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 473 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 474 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 475 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 476 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | atomic_inc(&mddev->active); |
| 479 | return mddev; |
| 480 | } |
| 481 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 482 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 483 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 484 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 486 | struct bio_set *bs = NULL; |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 489 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 490 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 491 | mddev->ctime == 0 && !mddev->hold_active) { |
| 492 | /* Array is not configured at all, and not held active, |
| 493 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 494 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 495 | bs = mddev->bio_set; |
| 496 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 497 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 498 | /* We did a probe so need to clean up. Call |
| 499 | * queue_work inside the spinlock so that |
| 500 | * flush_workqueue() after mddev_find will |
| 501 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 502 | */ |
| 503 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 504 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 505 | } else |
| 506 | kfree(mddev); |
| 507 | } |
| 508 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 509 | if (bs) |
| 510 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 513 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 514 | { |
| 515 | mutex_init(&mddev->open_mutex); |
| 516 | mutex_init(&mddev->reconfig_mutex); |
| 517 | mutex_init(&mddev->bitmap_info.mutex); |
| 518 | INIT_LIST_HEAD(&mddev->disks); |
| 519 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 520 | init_timer(&mddev->safemode_timer); |
| 521 | atomic_set(&mddev->active, 1); |
| 522 | atomic_set(&mddev->openers, 0); |
| 523 | atomic_set(&mddev->active_io, 0); |
| 524 | spin_lock_init(&mddev->write_lock); |
| 525 | atomic_set(&mddev->flush_pending, 0); |
| 526 | init_waitqueue_head(&mddev->sb_wait); |
| 527 | init_waitqueue_head(&mddev->recovery_wait); |
| 528 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 529 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 530 | mddev->resync_min = 0; |
| 531 | mddev->resync_max = MaxSector; |
| 532 | mddev->level = LEVEL_NONE; |
| 533 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 534 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 535 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 536 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 538 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 540 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 541 | unit &= ~((1<<MdpMinorShift)-1); |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | retry: |
| 544 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 546 | if (unit) { |
| 547 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 548 | if (mddev->unit == unit) { |
| 549 | mddev_get(mddev); |
| 550 | spin_unlock(&all_mddevs_lock); |
| 551 | kfree(new); |
| 552 | return mddev; |
| 553 | } |
| 554 | |
| 555 | if (new) { |
| 556 | list_add(&new->all_mddevs, &all_mddevs); |
| 557 | spin_unlock(&all_mddevs_lock); |
| 558 | new->hold_active = UNTIL_IOCTL; |
| 559 | return new; |
| 560 | } |
| 561 | } else if (new) { |
| 562 | /* find an unused unit number */ |
| 563 | static int next_minor = 512; |
| 564 | int start = next_minor; |
| 565 | int is_free = 0; |
| 566 | int dev = 0; |
| 567 | while (!is_free) { |
| 568 | dev = MKDEV(MD_MAJOR, next_minor); |
| 569 | next_minor++; |
| 570 | if (next_minor > MINORMASK) |
| 571 | next_minor = 0; |
| 572 | if (next_minor == start) { |
| 573 | /* Oh dear, all in use. */ |
| 574 | spin_unlock(&all_mddevs_lock); |
| 575 | kfree(new); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | is_free = 1; |
| 580 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 581 | if (mddev->unit == dev) { |
| 582 | is_free = 0; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | new->unit = dev; |
| 587 | new->md_minor = MINOR(dev); |
| 588 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | list_add(&new->all_mddevs, &all_mddevs); |
| 590 | spin_unlock(&all_mddevs_lock); |
| 591 | return new; |
| 592 | } |
| 593 | spin_unlock(&all_mddevs_lock); |
| 594 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 595 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (!new) |
| 597 | return NULL; |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | new->unit = unit; |
| 600 | if (MAJOR(unit) == MD_MAJOR) |
| 601 | new->md_minor = MINOR(unit); |
| 602 | else |
| 603 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 604 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 605 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | goto retry; |
| 608 | } |
| 609 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 610 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 612 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 615 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 616 | { |
| 617 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 618 | } |
| 619 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 620 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 622 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 625 | static struct attribute_group md_redundancy_group; |
| 626 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 627 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 629 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 630 | /* These cannot be removed under reconfig_mutex as |
| 631 | * an access to the files will try to take reconfig_mutex |
| 632 | * while holding the file unremovable, which leads to |
| 633 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 634 | * So hold set sysfs_active while the remove in happeing, |
| 635 | * and anything else which might set ->to_remove or my |
| 636 | * otherwise change the sysfs namespace will fail with |
| 637 | * -EBUSY if sysfs_active is still set. |
| 638 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 639 | * test it under the same mutex to ensure its correct value |
| 640 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 641 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 642 | struct attribute_group *to_remove = mddev->to_remove; |
| 643 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 644 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 645 | mutex_unlock(&mddev->reconfig_mutex); |
| 646 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 647 | if (mddev->kobj.sd) { |
| 648 | if (to_remove != &md_redundancy_group) |
| 649 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 650 | if (mddev->pers == NULL || |
| 651 | mddev->pers->sync_request == NULL) { |
| 652 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 653 | if (mddev->sysfs_action) |
| 654 | sysfs_put(mddev->sysfs_action); |
| 655 | mddev->sysfs_action = NULL; |
| 656 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 657 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 658 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 659 | } else |
| 660 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 662 | /* As we've dropped the mutex we need a spinlock to |
| 663 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 664 | */ |
| 665 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 666 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 667 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 670 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 672 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 674 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (rdev->desc_nr == nr) |
| 676 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
| 680 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 681 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 682 | { |
| 683 | struct md_rdev *rdev; |
| 684 | |
| 685 | rdev_for_each_rcu(rdev, mddev) |
| 686 | if (rdev->desc_nr == nr) |
| 687 | return rdev; |
| 688 | |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 694 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 696 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | if (rdev->bdev->bd_dev == dev) |
| 698 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
| 702 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 703 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 704 | { |
| 705 | struct md_rdev *rdev; |
| 706 | |
| 707 | rdev_for_each_rcu(rdev, mddev) |
| 708 | if (rdev->bdev->bd_dev == dev) |
| 709 | return rdev; |
| 710 | |
| 711 | return NULL; |
| 712 | } |
| 713 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 714 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 715 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 716 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 717 | list_for_each_entry(pers, &pers_list, list) { |
| 718 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 719 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 720 | if (strcmp(pers->name, clevel)==0) |
| 721 | return pers; |
| 722 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 723 | return NULL; |
| 724 | } |
| 725 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 726 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 727 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 729 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 730 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 733 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | { |
| 735 | if (rdev->sb_page) |
| 736 | MD_BUG(); |
| 737 | |
| 738 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 739 | if (!rdev->sb_page) { |
| 740 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 741 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 747 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
| 749 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 750 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | rdev->sb_loaded = 0; |
| 752 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 753 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 754 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 756 | if (rdev->bb_page) { |
| 757 | put_page(rdev->bb_page); |
| 758 | rdev->bb_page = NULL; |
| 759 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 760 | kfree(rdev->badblocks.page); |
| 761 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 763 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 765 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 766 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 767 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 768 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 769 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 770 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 771 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 772 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 773 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 774 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 775 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 776 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 777 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 778 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 779 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 780 | } |
| 781 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 782 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 783 | sector_t sector, int size, struct page *page) |
| 784 | { |
| 785 | /* write first size bytes of page to sector of rdev |
| 786 | * Increment mddev->pending_writes before returning |
| 787 | * and decrement it on completion, waking up sb_wait |
| 788 | * if zero is reached. |
| 789 | * If an error occurred, call md_error |
| 790 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 791 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 792 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 793 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 794 | bio->bi_sector = sector; |
| 795 | bio_add_page(bio, page, size, 0); |
| 796 | bio->bi_private = rdev; |
| 797 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 798 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 799 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 800 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 801 | } |
| 802 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 803 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 804 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 805 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 806 | DEFINE_WAIT(wq); |
| 807 | for(;;) { |
| 808 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 809 | if (atomic_read(&mddev->pending_writes)==0) |
| 810 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 811 | schedule(); |
| 812 | } |
| 813 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 814 | } |
| 815 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 816 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 821 | 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] | 822 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 824 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | struct completion event; |
| 826 | int ret; |
| 827 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 828 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 830 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 831 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 832 | if (metadata_op) |
| 833 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 834 | else if (rdev->mddev->reshape_position != MaxSector && |
| 835 | (rdev->mddev->reshape_backwards == |
| 836 | (sector >= rdev->mddev->reshape_position))) |
| 837 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 838 | else |
| 839 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | bio_add_page(bio, page, size, 0); |
| 841 | init_completion(&event); |
| 842 | bio->bi_private = &event; |
| 843 | bio->bi_end_io = bi_complete; |
| 844 | submit_bio(rw, bio); |
| 845 | wait_for_completion(&event); |
| 846 | |
| 847 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 848 | bio_put(bio); |
| 849 | return ret; |
| 850 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 851 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 853 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
| 855 | char b[BDEVNAME_SIZE]; |
| 856 | if (!rdev->sb_page) { |
| 857 | MD_BUG(); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | if (rdev->sb_loaded) |
| 861 | return 0; |
| 862 | |
| 863 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 864 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | goto fail; |
| 866 | rdev->sb_loaded = 1; |
| 867 | return 0; |
| 868 | |
| 869 | fail: |
| 870 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 871 | bdevname(rdev->bdev,b)); |
| 872 | return -EINVAL; |
| 873 | } |
| 874 | |
| 875 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 876 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 877 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 878 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 879 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 880 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 884 | { |
| 885 | int ret; |
| 886 | mdp_super_t *tmp1, *tmp2; |
| 887 | |
| 888 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 889 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 890 | |
| 891 | if (!tmp1 || !tmp2) { |
| 892 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 893 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | goto abort; |
| 895 | } |
| 896 | |
| 897 | *tmp1 = *sb1; |
| 898 | *tmp2 = *sb2; |
| 899 | |
| 900 | /* |
| 901 | * nr_disks is not constant |
| 902 | */ |
| 903 | tmp1->nr_disks = 0; |
| 904 | tmp2->nr_disks = 0; |
| 905 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 906 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 908 | kfree(tmp1); |
| 909 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | return ret; |
| 911 | } |
| 912 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 913 | |
| 914 | static u32 md_csum_fold(u32 csum) |
| 915 | { |
| 916 | csum = (csum & 0xffff) + (csum >> 16); |
| 917 | return (csum & 0xffff) + (csum >> 16); |
| 918 | } |
| 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 921 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 922 | u64 newcsum = 0; |
| 923 | u32 *sb32 = (u32*)sb; |
| 924 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | unsigned int disk_csum, csum; |
| 926 | |
| 927 | disk_csum = sb->sb_csum; |
| 928 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 929 | |
| 930 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 931 | newcsum += sb32[i]; |
| 932 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 933 | |
| 934 | |
| 935 | #ifdef CONFIG_ALPHA |
| 936 | /* This used to use csum_partial, which was wrong for several |
| 937 | * reasons including that different results are returned on |
| 938 | * different architectures. It isn't critical that we get exactly |
| 939 | * the same return value as before (we always csum_fold before |
| 940 | * testing, and that removes any differences). However as we |
| 941 | * know that csum_partial always returned a 16bit value on |
| 942 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 943 | */ |
| 944 | sb->sb_csum = md_csum_fold(disk_csum); |
| 945 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 947 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | return csum; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | /* |
| 953 | * Handle superblock details. |
| 954 | * We want to be able to handle multiple superblock formats |
| 955 | * so we have a common interface to them all, and an array of |
| 956 | * different handlers. |
| 957 | * We rely on user-space to write the initial superblock, and support |
| 958 | * reading and updating of superblocks. |
| 959 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 960 | * 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] | 961 | * loads and validates a superblock on dev. |
| 962 | * if refdev != NULL, compare superblocks on both devices |
| 963 | * Return: |
| 964 | * 0 - dev has a superblock that is compatible with refdev |
| 965 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 966 | * so dev should be used as the refdev in future |
| 967 | * -EINVAL superblock incompatible or invalid |
| 968 | * -othererror e.g. -EIO |
| 969 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 970 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | * Verify that dev is acceptable into mddev. |
| 972 | * The first time, mddev->raid_disks will be 0, and data from |
| 973 | * dev should be merged in. Subsequent calls check that dev |
| 974 | * is new enough. Return 0 or -EINVAL |
| 975 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 976 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | * Update the superblock for rdev with data in mddev |
| 978 | * This does not write to disc. |
| 979 | * |
| 980 | */ |
| 981 | |
| 982 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 983 | char *name; |
| 984 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 985 | int (*load_super)(struct md_rdev *rdev, |
| 986 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 987 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 988 | int (*validate_super)(struct mddev *mddev, |
| 989 | struct md_rdev *rdev); |
| 990 | void (*sync_super)(struct mddev *mddev, |
| 991 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 992 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 993 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 994 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 995 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | }; |
| 997 | |
| 998 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 999 | * Check that the given mddev has no bitmap. |
| 1000 | * |
| 1001 | * This function is called from the run method of all personalities that do not |
| 1002 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1003 | * has a bitmap. Otherwise, it returns 0. |
| 1004 | * |
| 1005 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1006 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1007 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1008 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1009 | return 0; |
| 1010 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1011 | mdname(mddev), mddev->pers->name); |
| 1012 | return 1; |
| 1013 | } |
| 1014 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1015 | |
| 1016 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | * load_super for 0.90.0 |
| 1018 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1019 | 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] | 1020 | { |
| 1021 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1022 | mdp_super_t *sb; |
| 1023 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1026 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | * it's at the end of the disk. |
| 1028 | * |
| 1029 | * It also happens to be a multiple of 4Kb. |
| 1030 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1031 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1033 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | if (ret) return ret; |
| 1035 | |
| 1036 | ret = -EINVAL; |
| 1037 | |
| 1038 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1039 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1042 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1043 | b); |
| 1044 | goto abort; |
| 1045 | } |
| 1046 | |
| 1047 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1048 | sb->minor_version < 90 || |
| 1049 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1051 | sb->major_version, sb->minor_version, |
| 1052 | b); |
| 1053 | goto abort; |
| 1054 | } |
| 1055 | |
| 1056 | if (sb->raid_disks <= 0) |
| 1057 | goto abort; |
| 1058 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1059 | 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] | 1060 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1061 | b); |
| 1062 | goto abort; |
| 1063 | } |
| 1064 | |
| 1065 | rdev->preferred_minor = sb->md_minor; |
| 1066 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1067 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1068 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1069 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (sb->level == LEVEL_MULTIPATH) |
| 1072 | rdev->desc_nr = -1; |
| 1073 | else |
| 1074 | rdev->desc_nr = sb->this_disk.number; |
| 1075 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1076 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1078 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1080 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (!uuid_equal(refsb, sb)) { |
| 1082 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1083 | b, bdevname(refdev->bdev,b2)); |
| 1084 | goto abort; |
| 1085 | } |
| 1086 | if (!sb_equal(refsb, sb)) { |
| 1087 | printk(KERN_WARNING "md: %s has same UUID" |
| 1088 | " but different superblock to %s\n", |
| 1089 | b, bdevname(refdev->bdev, b2)); |
| 1090 | goto abort; |
| 1091 | } |
| 1092 | ev1 = md_event(sb); |
| 1093 | ev2 = md_event(refsb); |
| 1094 | if (ev1 > ev2) |
| 1095 | ret = 1; |
| 1096 | else |
| 1097 | ret = 0; |
| 1098 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1099 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1100 | /* Limit to 4TB as metadata cannot record more than that. |
| 1101 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1102 | * record this size) |
| 1103 | */ |
| 1104 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1105 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1107 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1108 | /* "this cannot possibly happen" ... */ |
| 1109 | ret = -EINVAL; |
| 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | abort: |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * validate_super for 0.90.0 |
| 1117 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1118 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
| 1120 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1121 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1122 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1124 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1125 | clear_bit(Faulty, &rdev->flags); |
| 1126 | clear_bit(In_sync, &rdev->flags); |
| 1127 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | if (mddev->raid_disks == 0) { |
| 1130 | mddev->major_version = 0; |
| 1131 | mddev->minor_version = sb->minor_version; |
| 1132 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1133 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1134 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | mddev->ctime = sb->ctime; |
| 1136 | mddev->utime = sb->utime; |
| 1137 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1138 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | mddev->layout = sb->layout; |
| 1140 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1141 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1142 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1143 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1144 | mddev->bitmap_info.space = 0; |
| 1145 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1146 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1147 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1148 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1150 | if (mddev->minor_version >= 91) { |
| 1151 | mddev->reshape_position = sb->reshape_position; |
| 1152 | mddev->delta_disks = sb->delta_disks; |
| 1153 | mddev->new_level = sb->new_level; |
| 1154 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1155 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1156 | if (mddev->delta_disks < 0) |
| 1157 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1158 | } else { |
| 1159 | mddev->reshape_position = MaxSector; |
| 1160 | mddev->delta_disks = 0; |
| 1161 | mddev->new_level = mddev->level; |
| 1162 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1163 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1167 | mddev->recovery_cp = MaxSector; |
| 1168 | else { |
| 1169 | if (sb->events_hi == sb->cp_events_hi && |
| 1170 | sb->events_lo == sb->cp_events_lo) { |
| 1171 | mddev->recovery_cp = sb->recovery_cp; |
| 1172 | } else |
| 1173 | mddev->recovery_cp = 0; |
| 1174 | } |
| 1175 | |
| 1176 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1177 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1178 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1179 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1180 | |
| 1181 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1182 | |
| 1183 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1184 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1185 | mddev->bitmap_info.offset = |
| 1186 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1187 | mddev->bitmap_info.space = |
| 1188 | mddev->bitmap_info.space; |
| 1189 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1190 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1191 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1192 | /* Insist on good event counter while assembling, except |
| 1193 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1195 | if (sb->disks[rdev->desc_nr].state & ( |
| 1196 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1197 | if (ev1 < mddev->events) |
| 1198 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1199 | } else if (mddev->bitmap) { |
| 1200 | /* if adding to array with a bitmap, then we can accept an |
| 1201 | * older device ... but not too old. |
| 1202 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1203 | if (ev1 < mddev->bitmap->events_cleared) |
| 1204 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1205 | } else { |
| 1206 | if (ev1 < mddev->events) |
| 1207 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1208 | return 0; |
| 1209 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | desc = sb->disks + rdev->desc_nr; |
| 1213 | |
| 1214 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1215 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1216 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1217 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1218 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1220 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1221 | /* active but not in sync implies recovery up to |
| 1222 | * reshape position. We don't know exactly where |
| 1223 | * that is, so set to zero for now */ |
| 1224 | if (mddev->minor_version >= 91) { |
| 1225 | rdev->recovery_offset = 0; |
| 1226 | rdev->raid_disk = desc->raid_disk; |
| 1227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1229 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1230 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1231 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1232 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
| 1237 | * sync_super for 0.90.0 |
| 1238 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1239 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { |
| 1241 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1242 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
| 1246 | /* make rdev->sb match mddev data.. |
| 1247 | * |
| 1248 | * 1/ zero out disks |
| 1249 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1250 | * 3/ any empty disks < next_spare become removed |
| 1251 | * |
| 1252 | * disks[0] gets initialised to REMOVED because |
| 1253 | * we cannot be sure from other fields if it has |
| 1254 | * been initialised or not. |
| 1255 | */ |
| 1256 | int i; |
| 1257 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1258 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1259 | rdev->sb_size = MD_SB_BYTES; |
| 1260 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1261 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | memset(sb, 0, sizeof(*sb)); |
| 1264 | |
| 1265 | sb->md_magic = MD_SB_MAGIC; |
| 1266 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | sb->patch_version = mddev->patch_version; |
| 1268 | sb->gvalid_words = 0; /* ignored */ |
| 1269 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1270 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1271 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1272 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1273 | |
| 1274 | sb->ctime = mddev->ctime; |
| 1275 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1276 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | sb->raid_disks = mddev->raid_disks; |
| 1278 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1279 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | sb->utime = mddev->utime; |
| 1281 | sb->state = 0; |
| 1282 | sb->events_hi = (mddev->events>>32); |
| 1283 | sb->events_lo = (u32)mddev->events; |
| 1284 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1285 | if (mddev->reshape_position == MaxSector) |
| 1286 | sb->minor_version = 90; |
| 1287 | else { |
| 1288 | sb->minor_version = 91; |
| 1289 | sb->reshape_position = mddev->reshape_position; |
| 1290 | sb->new_level = mddev->new_level; |
| 1291 | sb->delta_disks = mddev->delta_disks; |
| 1292 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1293 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1294 | } |
| 1295 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | if (mddev->in_sync) |
| 1297 | { |
| 1298 | sb->recovery_cp = mddev->recovery_cp; |
| 1299 | sb->cp_events_hi = (mddev->events>>32); |
| 1300 | sb->cp_events_lo = (u32)mddev->events; |
| 1301 | if (mddev->recovery_cp == MaxSector) |
| 1302 | sb->state = (1<< MD_SB_CLEAN); |
| 1303 | } else |
| 1304 | sb->recovery_cp = 0; |
| 1305 | |
| 1306 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1307 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1309 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1310 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1313 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1315 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1316 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1317 | |
| 1318 | if (rdev2->raid_disk >= 0 && |
| 1319 | sb->minor_version >= 91) |
| 1320 | /* we have nowhere to store the recovery_offset, |
| 1321 | * but if it is not below the reshape_position, |
| 1322 | * we can piggy-back on that. |
| 1323 | */ |
| 1324 | is_active = 1; |
| 1325 | if (rdev2->raid_disk < 0 || |
| 1326 | test_bit(Faulty, &rdev2->flags)) |
| 1327 | is_active = 0; |
| 1328 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1329 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1331 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1332 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | d = &sb->disks[rdev2->desc_nr]; |
| 1334 | nr_disks++; |
| 1335 | d->number = rdev2->desc_nr; |
| 1336 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1337 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1338 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | d->raid_disk = rdev2->raid_disk; |
| 1340 | else |
| 1341 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1342 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1344 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1346 | if (test_bit(In_sync, &rdev2->flags)) |
| 1347 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | active++; |
| 1349 | working++; |
| 1350 | } else { |
| 1351 | d->state = 0; |
| 1352 | spare++; |
| 1353 | working++; |
| 1354 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1355 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1356 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1359 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1360 | mdp_disk_t *d = &sb->disks[i]; |
| 1361 | if (d->state == 0 && d->number == 0) { |
| 1362 | d->number = i; |
| 1363 | d->raid_disk = i; |
| 1364 | d->state = (1<<MD_DISK_REMOVED); |
| 1365 | d->state |= (1<<MD_DISK_FAULTY); |
| 1366 | failed++; |
| 1367 | } |
| 1368 | } |
| 1369 | sb->nr_disks = nr_disks; |
| 1370 | sb->active_disks = active; |
| 1371 | sb->working_disks = working; |
| 1372 | sb->failed_disks = failed; |
| 1373 | sb->spare_disks = spare; |
| 1374 | |
| 1375 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1376 | sb->sb_csum = calc_sb_csum(sb); |
| 1377 | } |
| 1378 | |
| 1379 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1380 | * rdev_size_change for 0.90.0 |
| 1381 | */ |
| 1382 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1383 | 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] | 1384 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1385 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1386 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1387 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1388 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1389 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1390 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1391 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1392 | /* Limit to 4TB as metadata cannot record more than that. |
| 1393 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1394 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1395 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1396 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1397 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1398 | rdev->sb_page); |
| 1399 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1400 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1401 | } |
| 1402 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1403 | static int |
| 1404 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1405 | { |
| 1406 | /* non-zero offset changes not possible with v0.90 */ |
| 1407 | return new_offset == 0; |
| 1408 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1409 | |
| 1410 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | * version 1 superblock |
| 1412 | */ |
| 1413 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1414 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1416 | __le32 disk_csum; |
| 1417 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | unsigned long long newcsum; |
| 1419 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1420 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | disk_csum = sb->sb_csum; |
| 1423 | sb->sb_csum = 0; |
| 1424 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1425 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | newcsum += le32_to_cpu(*isuper++); |
| 1427 | |
| 1428 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1429 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
| 1431 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1432 | sb->sb_csum = disk_csum; |
| 1433 | return cpu_to_le32(csum); |
| 1434 | } |
| 1435 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1436 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1437 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | struct mdp_superblock_1 *sb; |
| 1441 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1442 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1443 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1445 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1448 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | * It is always aligned to a 4K boundary and |
| 1450 | * depeding on minor_version, it can be: |
| 1451 | * 0: At least 8K, but less than 12K, from end of device |
| 1452 | * 1: At start of device |
| 1453 | * 2: 4K from start of device. |
| 1454 | */ |
| 1455 | switch(minor_version) { |
| 1456 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1457 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1458 | sb_start -= 8*2; |
| 1459 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | break; |
| 1461 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1462 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1465 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | break; |
| 1467 | default: |
| 1468 | return -EINVAL; |
| 1469 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1470 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1472 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1473 | * and it is safe to read 4k, so we do that |
| 1474 | */ |
| 1475 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | if (ret) return ret; |
| 1477 | |
| 1478 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1479 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
| 1481 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1482 | sb->major_version != cpu_to_le32(1) || |
| 1483 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1484 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1485 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return -EINVAL; |
| 1487 | |
| 1488 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1489 | printk("md: invalid superblock checksum on %s\n", |
| 1490 | bdevname(rdev->bdev,b)); |
| 1491 | return -EINVAL; |
| 1492 | } |
| 1493 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1494 | printk("md: data_size too small on %s\n", |
| 1495 | bdevname(rdev->bdev,b)); |
| 1496 | return -EINVAL; |
| 1497 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1498 | if (sb->pad0 || |
| 1499 | sb->pad3[0] || |
| 1500 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1501 | /* Some padding is non-zero, might be a new feature */ |
| 1502 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | rdev->preferred_minor = 0xffff; |
| 1505 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1506 | rdev->new_data_offset = rdev->data_offset; |
| 1507 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1508 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1509 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1510 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1512 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1513 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1514 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1515 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1516 | |
| 1517 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1518 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1519 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1520 | if (minor_version |
| 1521 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1522 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1523 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1524 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1525 | rdev->desc_nr = -1; |
| 1526 | else |
| 1527 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1528 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1529 | if (!rdev->bb_page) { |
| 1530 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1531 | if (!rdev->bb_page) |
| 1532 | return -ENOMEM; |
| 1533 | } |
| 1534 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1535 | rdev->badblocks.count == 0) { |
| 1536 | /* need to load the bad block list. |
| 1537 | * Currently we limit it to one page. |
| 1538 | */ |
| 1539 | s32 offset; |
| 1540 | sector_t bb_sector; |
| 1541 | u64 *bbp; |
| 1542 | int i; |
| 1543 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1544 | if (sectors > (PAGE_SIZE / 512)) |
| 1545 | return -EINVAL; |
| 1546 | offset = le32_to_cpu(sb->bblog_offset); |
| 1547 | if (offset == 0) |
| 1548 | return -EINVAL; |
| 1549 | bb_sector = (long long)offset; |
| 1550 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1551 | rdev->bb_page, READ, true)) |
| 1552 | return -EIO; |
| 1553 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1554 | rdev->badblocks.shift = sb->bblog_shift; |
| 1555 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1556 | u64 bb = le64_to_cpu(*bbp); |
| 1557 | int count = bb & (0x3ff); |
| 1558 | u64 sector = bb >> 10; |
| 1559 | sector <<= sb->bblog_shift; |
| 1560 | count <<= sb->bblog_shift; |
| 1561 | if (bb + 1 == 0) |
| 1562 | break; |
| 1563 | if (md_set_badblocks(&rdev->badblocks, |
| 1564 | sector, count, 1) == 0) |
| 1565 | return -EINVAL; |
| 1566 | } |
| 1567 | } else if (sb->bblog_offset == 0) |
| 1568 | rdev->badblocks.shift = -1; |
| 1569 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1570 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1571 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1572 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1574 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
| 1576 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1577 | sb->level != refsb->level || |
| 1578 | sb->layout != refsb->layout || |
| 1579 | sb->chunksize != refsb->chunksize) { |
| 1580 | printk(KERN_WARNING "md: %s has strangely different" |
| 1581 | " superblock to %s\n", |
| 1582 | bdevname(rdev->bdev,b), |
| 1583 | bdevname(refdev->bdev,b2)); |
| 1584 | return -EINVAL; |
| 1585 | } |
| 1586 | ev1 = le64_to_cpu(sb->events); |
| 1587 | ev2 = le64_to_cpu(refsb->events); |
| 1588 | |
| 1589 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1590 | ret = 1; |
| 1591 | else |
| 1592 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1594 | if (minor_version) { |
| 1595 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1596 | sectors -= rdev->data_offset; |
| 1597 | } else |
| 1598 | sectors = rdev->sb_start; |
| 1599 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1601 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1602 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1605 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1607 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1608 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1610 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1611 | clear_bit(Faulty, &rdev->flags); |
| 1612 | clear_bit(In_sync, &rdev->flags); |
| 1613 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | if (mddev->raid_disks == 0) { |
| 1616 | mddev->major_version = 1; |
| 1617 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1618 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1619 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1621 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1622 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1623 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | mddev->layout = le32_to_cpu(sb->layout); |
| 1625 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1626 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1627 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1628 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1629 | mddev->bitmap_info.space = 0; |
| 1630 | /* Default location for bitmap is 1K after superblock |
| 1631 | * using 3K - total of 4K |
| 1632 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1633 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1634 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1635 | mddev->reshape_backwards = 0; |
| 1636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1638 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1639 | |
| 1640 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1641 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1642 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1643 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1644 | mddev->bitmap_info.offset = |
| 1645 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1646 | /* Metadata doesn't record how much space is available. |
| 1647 | * For 1.0, we assume we can use up to the superblock |
| 1648 | * if before, else to 4K beyond superblock. |
| 1649 | * For others, assume no change is possible. |
| 1650 | */ |
| 1651 | if (mddev->minor_version > 0) |
| 1652 | mddev->bitmap_info.space = 0; |
| 1653 | else if (mddev->bitmap_info.offset > 0) |
| 1654 | mddev->bitmap_info.space = |
| 1655 | 8 - mddev->bitmap_info.offset; |
| 1656 | else |
| 1657 | mddev->bitmap_info.space = |
| 1658 | -mddev->bitmap_info.offset; |
| 1659 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1660 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1661 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1662 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1663 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1664 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1665 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1666 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1667 | if (mddev->delta_disks < 0 || |
| 1668 | (mddev->delta_disks == 0 && |
| 1669 | (le32_to_cpu(sb->feature_map) |
| 1670 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1671 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1672 | } else { |
| 1673 | mddev->reshape_position = MaxSector; |
| 1674 | mddev->delta_disks = 0; |
| 1675 | mddev->new_level = mddev->level; |
| 1676 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1677 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1680 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1681 | /* Insist of good event counter while assembling, except for |
| 1682 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1684 | if (rdev->desc_nr >= 0 && |
| 1685 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1686 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1687 | if (ev1 < mddev->events) |
| 1688 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1689 | } else if (mddev->bitmap) { |
| 1690 | /* If adding to array with a bitmap, then we can accept an |
| 1691 | * older device, but not too old. |
| 1692 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1693 | if (ev1 < mddev->bitmap->events_cleared) |
| 1694 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1695 | } else { |
| 1696 | if (ev1 < mddev->events) |
| 1697 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1698 | return 0; |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1701 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1702 | if (rdev->desc_nr < 0 || |
| 1703 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1704 | role = 0xffff; |
| 1705 | rdev->desc_nr = -1; |
| 1706 | } else |
| 1707 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | switch(role) { |
| 1709 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | break; |
| 1711 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1712 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1715 | if ((le32_to_cpu(sb->feature_map) & |
| 1716 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1717 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1718 | else |
| 1719 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | rdev->raid_disk = role; |
| 1721 | break; |
| 1722 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1723 | if (sb->devflags & WriteMostly1) |
| 1724 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1725 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1726 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1727 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1728 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | return 0; |
| 1731 | } |
| 1732 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1733 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
| 1735 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1736 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | int max_dev, i; |
| 1738 | /* make rdev->sb match mddev and rdev data. */ |
| 1739 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1740 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
| 1742 | sb->feature_map = 0; |
| 1743 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1744 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1746 | |
| 1747 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1748 | sb->events = cpu_to_le64(mddev->events); |
| 1749 | if (mddev->in_sync) |
| 1750 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1751 | else |
| 1752 | sb->resync_offset = cpu_to_le64(0); |
| 1753 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1754 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1755 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1756 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1757 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1758 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1759 | sb->level = cpu_to_le32(mddev->level); |
| 1760 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1761 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1762 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1763 | sb->devflags |= WriteMostly1; |
| 1764 | else |
| 1765 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1766 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1767 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1768 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1769 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1770 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1771 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1772 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1773 | |
| 1774 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1775 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1776 | sb->feature_map |= |
| 1777 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1778 | sb->recovery_offset = |
| 1779 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1780 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1781 | if (test_bit(Replacement, &rdev->flags)) |
| 1782 | sb->feature_map |= |
| 1783 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1784 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1785 | if (mddev->reshape_position != MaxSector) { |
| 1786 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1787 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1788 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1789 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1790 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1791 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1792 | if (mddev->delta_disks == 0 && |
| 1793 | mddev->reshape_backwards) |
| 1794 | sb->feature_map |
| 1795 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1796 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1797 | sb->feature_map |
| 1798 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1799 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1800 | - rdev->data_offset)); |
| 1801 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1802 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1803 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1804 | if (rdev->badblocks.count == 0) |
| 1805 | /* Nothing to do for bad blocks*/ ; |
| 1806 | else if (sb->bblog_offset == 0) |
| 1807 | /* Cannot record bad blocks on this device */ |
| 1808 | md_error(mddev, rdev); |
| 1809 | else { |
| 1810 | struct badblocks *bb = &rdev->badblocks; |
| 1811 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1812 | u64 *p = bb->page; |
| 1813 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1814 | if (bb->changed) { |
| 1815 | unsigned seq; |
| 1816 | |
| 1817 | retry: |
| 1818 | seq = read_seqbegin(&bb->lock); |
| 1819 | |
| 1820 | memset(bbp, 0xff, PAGE_SIZE); |
| 1821 | |
| 1822 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1823 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1824 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1825 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1826 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1827 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1828 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1829 | if (read_seqretry(&bb->lock, seq)) |
| 1830 | goto retry; |
| 1831 | |
| 1832 | bb->sector = (rdev->sb_start + |
| 1833 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1834 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1839 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | if (rdev2->desc_nr+1 > max_dev) |
| 1841 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1842 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1843 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1844 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1845 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1846 | rdev->sb_size = max_dev * 2 + 256; |
| 1847 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1848 | if (rdev->sb_size & bmask) |
| 1849 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1850 | } else |
| 1851 | max_dev = le32_to_cpu(sb->max_dev); |
| 1852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | for (i=0; i<max_dev;i++) |
| 1854 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1855 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1856 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1858 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1860 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1862 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1863 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | else |
| 1865 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1866 | } |
| 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1869 | } |
| 1870 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1871 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1872 | 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] | 1873 | { |
| 1874 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1875 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1876 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1877 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1878 | if (rdev->data_offset != rdev->new_data_offset) |
| 1879 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1880 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1881 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1882 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1883 | max_sectors -= rdev->data_offset; |
| 1884 | if (!num_sectors || num_sectors > max_sectors) |
| 1885 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1886 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1887 | /* minor version 0 with bitmap we can't move */ |
| 1888 | return 0; |
| 1889 | } else { |
| 1890 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1891 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1892 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1893 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1894 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1895 | if (!num_sectors || num_sectors > max_sectors) |
| 1896 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1897 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1898 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1899 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1900 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1901 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1902 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1903 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1904 | rdev->sb_page); |
| 1905 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1906 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1907 | |
| 1908 | } |
| 1909 | |
| 1910 | static int |
| 1911 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1912 | unsigned long long new_offset) |
| 1913 | { |
| 1914 | /* All necessary checks on new >= old have been done */ |
| 1915 | struct bitmap *bitmap; |
| 1916 | if (new_offset >= rdev->data_offset) |
| 1917 | return 1; |
| 1918 | |
| 1919 | /* with 1.0 metadata, there is no metadata to tread on |
| 1920 | * so we can always move back */ |
| 1921 | if (rdev->mddev->minor_version == 0) |
| 1922 | return 1; |
| 1923 | |
| 1924 | /* otherwise we must be sure not to step on |
| 1925 | * any metadata, so stay: |
| 1926 | * 36K beyond start of superblock |
| 1927 | * beyond end of badblocks |
| 1928 | * beyond write-intent bitmap |
| 1929 | */ |
| 1930 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1931 | return 0; |
| 1932 | bitmap = rdev->mddev->bitmap; |
| 1933 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1934 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1935 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1936 | return 0; |
| 1937 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1938 | return 0; |
| 1939 | |
| 1940 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1943 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | [0] = { |
| 1945 | .name = "0.90.0", |
| 1946 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1947 | .load_super = super_90_load, |
| 1948 | .validate_super = super_90_validate, |
| 1949 | .sync_super = super_90_sync, |
| 1950 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1951 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | }, |
| 1953 | [1] = { |
| 1954 | .name = "md-1", |
| 1955 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1956 | .load_super = super_1_load, |
| 1957 | .validate_super = super_1_validate, |
| 1958 | .sync_super = super_1_sync, |
| 1959 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1960 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | }, |
| 1962 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1964 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1965 | { |
| 1966 | if (mddev->sync_super) { |
| 1967 | mddev->sync_super(mddev, rdev); |
| 1968 | return; |
| 1969 | } |
| 1970 | |
| 1971 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1972 | |
| 1973 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1974 | } |
| 1975 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1976 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1978 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1980 | rcu_read_lock(); |
| 1981 | rdev_for_each_rcu(rdev, mddev1) |
| 1982 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1983 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1984 | rdev2->bdev->bd_contains) { |
| 1985 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1986 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1987 | } |
| 1988 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | return 0; |
| 1990 | } |
| 1991 | |
| 1992 | static LIST_HEAD(pending_raid_disks); |
| 1993 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1994 | /* |
| 1995 | * Try to register data integrity profile for an mddev |
| 1996 | * |
| 1997 | * This is called when an array is started and after a disk has been kicked |
| 1998 | * from the array. It only succeeds if all working and active component devices |
| 1999 | * are integrity capable with matching profiles. |
| 2000 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2001 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2002 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2003 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2004 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2005 | if (list_empty(&mddev->disks)) |
| 2006 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2007 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2008 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2009 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2010 | /* skip spares and non-functional disks */ |
| 2011 | if (test_bit(Faulty, &rdev->flags)) |
| 2012 | continue; |
| 2013 | if (rdev->raid_disk < 0) |
| 2014 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2015 | if (!reference) { |
| 2016 | /* Use the first rdev as the reference */ |
| 2017 | reference = rdev; |
| 2018 | continue; |
| 2019 | } |
| 2020 | /* does this rdev's profile match the reference profile? */ |
| 2021 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2022 | rdev->bdev->bd_disk) < 0) |
| 2023 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2024 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2025 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2026 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2027 | /* |
| 2028 | * All component devices are integrity capable and have matching |
| 2029 | * profiles, register the common profile for the md device. |
| 2030 | */ |
| 2031 | if (blk_integrity_register(mddev->gendisk, |
| 2032 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2033 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2034 | mdname(mddev)); |
| 2035 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2036 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2037 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2038 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2039 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2040 | mdname(mddev)); |
| 2041 | return -EINVAL; |
| 2042 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2043 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2044 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2045 | EXPORT_SYMBOL(md_integrity_register); |
| 2046 | |
| 2047 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2048 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2049 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2050 | struct blk_integrity *bi_rdev; |
| 2051 | struct blk_integrity *bi_mddev; |
| 2052 | |
| 2053 | if (!mddev->gendisk) |
| 2054 | return; |
| 2055 | |
| 2056 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2057 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2058 | |
| 2059 | if (!bi_mddev) /* nothing to do */ |
| 2060 | return; |
| 2061 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2062 | return; |
| 2063 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2064 | rdev->bdev->bd_disk) >= 0) |
| 2065 | return; |
| 2066 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2067 | blk_integrity_unregister(mddev->gendisk); |
| 2068 | } |
| 2069 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2070 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2071 | 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] | 2072 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2073 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2074 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2075 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2076 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
| 2078 | if (rdev->mddev) { |
| 2079 | MD_BUG(); |
| 2080 | return -EINVAL; |
| 2081 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2082 | |
| 2083 | /* prevent duplicates */ |
| 2084 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2085 | return -EEXIST; |
| 2086 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2087 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2088 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2089 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2090 | if (mddev->pers) { |
| 2091 | /* Cannot change size, so fail |
| 2092 | * If mddev->level <= 0, then we don't care |
| 2093 | * about aligning sizes (e.g. linear) |
| 2094 | */ |
| 2095 | if (mddev->level > 0) |
| 2096 | return -ENOSPC; |
| 2097 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2098 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
| 2101 | /* Verify rdev->desc_nr is unique. |
| 2102 | * If it is -1, assign a free number, else |
| 2103 | * check number is not in use |
| 2104 | */ |
| 2105 | if (rdev->desc_nr < 0) { |
| 2106 | int choice = 0; |
| 2107 | if (mddev->pers) choice = mddev->raid_disks; |
| 2108 | while (find_rdev_nr(mddev, choice)) |
| 2109 | choice++; |
| 2110 | rdev->desc_nr = choice; |
| 2111 | } else { |
| 2112 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2113 | return -EBUSY; |
| 2114 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2115 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2116 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2117 | mdname(mddev), mddev->max_disks); |
| 2118 | return -EBUSY; |
| 2119 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2120 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2121 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2122 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2125 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2126 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2127 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2128 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2129 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2130 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2131 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2132 | /* failure here is OK */; |
| 2133 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2134 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2135 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2136 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2137 | |
| 2138 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2139 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2142 | |
| 2143 | fail: |
| 2144 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2145 | b, mdname(mddev)); |
| 2146 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2149 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2150 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2151 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2152 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2153 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2156 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
| 2158 | char b[BDEVNAME_SIZE]; |
| 2159 | if (!rdev->mddev) { |
| 2160 | MD_BUG(); |
| 2161 | return; |
| 2162 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2163 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2164 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2166 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2167 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2168 | sysfs_put(rdev->sysfs_state); |
| 2169 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2170 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2171 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2172 | * writing to 'remove' to "dev/state". We also need |
| 2173 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2174 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2175 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2176 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2177 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2178 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /* |
| 2182 | * prevent the device from being mounted, repartitioned or |
| 2183 | * otherwise reused by a RAID array (or any other kernel |
| 2184 | * subsystem), by bd_claiming the device. |
| 2185 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2186 | 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] | 2187 | { |
| 2188 | int err = 0; |
| 2189 | struct block_device *bdev; |
| 2190 | char b[BDEVNAME_SIZE]; |
| 2191 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2192 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2193 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | if (IS_ERR(bdev)) { |
| 2195 | printk(KERN_ERR "md: could not open %s.\n", |
| 2196 | __bdevname(dev, b)); |
| 2197 | return PTR_ERR(bdev); |
| 2198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | rdev->bdev = bdev; |
| 2200 | return err; |
| 2201 | } |
| 2202 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2203 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | { |
| 2205 | struct block_device *bdev = rdev->bdev; |
| 2206 | rdev->bdev = NULL; |
| 2207 | if (!bdev) |
| 2208 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2209 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | void md_autodetect_dev(dev_t dev); |
| 2213 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2214 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
| 2216 | char b[BDEVNAME_SIZE]; |
| 2217 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2218 | bdevname(rdev->bdev,b)); |
| 2219 | if (rdev->mddev) |
| 2220 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2221 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2223 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2224 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | #endif |
| 2226 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2227 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2230 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | { |
| 2232 | unbind_rdev_from_array(rdev); |
| 2233 | export_rdev(rdev); |
| 2234 | } |
| 2235 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2236 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2238 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2240 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | if (!rdev->mddev) { |
| 2242 | MD_BUG(); |
| 2243 | continue; |
| 2244 | } |
| 2245 | kick_rdev_from_array(rdev); |
| 2246 | } |
| 2247 | if (!list_empty(&mddev->disks)) |
| 2248 | MD_BUG(); |
| 2249 | mddev->raid_disks = 0; |
| 2250 | mddev->major_version = 0; |
| 2251 | } |
| 2252 | |
| 2253 | static void print_desc(mdp_disk_t *desc) |
| 2254 | { |
| 2255 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2256 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2257 | } |
| 2258 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2259 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | { |
| 2261 | int i; |
| 2262 | |
| 2263 | printk(KERN_INFO |
| 2264 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2265 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2266 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2267 | sb->ctime); |
| 2268 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2269 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2270 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2271 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2272 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2273 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2274 | sb->failed_disks, sb->spare_disks, |
| 2275 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2276 | |
| 2277 | printk(KERN_INFO); |
| 2278 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2279 | mdp_disk_t *desc; |
| 2280 | |
| 2281 | desc = sb->disks + i; |
| 2282 | if (desc->number || desc->major || desc->minor || |
| 2283 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2284 | printk(" D %2d: ", i); |
| 2285 | print_desc(desc); |
| 2286 | } |
| 2287 | } |
| 2288 | printk(KERN_INFO "md: THIS: "); |
| 2289 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2292 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2293 | { |
| 2294 | __u8 *uuid; |
| 2295 | |
| 2296 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2297 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2298 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2299 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2300 | le32_to_cpu(sb->major_version), |
| 2301 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2302 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2303 | sb->set_name, |
| 2304 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2305 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2306 | |
| 2307 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2308 | printk(KERN_INFO |
| 2309 | "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] | 2310 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2311 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2312 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2313 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2314 | le32_to_cpu(sb->level), |
| 2315 | (unsigned long long)le64_to_cpu(sb->size), |
| 2316 | le32_to_cpu(sb->raid_disks), |
| 2317 | le32_to_cpu(sb->layout), |
| 2318 | le32_to_cpu(sb->chunksize), |
| 2319 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2320 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2321 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2322 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2323 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2324 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2325 | sb->devflags, |
| 2326 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2327 | (unsigned long long)le64_to_cpu(sb->events), |
| 2328 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2329 | le32_to_cpu(sb->sb_csum), |
| 2330 | le32_to_cpu(sb->max_dev) |
| 2331 | ); |
| 2332 | } |
| 2333 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2334 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | { |
| 2336 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2337 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2338 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2339 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2340 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2342 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2343 | switch (major_version) { |
| 2344 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2345 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2346 | break; |
| 2347 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2348 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2349 | break; |
| 2350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | } else |
| 2352 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2353 | } |
| 2354 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2355 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2357 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2358 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2359 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | char b[BDEVNAME_SIZE]; |
| 2361 | |
| 2362 | printk("\n"); |
| 2363 | printk("md: **********************************\n"); |
| 2364 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2365 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2366 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2368 | if (mddev->bitmap) |
| 2369 | bitmap_print_sb(mddev->bitmap); |
| 2370 | else |
| 2371 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2372 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2374 | printk("\n"); |
| 2375 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2376 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2377 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
| 2379 | printk("md: **********************************\n"); |
| 2380 | printk("\n"); |
| 2381 | } |
| 2382 | |
| 2383 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2384 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2386 | /* Update each superblock (in-memory image), but |
| 2387 | * if we are allowed to, skip spares which already |
| 2388 | * have the right event counter, or have one earlier |
| 2389 | * (which would mean they aren't being marked as dirty |
| 2390 | * with the rest of the array) |
| 2391 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2392 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2393 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2394 | if (rdev->sb_events == mddev->events || |
| 2395 | (nospares && |
| 2396 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2397 | rdev->sb_events+1 == mddev->events)) { |
| 2398 | /* Don't update this superblock */ |
| 2399 | rdev->sb_loaded = 2; |
| 2400 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2401 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2402 | rdev->sb_loaded = 1; |
| 2403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2407 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2409 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2410 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2412 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2415 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2416 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2417 | if (rdev->raid_disk >= 0 && |
| 2418 | mddev->delta_disks >= 0 && |
| 2419 | !test_bit(In_sync, &rdev->flags) && |
| 2420 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2421 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2422 | |
| 2423 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2424 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2425 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2426 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2427 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2428 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2429 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2430 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2431 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2432 | md_ack_all_badblocks(&rdev->badblocks); |
| 2433 | md_error(mddev, rdev); |
| 2434 | } |
| 2435 | clear_bit(Blocked, &rdev->flags); |
| 2436 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2437 | wake_up(&rdev->blocked_wait); |
| 2438 | } |
| 2439 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2440 | wake_up(&mddev->sb_wait); |
| 2441 | return; |
| 2442 | } |
| 2443 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2444 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2445 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2446 | mddev->utime = get_seconds(); |
| 2447 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2448 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2449 | force_change = 1; |
| 2450 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2451 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2452 | * though if events isn't the right even/odd, we will have to do |
| 2453 | * spares after all |
| 2454 | */ |
| 2455 | nospares = 1; |
| 2456 | if (force_change) |
| 2457 | nospares = 0; |
| 2458 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2459 | /* If the array is degraded, then skipping spares is both |
| 2460 | * dangerous and fairly pointless. |
| 2461 | * Dangerous because a device that was removed from the array |
| 2462 | * might have a event_count that still looks up-to-date, |
| 2463 | * so it can be re-added without a resync. |
| 2464 | * Pointless because if there are any spares to skip, |
| 2465 | * then a recovery will happen and soon that array won't |
| 2466 | * be degraded any more and the spare can go back to sleep then. |
| 2467 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2468 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2469 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2470 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2471 | |
| 2472 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2473 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2474 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2475 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2476 | && mddev->can_decrease_events |
| 2477 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2478 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2479 | mddev->can_decrease_events = 0; |
| 2480 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2481 | /* otherwise we have to go forward and ... */ |
| 2482 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2483 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | |
| 2486 | if (!mddev->events) { |
| 2487 | /* |
| 2488 | * oops, this 64-bit counter should never wrap. |
| 2489 | * Either we are in around ~1 trillion A.C., assuming |
| 2490 | * 1 reboot per second, or we have a bug: |
| 2491 | */ |
| 2492 | MD_BUG(); |
| 2493 | mddev->events --; |
| 2494 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2495 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2496 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2497 | if (rdev->badblocks.changed) |
| 2498 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2499 | if (test_bit(Faulty, &rdev->flags)) |
| 2500 | set_bit(FaultRecorded, &rdev->flags); |
| 2501 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2502 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2503 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2504 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2506 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2507 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2509 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2510 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2512 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2513 | if (rdev->sb_loaded != 1) |
| 2514 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2516 | if (!test_bit(Faulty, &rdev->flags) && |
| 2517 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2518 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2519 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2520 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2521 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2522 | bdevname(rdev->bdev, b), |
| 2523 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2524 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2525 | if (rdev->badblocks.size) { |
| 2526 | md_super_write(mddev, rdev, |
| 2527 | rdev->badblocks.sector, |
| 2528 | rdev->badblocks.size << 9, |
| 2529 | rdev->bb_page); |
| 2530 | rdev->badblocks.size = 0; |
| 2531 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2532 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2533 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2534 | pr_debug("md: %s (skipping faulty)\n", |
| 2535 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2536 | else |
| 2537 | pr_debug("(skipping incremental s/r "); |
| 2538 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2539 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | /* only need to write one superblock... */ |
| 2541 | break; |
| 2542 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2543 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2544 | /* 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] | 2545 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2546 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2547 | if (mddev->in_sync != sync_req || |
| 2548 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2549 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2550 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2551 | goto repeat; |
| 2552 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2553 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2554 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2555 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2556 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2557 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2558 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2559 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2560 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2561 | clear_bit(Blocked, &rdev->flags); |
| 2562 | |
| 2563 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2564 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2565 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2566 | wake_up(&rdev->blocked_wait); |
| 2567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2568 | } |
| 2569 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2570 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2571 | * We want to accept with case. For this we use cmd_match. |
| 2572 | */ |
| 2573 | static int cmd_match(const char *cmd, const char *str) |
| 2574 | { |
| 2575 | /* See if cmd, written into a sysfs file, matches |
| 2576 | * str. They must either be the same, or cmd can |
| 2577 | * have a trailing newline |
| 2578 | */ |
| 2579 | while (*cmd && *str && *cmd == *str) { |
| 2580 | cmd++; |
| 2581 | str++; |
| 2582 | } |
| 2583 | if (*cmd == '\n') |
| 2584 | cmd++; |
| 2585 | if (*str || *cmd) |
| 2586 | return 0; |
| 2587 | return 1; |
| 2588 | } |
| 2589 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2590 | struct rdev_sysfs_entry { |
| 2591 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2592 | ssize_t (*show)(struct md_rdev *, char *); |
| 2593 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2594 | }; |
| 2595 | |
| 2596 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2597 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2598 | { |
| 2599 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2600 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2601 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2602 | if (test_bit(Faulty, &rdev->flags) || |
| 2603 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2604 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2605 | sep = ","; |
| 2606 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2607 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2608 | len += sprintf(page+len, "%sin_sync",sep); |
| 2609 | sep = ","; |
| 2610 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2611 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2612 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2613 | sep = ","; |
| 2614 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2615 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2616 | (rdev->badblocks.unacked_exist |
| 2617 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2618 | len += sprintf(page+len, "%sblocked", sep); |
| 2619 | sep = ","; |
| 2620 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2621 | if (!test_bit(Faulty, &rdev->flags) && |
| 2622 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2623 | len += sprintf(page+len, "%sspare", sep); |
| 2624 | sep = ","; |
| 2625 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2626 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2627 | len += sprintf(page+len, "%swrite_error", sep); |
| 2628 | sep = ","; |
| 2629 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2630 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2631 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2632 | sep = ","; |
| 2633 | } |
| 2634 | if (test_bit(Replacement, &rdev->flags)) { |
| 2635 | len += sprintf(page+len, "%sreplacement", sep); |
| 2636 | sep = ","; |
| 2637 | } |
| 2638 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2639 | return len+sprintf(page+len, "\n"); |
| 2640 | } |
| 2641 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2642 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2643 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2644 | { |
| 2645 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2646 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2647 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2648 | * writemostly - sets write_mostly |
| 2649 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2650 | * blocked - sets the Blocked flags |
| 2651 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2652 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2653 | * write_error - sets WriteErrorSeen |
| 2654 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2655 | */ |
| 2656 | int err = -EINVAL; |
| 2657 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2658 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2659 | if (test_bit(Faulty, &rdev->flags)) |
| 2660 | err = 0; |
| 2661 | else |
| 2662 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2663 | } else if (cmd_match(buf, "remove")) { |
| 2664 | if (rdev->raid_disk >= 0) |
| 2665 | err = -EBUSY; |
| 2666 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2667 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2668 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2669 | if (mddev->pers) |
| 2670 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2671 | md_new_event(mddev); |
| 2672 | err = 0; |
| 2673 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2674 | } else if (cmd_match(buf, "writemostly")) { |
| 2675 | set_bit(WriteMostly, &rdev->flags); |
| 2676 | err = 0; |
| 2677 | } else if (cmd_match(buf, "-writemostly")) { |
| 2678 | clear_bit(WriteMostly, &rdev->flags); |
| 2679 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2680 | } else if (cmd_match(buf, "blocked")) { |
| 2681 | set_bit(Blocked, &rdev->flags); |
| 2682 | err = 0; |
| 2683 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2684 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2685 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2686 | /* metadata handler doesn't understand badblocks, |
| 2687 | * so we need to fail the device |
| 2688 | */ |
| 2689 | md_error(rdev->mddev, rdev); |
| 2690 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2691 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2692 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2693 | wake_up(&rdev->blocked_wait); |
| 2694 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2695 | md_wakeup_thread(rdev->mddev->thread); |
| 2696 | |
| 2697 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2698 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2699 | set_bit(In_sync, &rdev->flags); |
| 2700 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2701 | } else if (cmd_match(buf, "write_error")) { |
| 2702 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2703 | err = 0; |
| 2704 | } else if (cmd_match(buf, "-write_error")) { |
| 2705 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2706 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2707 | } else if (cmd_match(buf, "want_replacement")) { |
| 2708 | /* Any non-spare device that is not a replacement can |
| 2709 | * become want_replacement at any time, but we then need to |
| 2710 | * check if recovery is needed. |
| 2711 | */ |
| 2712 | if (rdev->raid_disk >= 0 && |
| 2713 | !test_bit(Replacement, &rdev->flags)) |
| 2714 | set_bit(WantReplacement, &rdev->flags); |
| 2715 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2716 | md_wakeup_thread(rdev->mddev->thread); |
| 2717 | err = 0; |
| 2718 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2719 | /* Clearing 'want_replacement' is always allowed. |
| 2720 | * Once replacements starts it is too late though. |
| 2721 | */ |
| 2722 | err = 0; |
| 2723 | clear_bit(WantReplacement, &rdev->flags); |
| 2724 | } else if (cmd_match(buf, "replacement")) { |
| 2725 | /* Can only set a device as a replacement when array has not |
| 2726 | * yet been started. Once running, replacement is automatic |
| 2727 | * from spares, or by assigning 'slot'. |
| 2728 | */ |
| 2729 | if (rdev->mddev->pers) |
| 2730 | err = -EBUSY; |
| 2731 | else { |
| 2732 | set_bit(Replacement, &rdev->flags); |
| 2733 | err = 0; |
| 2734 | } |
| 2735 | } else if (cmd_match(buf, "-replacement")) { |
| 2736 | /* Similarly, can only clear Replacement before start */ |
| 2737 | if (rdev->mddev->pers) |
| 2738 | err = -EBUSY; |
| 2739 | else { |
| 2740 | clear_bit(Replacement, &rdev->flags); |
| 2741 | err = 0; |
| 2742 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2743 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2744 | if (!err) |
| 2745 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2746 | return err ? err : len; |
| 2747 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2748 | static struct rdev_sysfs_entry rdev_state = |
| 2749 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2750 | |
| 2751 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2752 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2753 | { |
| 2754 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2755 | } |
| 2756 | |
| 2757 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2758 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2759 | { |
| 2760 | char *e; |
| 2761 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2762 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2763 | atomic_set(&rdev->corrected_errors, n); |
| 2764 | return len; |
| 2765 | } |
| 2766 | return -EINVAL; |
| 2767 | } |
| 2768 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2769 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2770 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2771 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2772 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2773 | { |
| 2774 | if (rdev->raid_disk < 0) |
| 2775 | return sprintf(page, "none\n"); |
| 2776 | else |
| 2777 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2778 | } |
| 2779 | |
| 2780 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2781 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2782 | { |
| 2783 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2784 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2785 | int slot = simple_strtoul(buf, &e, 10); |
| 2786 | if (strncmp(buf, "none", 4)==0) |
| 2787 | slot = -1; |
| 2788 | else if (e==buf || (*e && *e!= '\n')) |
| 2789 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2790 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2791 | /* Setting 'slot' on an active array requires also |
| 2792 | * updating the 'rd%d' link, and communicating |
| 2793 | * with the personality with ->hot_*_disk. |
| 2794 | * For now we only support removing |
| 2795 | * failed/spare devices. This normally happens automatically, |
| 2796 | * but not when the metadata is externally managed. |
| 2797 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2798 | if (rdev->raid_disk == -1) |
| 2799 | return -EEXIST; |
| 2800 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2801 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2802 | return -EINVAL; |
| 2803 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2804 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2805 | if (err) |
| 2806 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2807 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2808 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2809 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2810 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2811 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2812 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2813 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2814 | */ |
| 2815 | |
| 2816 | if (rdev->raid_disk != -1) |
| 2817 | return -EBUSY; |
| 2818 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2819 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2820 | return -EBUSY; |
| 2821 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2822 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2823 | return -EINVAL; |
| 2824 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2825 | if (slot >= rdev->mddev->raid_disks && |
| 2826 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2827 | return -ENOSPC; |
| 2828 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2829 | rdev->raid_disk = slot; |
| 2830 | if (test_bit(In_sync, &rdev->flags)) |
| 2831 | rdev->saved_raid_disk = slot; |
| 2832 | else |
| 2833 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2834 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2835 | err = rdev->mddev->pers-> |
| 2836 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2837 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2838 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2839 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2840 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2841 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2842 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2843 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2844 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2845 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2846 | if (slot >= rdev->mddev->raid_disks && |
| 2847 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2848 | return -ENOSPC; |
| 2849 | rdev->raid_disk = slot; |
| 2850 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2851 | clear_bit(Faulty, &rdev->flags); |
| 2852 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2853 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2854 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2855 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2856 | return len; |
| 2857 | } |
| 2858 | |
| 2859 | |
| 2860 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2861 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2862 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2863 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2864 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2865 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2866 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2870 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2871 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2872 | unsigned long long offset; |
| 2873 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2874 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2875 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2876 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2877 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2878 | /* Must set offset before size, so overlap checks |
| 2879 | * can be sane */ |
| 2880 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2881 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2882 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2883 | return len; |
| 2884 | } |
| 2885 | |
| 2886 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2887 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2888 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2889 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2890 | { |
| 2891 | return sprintf(page, "%llu\n", |
| 2892 | (unsigned long long)rdev->new_data_offset); |
| 2893 | } |
| 2894 | |
| 2895 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2896 | const char *buf, size_t len) |
| 2897 | { |
| 2898 | unsigned long long new_offset; |
| 2899 | struct mddev *mddev = rdev->mddev; |
| 2900 | |
| 2901 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2902 | return -EINVAL; |
| 2903 | |
| 2904 | if (mddev->sync_thread) |
| 2905 | return -EBUSY; |
| 2906 | if (new_offset == rdev->data_offset) |
| 2907 | /* reset is always permitted */ |
| 2908 | ; |
| 2909 | else if (new_offset > rdev->data_offset) { |
| 2910 | /* must not push array size beyond rdev_sectors */ |
| 2911 | if (new_offset - rdev->data_offset |
| 2912 | + mddev->dev_sectors > rdev->sectors) |
| 2913 | return -E2BIG; |
| 2914 | } |
| 2915 | /* Metadata worries about other space details. */ |
| 2916 | |
| 2917 | /* decreasing the offset is inconsistent with a backwards |
| 2918 | * reshape. |
| 2919 | */ |
| 2920 | if (new_offset < rdev->data_offset && |
| 2921 | mddev->reshape_backwards) |
| 2922 | return -EINVAL; |
| 2923 | /* Increasing offset is inconsistent with forwards |
| 2924 | * reshape. reshape_direction should be set to |
| 2925 | * 'backwards' first. |
| 2926 | */ |
| 2927 | if (new_offset > rdev->data_offset && |
| 2928 | !mddev->reshape_backwards) |
| 2929 | return -EINVAL; |
| 2930 | |
| 2931 | if (mddev->pers && mddev->persistent && |
| 2932 | !super_types[mddev->major_version] |
| 2933 | .allow_new_offset(rdev, new_offset)) |
| 2934 | return -E2BIG; |
| 2935 | rdev->new_data_offset = new_offset; |
| 2936 | if (new_offset > rdev->data_offset) |
| 2937 | mddev->reshape_backwards = 1; |
| 2938 | else if (new_offset < rdev->data_offset) |
| 2939 | mddev->reshape_backwards = 0; |
| 2940 | |
| 2941 | return len; |
| 2942 | } |
| 2943 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2944 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2945 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2946 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2947 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2948 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2949 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2950 | } |
| 2951 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2952 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2953 | { |
| 2954 | /* check if two start/length pairs overlap */ |
| 2955 | if (s1+l1 <= s2) |
| 2956 | return 0; |
| 2957 | if (s2+l2 <= s1) |
| 2958 | return 0; |
| 2959 | return 1; |
| 2960 | } |
| 2961 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2962 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2963 | { |
| 2964 | unsigned long long blocks; |
| 2965 | sector_t new; |
| 2966 | |
| 2967 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2968 | return -EINVAL; |
| 2969 | |
| 2970 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2971 | return -EINVAL; /* sector conversion overflow */ |
| 2972 | |
| 2973 | new = blocks * 2; |
| 2974 | if (new != blocks * 2) |
| 2975 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2976 | |
| 2977 | *sectors = new; |
| 2978 | return 0; |
| 2979 | } |
| 2980 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2981 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2982 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2983 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2984 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2985 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2986 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2987 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2988 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2989 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2990 | if (rdev->data_offset != rdev->new_data_offset) |
| 2991 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2992 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2993 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2994 | sectors = super_types[my_mddev->major_version]. |
| 2995 | rdev_size_change(rdev, sectors); |
| 2996 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2997 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2998 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2999 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3000 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3001 | if (!my_mddev->pers->resize) |
| 3002 | /* Cannot change size for RAID0 or Linear etc */ |
| 3003 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3004 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3005 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3006 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3007 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3008 | rdev->sectors = sectors; |
| 3009 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3010 | /* need to check that all other rdevs with the same ->bdev |
| 3011 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3012 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3013 | * we have to change it back, we will have the lock again. |
| 3014 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3015 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3016 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3017 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3018 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3019 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3020 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3021 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3022 | |
| 3023 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3024 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3025 | if (rdev->bdev == rdev2->bdev && |
| 3026 | rdev != rdev2 && |
| 3027 | overlaps(rdev->data_offset, rdev->sectors, |
| 3028 | rdev2->data_offset, |
| 3029 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3030 | overlap = 1; |
| 3031 | break; |
| 3032 | } |
| 3033 | mddev_unlock(mddev); |
| 3034 | if (overlap) { |
| 3035 | mddev_put(mddev); |
| 3036 | break; |
| 3037 | } |
| 3038 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3039 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3040 | if (overlap) { |
| 3041 | /* Someone else could have slipped in a size |
| 3042 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3043 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3044 | * safe, and trust userspace not to race with |
| 3045 | * itself |
| 3046 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3047 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3048 | return -EBUSY; |
| 3049 | } |
| 3050 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3051 | return len; |
| 3052 | } |
| 3053 | |
| 3054 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3055 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3056 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3057 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3058 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3059 | { |
| 3060 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3061 | |
| 3062 | if (test_bit(In_sync, &rdev->flags) || |
| 3063 | recovery_start == MaxSector) |
| 3064 | return sprintf(page, "none\n"); |
| 3065 | |
| 3066 | return sprintf(page, "%llu\n", recovery_start); |
| 3067 | } |
| 3068 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3069 | 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] | 3070 | { |
| 3071 | unsigned long long recovery_start; |
| 3072 | |
| 3073 | if (cmd_match(buf, "none")) |
| 3074 | recovery_start = MaxSector; |
| 3075 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3076 | return -EINVAL; |
| 3077 | |
| 3078 | if (rdev->mddev->pers && |
| 3079 | rdev->raid_disk >= 0) |
| 3080 | return -EBUSY; |
| 3081 | |
| 3082 | rdev->recovery_offset = recovery_start; |
| 3083 | if (recovery_start == MaxSector) |
| 3084 | set_bit(In_sync, &rdev->flags); |
| 3085 | else |
| 3086 | clear_bit(In_sync, &rdev->flags); |
| 3087 | return len; |
| 3088 | } |
| 3089 | |
| 3090 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3091 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3092 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3093 | |
| 3094 | static ssize_t |
| 3095 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3096 | static ssize_t |
| 3097 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3098 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3099 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3100 | { |
| 3101 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3102 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3103 | 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] | 3104 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3105 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3106 | /* Maybe that ack was all we needed */ |
| 3107 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3108 | wake_up(&rdev->blocked_wait); |
| 3109 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3110 | } |
| 3111 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3112 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3113 | |
| 3114 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3115 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3116 | { |
| 3117 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3118 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3119 | 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] | 3120 | { |
| 3121 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3122 | } |
| 3123 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3124 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3125 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3126 | static struct attribute *rdev_default_attrs[] = { |
| 3127 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3128 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3129 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3130 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3131 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3132 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3133 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3134 | &rdev_bad_blocks.attr, |
| 3135 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3136 | NULL, |
| 3137 | }; |
| 3138 | static ssize_t |
| 3139 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3140 | { |
| 3141 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3142 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3143 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3144 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3145 | |
| 3146 | if (!entry->show) |
| 3147 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3148 | |
| 3149 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3150 | if (!rv) { |
| 3151 | if (rdev->mddev == NULL) |
| 3152 | rv = -EBUSY; |
| 3153 | else |
| 3154 | rv = entry->show(rdev, page); |
| 3155 | mddev_unlock(mddev); |
| 3156 | } |
| 3157 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | static ssize_t |
| 3161 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3162 | const char *page, size_t length) |
| 3163 | { |
| 3164 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3165 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3166 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3167 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3168 | |
| 3169 | if (!entry->store) |
| 3170 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3171 | if (!capable(CAP_SYS_ADMIN)) |
| 3172 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3173 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3174 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3175 | if (rdev->mddev == NULL) |
| 3176 | rv = -EBUSY; |
| 3177 | else |
| 3178 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3179 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3180 | } |
| 3181 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | static void rdev_free(struct kobject *ko) |
| 3185 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3186 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3187 | kfree(rdev); |
| 3188 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3189 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3190 | .show = rdev_attr_show, |
| 3191 | .store = rdev_attr_store, |
| 3192 | }; |
| 3193 | static struct kobj_type rdev_ktype = { |
| 3194 | .release = rdev_free, |
| 3195 | .sysfs_ops = &rdev_sysfs_ops, |
| 3196 | .default_attrs = rdev_default_attrs, |
| 3197 | }; |
| 3198 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3199 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3200 | { |
| 3201 | rdev->desc_nr = -1; |
| 3202 | rdev->saved_raid_disk = -1; |
| 3203 | rdev->raid_disk = -1; |
| 3204 | rdev->flags = 0; |
| 3205 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3206 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3207 | rdev->sb_events = 0; |
| 3208 | rdev->last_read_error.tv_sec = 0; |
| 3209 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3210 | rdev->sb_loaded = 0; |
| 3211 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3212 | atomic_set(&rdev->nr_pending, 0); |
| 3213 | atomic_set(&rdev->read_errors, 0); |
| 3214 | atomic_set(&rdev->corrected_errors, 0); |
| 3215 | |
| 3216 | INIT_LIST_HEAD(&rdev->same_set); |
| 3217 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3218 | |
| 3219 | /* Add space to store bad block list. |
| 3220 | * This reserves the space even on arrays where it cannot |
| 3221 | * be used - I wonder if that matters |
| 3222 | */ |
| 3223 | rdev->badblocks.count = 0; |
| 3224 | rdev->badblocks.shift = 0; |
| 3225 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3226 | seqlock_init(&rdev->badblocks.lock); |
| 3227 | if (rdev->badblocks.page == NULL) |
| 3228 | return -ENOMEM; |
| 3229 | |
| 3230 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3231 | } |
| 3232 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | /* |
| 3234 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3235 | * |
| 3236 | * mark the device faulty if: |
| 3237 | * |
| 3238 | * - the device is nonexistent (zero size) |
| 3239 | * - the device has no valid superblock |
| 3240 | * |
| 3241 | * a faulty rdev _never_ has rdev->sb set. |
| 3242 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3243 | 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] | 3244 | { |
| 3245 | char b[BDEVNAME_SIZE]; |
| 3246 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3247 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | sector_t size; |
| 3249 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3250 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | if (!rdev) { |
| 3252 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3253 | return ERR_PTR(-ENOMEM); |
| 3254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3256 | err = md_rdev_init(rdev); |
| 3257 | if (err) |
| 3258 | goto abort_free; |
| 3259 | err = alloc_disk_sb(rdev); |
| 3260 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | goto abort_free; |
| 3262 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3263 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | if (err) |
| 3265 | goto abort_free; |
| 3266 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3267 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3268 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3269 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | if (!size) { |
| 3271 | printk(KERN_WARNING |
| 3272 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3273 | bdevname(rdev->bdev,b)); |
| 3274 | err = -EINVAL; |
| 3275 | goto abort_free; |
| 3276 | } |
| 3277 | |
| 3278 | if (super_format >= 0) { |
| 3279 | err = super_types[super_format]. |
| 3280 | load_super(rdev, NULL, super_minor); |
| 3281 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3282 | printk(KERN_WARNING |
| 3283 | "md: %s does not have a valid v%d.%d " |
| 3284 | "superblock, not importing!\n", |
| 3285 | bdevname(rdev->bdev,b), |
| 3286 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | goto abort_free; |
| 3288 | } |
| 3289 | if (err < 0) { |
| 3290 | printk(KERN_WARNING |
| 3291 | "md: could not read %s's sb, not importing!\n", |
| 3292 | bdevname(rdev->bdev,b)); |
| 3293 | goto abort_free; |
| 3294 | } |
| 3295 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3296 | if (super_format == -1) |
| 3297 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3298 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | return rdev; |
| 3301 | |
| 3302 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3303 | if (rdev->bdev) |
| 3304 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3305 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | kfree(rdev); |
| 3307 | return ERR_PTR(err); |
| 3308 | } |
| 3309 | |
| 3310 | /* |
| 3311 | * Check a full RAID array for plausibility |
| 3312 | */ |
| 3313 | |
| 3314 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3315 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | { |
| 3317 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3318 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | char b[BDEVNAME_SIZE]; |
| 3320 | |
| 3321 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3322 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | switch (super_types[mddev->major_version]. |
| 3324 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3325 | case 1: |
| 3326 | freshest = rdev; |
| 3327 | break; |
| 3328 | case 0: |
| 3329 | break; |
| 3330 | default: |
| 3331 | printk( KERN_ERR \ |
| 3332 | "md: fatal superblock inconsistency in %s" |
| 3333 | " -- removing from array\n", |
| 3334 | bdevname(rdev->bdev,b)); |
| 3335 | kick_rdev_from_array(rdev); |
| 3336 | } |
| 3337 | |
| 3338 | |
| 3339 | super_types[mddev->major_version]. |
| 3340 | validate_super(mddev, freshest); |
| 3341 | |
| 3342 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3343 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3344 | if (mddev->max_disks && |
| 3345 | (rdev->desc_nr >= mddev->max_disks || |
| 3346 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3347 | printk(KERN_WARNING |
| 3348 | "md: %s: %s: only %d devices permitted\n", |
| 3349 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3350 | mddev->max_disks); |
| 3351 | kick_rdev_from_array(rdev); |
| 3352 | continue; |
| 3353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | if (rdev != freshest) |
| 3355 | if (super_types[mddev->major_version]. |
| 3356 | validate_super(mddev, rdev)) { |
| 3357 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3358 | " from array!\n", |
| 3359 | bdevname(rdev->bdev,b)); |
| 3360 | kick_rdev_from_array(rdev); |
| 3361 | continue; |
| 3362 | } |
| 3363 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3364 | rdev->desc_nr = i++; |
| 3365 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3366 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3367 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3368 | rdev->raid_disk = -1; |
| 3369 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | } |
| 3371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | } |
| 3373 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3374 | /* Read a fixed-point number. |
| 3375 | * Numbers in sysfs attributes should be in "standard" units where |
| 3376 | * possible, so time should be in seconds. |
| 3377 | * However we internally use a a much smaller unit such as |
| 3378 | * milliseconds or jiffies. |
| 3379 | * This function takes a decimal number with a possible fractional |
| 3380 | * component, and produces an integer which is the result of |
| 3381 | * multiplying that number by 10^'scale'. |
| 3382 | * all without any floating-point arithmetic. |
| 3383 | */ |
| 3384 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3385 | { |
| 3386 | unsigned long result = 0; |
| 3387 | long decimals = -1; |
| 3388 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3389 | if (*cp == '.') |
| 3390 | decimals = 0; |
| 3391 | else if (decimals < scale) { |
| 3392 | unsigned int value; |
| 3393 | value = *cp - '0'; |
| 3394 | result = result * 10 + value; |
| 3395 | if (decimals >= 0) |
| 3396 | decimals++; |
| 3397 | } |
| 3398 | cp++; |
| 3399 | } |
| 3400 | if (*cp == '\n') |
| 3401 | cp++; |
| 3402 | if (*cp) |
| 3403 | return -EINVAL; |
| 3404 | if (decimals < 0) |
| 3405 | decimals = 0; |
| 3406 | while (decimals < scale) { |
| 3407 | result *= 10; |
| 3408 | decimals ++; |
| 3409 | } |
| 3410 | *res = result; |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3415 | static void md_safemode_timeout(unsigned long data); |
| 3416 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3417 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3418 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3419 | { |
| 3420 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3421 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3422 | } |
| 3423 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3424 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3425 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3426 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3427 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3428 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3429 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3430 | if (msec == 0) |
| 3431 | mddev->safemode_delay = 0; |
| 3432 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3433 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3434 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3435 | if (mddev->safemode_delay == 0) |
| 3436 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3437 | if (mddev->safemode_delay < old_delay) |
| 3438 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3439 | } |
| 3440 | return len; |
| 3441 | } |
| 3442 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3443 | __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] | 3444 | |
| 3445 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3446 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3447 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3448 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3449 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3450 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3451 | else if (mddev->clevel[0]) |
| 3452 | return sprintf(page, "%s\n", mddev->clevel); |
| 3453 | else if (mddev->level != LEVEL_NONE) |
| 3454 | return sprintf(page, "%d\n", mddev->level); |
| 3455 | else |
| 3456 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3457 | } |
| 3458 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3459 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3460 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3461 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3462 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3463 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3464 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3465 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3466 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3467 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3468 | |
| 3469 | if (mddev->pers == NULL) { |
| 3470 | if (len == 0) |
| 3471 | return 0; |
| 3472 | if (len >= sizeof(mddev->clevel)) |
| 3473 | return -ENOSPC; |
| 3474 | strncpy(mddev->clevel, buf, len); |
| 3475 | if (mddev->clevel[len-1] == '\n') |
| 3476 | len--; |
| 3477 | mddev->clevel[len] = 0; |
| 3478 | mddev->level = LEVEL_NONE; |
| 3479 | return rv; |
| 3480 | } |
| 3481 | |
| 3482 | /* request to change the personality. Need to ensure: |
| 3483 | * - array is not engaged in resync/recovery/reshape |
| 3484 | * - old personality can be suspended |
| 3485 | * - new personality will access other array. |
| 3486 | */ |
| 3487 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3488 | if (mddev->sync_thread || |
| 3489 | mddev->reshape_position != MaxSector || |
| 3490 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3491 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3492 | |
| 3493 | if (!mddev->pers->quiesce) { |
| 3494 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3495 | mdname(mddev), mddev->pers->name); |
| 3496 | return -EINVAL; |
| 3497 | } |
| 3498 | |
| 3499 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3500 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3501 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3502 | strncpy(clevel, buf, len); |
| 3503 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3504 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3505 | clevel[len] = 0; |
| 3506 | if (strict_strtol(clevel, 10, &level)) |
| 3507 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3508 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3509 | if (request_module("md-%s", clevel) != 0) |
| 3510 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3511 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3512 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3513 | if (!pers || !try_module_get(pers->owner)) { |
| 3514 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3515 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3516 | return -EINVAL; |
| 3517 | } |
| 3518 | spin_unlock(&pers_lock); |
| 3519 | |
| 3520 | if (pers == mddev->pers) { |
| 3521 | /* Nothing to do! */ |
| 3522 | module_put(pers->owner); |
| 3523 | return rv; |
| 3524 | } |
| 3525 | if (!pers->takeover) { |
| 3526 | module_put(pers->owner); |
| 3527 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3528 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3529 | return -EINVAL; |
| 3530 | } |
| 3531 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3532 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3533 | rdev->new_raid_disk = rdev->raid_disk; |
| 3534 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3535 | /* ->takeover must set new_* and/or delta_disks |
| 3536 | * if it succeeds, and may set them when it fails. |
| 3537 | */ |
| 3538 | priv = pers->takeover(mddev); |
| 3539 | if (IS_ERR(priv)) { |
| 3540 | mddev->new_level = mddev->level; |
| 3541 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3542 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3543 | mddev->raid_disks -= mddev->delta_disks; |
| 3544 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3545 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3546 | module_put(pers->owner); |
| 3547 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3548 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3549 | return PTR_ERR(priv); |
| 3550 | } |
| 3551 | |
| 3552 | /* Looks like we have a winner */ |
| 3553 | mddev_suspend(mddev); |
| 3554 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3555 | |
| 3556 | if (mddev->pers->sync_request == NULL && |
| 3557 | pers->sync_request != NULL) { |
| 3558 | /* need to add the md_redundancy_group */ |
| 3559 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3560 | printk(KERN_WARNING |
| 3561 | "md: cannot register extra attributes for %s\n", |
| 3562 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3563 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3564 | } |
| 3565 | if (mddev->pers->sync_request != NULL && |
| 3566 | pers->sync_request == NULL) { |
| 3567 | /* need to remove the md_redundancy_group */ |
| 3568 | if (mddev->to_remove == NULL) |
| 3569 | mddev->to_remove = &md_redundancy_group; |
| 3570 | } |
| 3571 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3572 | if (mddev->pers->sync_request == NULL && |
| 3573 | mddev->external) { |
| 3574 | /* We are converting from a no-redundancy array |
| 3575 | * to a redundancy array and metadata is managed |
| 3576 | * externally so we need to be sure that writes |
| 3577 | * won't block due to a need to transition |
| 3578 | * clean->dirty |
| 3579 | * until external management is started. |
| 3580 | */ |
| 3581 | mddev->in_sync = 0; |
| 3582 | mddev->safemode_delay = 0; |
| 3583 | mddev->safemode = 0; |
| 3584 | } |
| 3585 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3586 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3587 | if (rdev->raid_disk < 0) |
| 3588 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3589 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3590 | rdev->new_raid_disk = -1; |
| 3591 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3592 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3593 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3594 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3595 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3596 | if (rdev->raid_disk < 0) |
| 3597 | continue; |
| 3598 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3599 | continue; |
| 3600 | rdev->raid_disk = rdev->new_raid_disk; |
| 3601 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3602 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3603 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3604 | if (sysfs_link_rdev(mddev, rdev)) |
| 3605 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3606 | " for %s after level change\n", |
| 3607 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3608 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3612 | mddev->pers = pers; |
| 3613 | mddev->private = priv; |
| 3614 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3615 | mddev->level = mddev->new_level; |
| 3616 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3617 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3618 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3619 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3620 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3621 | if (mddev->pers->sync_request == NULL) { |
| 3622 | /* this is now an array without redundancy, so |
| 3623 | * it must always be in_sync |
| 3624 | */ |
| 3625 | mddev->in_sync = 1; |
| 3626 | del_timer_sync(&mddev->safemode_timer); |
| 3627 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3628 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3629 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3630 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3631 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3632 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3633 | return rv; |
| 3634 | } |
| 3635 | |
| 3636 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3637 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3638 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3639 | |
| 3640 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3641 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3642 | { |
| 3643 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3644 | if (mddev->reshape_position != MaxSector && |
| 3645 | mddev->layout != mddev->new_layout) |
| 3646 | return sprintf(page, "%d (%d)\n", |
| 3647 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3648 | return sprintf(page, "%d\n", mddev->layout); |
| 3649 | } |
| 3650 | |
| 3651 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3652 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3653 | { |
| 3654 | char *e; |
| 3655 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3656 | |
| 3657 | if (!*buf || (*e && *e != '\n')) |
| 3658 | return -EINVAL; |
| 3659 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3660 | if (mddev->pers) { |
| 3661 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3662 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3663 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3664 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3665 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3666 | if (err) { |
| 3667 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3668 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3669 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3670 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3671 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3672 | if (mddev->reshape_position == MaxSector) |
| 3673 | mddev->layout = n; |
| 3674 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3675 | return len; |
| 3676 | } |
| 3677 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3678 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3679 | |
| 3680 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3681 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3682 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3683 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3684 | if (mddev->raid_disks == 0) |
| 3685 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3686 | if (mddev->reshape_position != MaxSector && |
| 3687 | mddev->delta_disks != 0) |
| 3688 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3689 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3690 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3691 | } |
| 3692 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3693 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3694 | |
| 3695 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3696 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3697 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3698 | char *e; |
| 3699 | int rv = 0; |
| 3700 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3701 | |
| 3702 | if (!*buf || (*e && *e != '\n')) |
| 3703 | return -EINVAL; |
| 3704 | |
| 3705 | if (mddev->pers) |
| 3706 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3707 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3708 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3709 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3710 | |
| 3711 | rdev_for_each(rdev, mddev) { |
| 3712 | if (olddisks < n && |
| 3713 | rdev->data_offset < rdev->new_data_offset) |
| 3714 | return -EINVAL; |
| 3715 | if (olddisks > n && |
| 3716 | rdev->data_offset > rdev->new_data_offset) |
| 3717 | return -EINVAL; |
| 3718 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3719 | mddev->delta_disks = n - olddisks; |
| 3720 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3721 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3722 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3723 | mddev->raid_disks = n; |
| 3724 | return rv ? rv : len; |
| 3725 | } |
| 3726 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3727 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3728 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3729 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3730 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3731 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3732 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3733 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3734 | return sprintf(page, "%d (%d)\n", |
| 3735 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3736 | mddev->chunk_sectors << 9); |
| 3737 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3741 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3742 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3743 | char *e; |
| 3744 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3745 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3746 | if (!*buf || (*e && *e != '\n')) |
| 3747 | return -EINVAL; |
| 3748 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3749 | if (mddev->pers) { |
| 3750 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3751 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3752 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3753 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3754 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3755 | if (err) { |
| 3756 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3757 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3758 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3759 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3760 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3761 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3762 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3763 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3764 | return len; |
| 3765 | } |
| 3766 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3767 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3768 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3769 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3770 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3771 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3772 | if (mddev->recovery_cp == MaxSector) |
| 3773 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3774 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3775 | } |
| 3776 | |
| 3777 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3778 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3779 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3780 | char *e; |
| 3781 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3782 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3783 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3784 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3785 | if (cmd_match(buf, "none")) |
| 3786 | n = MaxSector; |
| 3787 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3788 | return -EINVAL; |
| 3789 | |
| 3790 | mddev->recovery_cp = n; |
NeilBrown | db07d85 | 2012-10-11 14:22:17 +1100 | [diff] [blame] | 3791 | if (mddev->pers) |
| 3792 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3793 | return len; |
| 3794 | } |
| 3795 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3796 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3797 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3798 | /* |
| 3799 | * The array state can be: |
| 3800 | * |
| 3801 | * clear |
| 3802 | * No devices, no size, no level |
| 3803 | * Equivalent to STOP_ARRAY ioctl |
| 3804 | * inactive |
| 3805 | * May have some settings, but array is not active |
| 3806 | * all IO results in error |
| 3807 | * When written, doesn't tear down array, but just stops it |
| 3808 | * suspended (not supported yet) |
| 3809 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3810 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3811 | * readonly |
| 3812 | * no resync can happen. no superblocks get written. |
| 3813 | * write requests fail |
| 3814 | * read-auto |
| 3815 | * like readonly, but behaves like 'clean' on a write request. |
| 3816 | * |
| 3817 | * clean - no pending writes, but otherwise active. |
| 3818 | * When written to inactive array, starts without resync |
| 3819 | * If a write request arrives then |
| 3820 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3821 | * if not known, block and switch to write-pending |
| 3822 | * If written to an active array that has pending writes, then fails. |
| 3823 | * active |
| 3824 | * fully active: IO and resync can be happening. |
| 3825 | * When written to inactive array, starts with resync |
| 3826 | * |
| 3827 | * write-pending |
| 3828 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3829 | * |
| 3830 | * active-idle |
| 3831 | * like active, but no writes have been seen for a while (100msec). |
| 3832 | * |
| 3833 | */ |
| 3834 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3835 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3836 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3837 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3838 | "write-pending", "active-idle", NULL }; |
| 3839 | |
| 3840 | static int match_word(const char *word, char **list) |
| 3841 | { |
| 3842 | int n; |
| 3843 | for (n=0; list[n]; n++) |
| 3844 | if (cmd_match(word, list[n])) |
| 3845 | break; |
| 3846 | return n; |
| 3847 | } |
| 3848 | |
| 3849 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3850 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3851 | { |
| 3852 | enum array_state st = inactive; |
| 3853 | |
| 3854 | if (mddev->pers) |
| 3855 | switch(mddev->ro) { |
| 3856 | case 1: |
| 3857 | st = readonly; |
| 3858 | break; |
| 3859 | case 2: |
| 3860 | st = read_auto; |
| 3861 | break; |
| 3862 | case 0: |
| 3863 | if (mddev->in_sync) |
| 3864 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3865 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3866 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3867 | else if (mddev->safemode) |
| 3868 | st = active_idle; |
| 3869 | else |
| 3870 | st = active; |
| 3871 | } |
| 3872 | else { |
| 3873 | if (list_empty(&mddev->disks) && |
| 3874 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3875 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3876 | st = clear; |
| 3877 | else |
| 3878 | st = inactive; |
| 3879 | } |
| 3880 | return sprintf(page, "%s\n", array_states[st]); |
| 3881 | } |
| 3882 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3883 | static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev); |
| 3884 | static int md_set_readonly(struct mddev * mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3885 | static int do_md_run(struct mddev * mddev); |
| 3886 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3887 | |
| 3888 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3889 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3890 | { |
| 3891 | int err = -EINVAL; |
| 3892 | enum array_state st = match_word(buf, array_states); |
| 3893 | switch(st) { |
| 3894 | case bad_word: |
| 3895 | break; |
| 3896 | case clear: |
| 3897 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3898 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3899 | break; |
| 3900 | case inactive: |
| 3901 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3902 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3903 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3904 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3905 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3906 | break; |
| 3907 | case suspended: |
| 3908 | break; /* not supported yet */ |
| 3909 | case readonly: |
| 3910 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3911 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3912 | else { |
| 3913 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3914 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3915 | err = do_md_run(mddev); |
| 3916 | } |
| 3917 | break; |
| 3918 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3919 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3920 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3921 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3922 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3923 | err = restart_array(mddev); |
| 3924 | if (err == 0) { |
| 3925 | mddev->ro = 2; |
| 3926 | set_disk_ro(mddev->gendisk, 0); |
| 3927 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3928 | } else { |
| 3929 | mddev->ro = 2; |
| 3930 | err = do_md_run(mddev); |
| 3931 | } |
| 3932 | break; |
| 3933 | case clean: |
| 3934 | if (mddev->pers) { |
| 3935 | restart_array(mddev); |
| 3936 | spin_lock_irq(&mddev->write_lock); |
| 3937 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3938 | if (mddev->in_sync == 0) { |
| 3939 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3940 | if (mddev->safemode == 1) |
| 3941 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3942 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3943 | } |
| 3944 | err = 0; |
| 3945 | } else |
| 3946 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3947 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3948 | } else |
| 3949 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3950 | break; |
| 3951 | case active: |
| 3952 | if (mddev->pers) { |
| 3953 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3954 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3955 | wake_up(&mddev->sb_wait); |
| 3956 | err = 0; |
| 3957 | } else { |
| 3958 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3959 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3960 | err = do_md_run(mddev); |
| 3961 | } |
| 3962 | break; |
| 3963 | case write_pending: |
| 3964 | case active_idle: |
| 3965 | /* these cannot be set */ |
| 3966 | break; |
| 3967 | } |
| 3968 | if (err) |
| 3969 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3970 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3971 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3972 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3973 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3974 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3975 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3976 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3977 | static struct md_sysfs_entry md_array_state = |
| 3978 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3979 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3980 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3981 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3982 | return sprintf(page, "%d\n", |
| 3983 | atomic_read(&mddev->max_corr_read_errors)); |
| 3984 | } |
| 3985 | |
| 3986 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3987 | 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] | 3988 | { |
| 3989 | char *e; |
| 3990 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3991 | |
| 3992 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3993 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3994 | return len; |
| 3995 | } |
| 3996 | return -EINVAL; |
| 3997 | } |
| 3998 | |
| 3999 | static struct md_sysfs_entry max_corr_read_errors = |
| 4000 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4001 | max_corrected_read_errors_store); |
| 4002 | |
| 4003 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4004 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4005 | { |
| 4006 | return -EINVAL; |
| 4007 | } |
| 4008 | |
| 4009 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4010 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4011 | { |
| 4012 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4013 | /* The new device is added to the array. |
| 4014 | * If the array has a persistent superblock, we read the |
| 4015 | * superblock to initialise info and check validity. |
| 4016 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4017 | * which mainly checks size. |
| 4018 | */ |
| 4019 | char *e; |
| 4020 | int major = simple_strtoul(buf, &e, 10); |
| 4021 | int minor; |
| 4022 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4023 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4024 | int err; |
| 4025 | |
| 4026 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4027 | return -EINVAL; |
| 4028 | minor = simple_strtoul(e+1, &e, 10); |
| 4029 | if (*e && *e != '\n') |
| 4030 | return -EINVAL; |
| 4031 | dev = MKDEV(major, minor); |
| 4032 | if (major != MAJOR(dev) || |
| 4033 | minor != MINOR(dev)) |
| 4034 | return -EOVERFLOW; |
| 4035 | |
| 4036 | |
| 4037 | if (mddev->persistent) { |
| 4038 | rdev = md_import_device(dev, mddev->major_version, |
| 4039 | mddev->minor_version); |
| 4040 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4041 | struct md_rdev *rdev0 |
| 4042 | = list_entry(mddev->disks.next, |
| 4043 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4044 | err = super_types[mddev->major_version] |
| 4045 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4046 | if (err < 0) |
| 4047 | goto out; |
| 4048 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4049 | } else if (mddev->external) |
| 4050 | rdev = md_import_device(dev, -2, -1); |
| 4051 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4052 | rdev = md_import_device(dev, -1, -1); |
| 4053 | |
| 4054 | if (IS_ERR(rdev)) |
| 4055 | return PTR_ERR(rdev); |
| 4056 | err = bind_rdev_to_array(rdev, mddev); |
| 4057 | out: |
| 4058 | if (err) |
| 4059 | export_rdev(rdev); |
| 4060 | return err ? err : len; |
| 4061 | } |
| 4062 | |
| 4063 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4064 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4065 | |
| 4066 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4067 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4068 | { |
| 4069 | char *end; |
| 4070 | unsigned long chunk, end_chunk; |
| 4071 | |
| 4072 | if (!mddev->bitmap) |
| 4073 | goto out; |
| 4074 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4075 | while (*buf) { |
| 4076 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4077 | if (buf == end) break; |
| 4078 | if (*end == '-') { /* range */ |
| 4079 | buf = end + 1; |
| 4080 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4081 | if (buf == end) break; |
| 4082 | } |
| 4083 | if (*end && !isspace(*end)) break; |
| 4084 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4085 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4086 | } |
| 4087 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4088 | out: |
| 4089 | return len; |
| 4090 | } |
| 4091 | |
| 4092 | static struct md_sysfs_entry md_bitmap = |
| 4093 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4094 | |
| 4095 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4096 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4097 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4098 | return sprintf(page, "%llu\n", |
| 4099 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4100 | } |
| 4101 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4102 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4103 | |
| 4104 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4105 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4106 | { |
| 4107 | /* If array is inactive, we can reduce the component size, but |
| 4108 | * not increase it (except from 0). |
| 4109 | * If array is active, we can try an on-line resize |
| 4110 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4111 | sector_t sectors; |
| 4112 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4113 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4114 | if (err < 0) |
| 4115 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4116 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4117 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4118 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4119 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4120 | if (mddev->dev_sectors == 0 || |
| 4121 | mddev->dev_sectors > sectors) |
| 4122 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4123 | else |
| 4124 | err = -ENOSPC; |
| 4125 | } |
| 4126 | return err ? err : len; |
| 4127 | } |
| 4128 | |
| 4129 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4130 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4131 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4132 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4133 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4134 | * This is one of |
| 4135 | * 'none' for arrays with no metadata (good luck...) |
| 4136 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4137 | * or N.M for internally known formats |
| 4138 | */ |
| 4139 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4140 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4141 | { |
| 4142 | if (mddev->persistent) |
| 4143 | return sprintf(page, "%d.%d\n", |
| 4144 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4145 | else if (mddev->external) |
| 4146 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4147 | else |
| 4148 | return sprintf(page, "none\n"); |
| 4149 | } |
| 4150 | |
| 4151 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4152 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4153 | { |
| 4154 | int major, minor; |
| 4155 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4156 | /* Changing the details of 'external' metadata is |
| 4157 | * always permitted. Otherwise there must be |
| 4158 | * no devices attached to the array. |
| 4159 | */ |
| 4160 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4161 | ; |
| 4162 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4163 | return -EBUSY; |
| 4164 | |
| 4165 | if (cmd_match(buf, "none")) { |
| 4166 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4167 | mddev->external = 0; |
| 4168 | mddev->major_version = 0; |
| 4169 | mddev->minor_version = 90; |
| 4170 | return len; |
| 4171 | } |
| 4172 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4173 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4174 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4175 | namelen = sizeof(mddev->metadata_type)-1; |
| 4176 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4177 | mddev->metadata_type[namelen] = 0; |
| 4178 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4179 | mddev->metadata_type[--namelen] = 0; |
| 4180 | mddev->persistent = 0; |
| 4181 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4182 | mddev->major_version = 0; |
| 4183 | mddev->minor_version = 90; |
| 4184 | return len; |
| 4185 | } |
| 4186 | major = simple_strtoul(buf, &e, 10); |
| 4187 | if (e==buf || *e != '.') |
| 4188 | return -EINVAL; |
| 4189 | buf = e+1; |
| 4190 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4191 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4192 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4193 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4194 | return -ENOENT; |
| 4195 | mddev->major_version = major; |
| 4196 | mddev->minor_version = minor; |
| 4197 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4198 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4199 | return len; |
| 4200 | } |
| 4201 | |
| 4202 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4203 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4204 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4205 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4206 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4207 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4208 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4209 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4210 | type = "frozen"; |
| 4211 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4212 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4213 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4214 | type = "reshape"; |
| 4215 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4216 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4217 | type = "resync"; |
| 4218 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4219 | type = "check"; |
| 4220 | else |
| 4221 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4222 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4223 | type = "recover"; |
| 4224 | } |
| 4225 | return sprintf(page, "%s\n", type); |
| 4226 | } |
| 4227 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4228 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4229 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4230 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4231 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4232 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4233 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4234 | return -EINVAL; |
| 4235 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4236 | if (cmd_match(page, "frozen")) |
| 4237 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4238 | else |
| 4239 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4240 | |
| 4241 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4242 | if (mddev->sync_thread) { |
| 4243 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4244 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4245 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4246 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4247 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4248 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4249 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4250 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4251 | else if (cmd_match(page, "recover")) { |
| 4252 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4253 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4254 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4255 | int err; |
| 4256 | if (mddev->pers->start_reshape == NULL) |
| 4257 | return -EINVAL; |
| 4258 | err = mddev->pers->start_reshape(mddev); |
| 4259 | if (err) |
| 4260 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4261 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4262 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4263 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4264 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4265 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4266 | return -EINVAL; |
| 4267 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4268 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4269 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4270 | if (mddev->ro == 2) { |
| 4271 | /* A write to sync_action is enough to justify |
| 4272 | * canceling read-auto mode |
| 4273 | */ |
| 4274 | mddev->ro = 0; |
| 4275 | md_wakeup_thread(mddev->sync_thread); |
| 4276 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4277 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4278 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4279 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4280 | return len; |
| 4281 | } |
| 4282 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4283 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4284 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4285 | { |
| 4286 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4287 | (unsigned long long) |
| 4288 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4291 | static struct md_sysfs_entry md_scan_mode = |
| 4292 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4293 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4294 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4295 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4296 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4297 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4298 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4299 | { |
| 4300 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4301 | mddev->sync_speed_min ? "local": "system"); |
| 4302 | } |
| 4303 | |
| 4304 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4305 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4306 | { |
| 4307 | int min; |
| 4308 | char *e; |
| 4309 | if (strncmp(buf, "system", 6)==0) { |
| 4310 | mddev->sync_speed_min = 0; |
| 4311 | return len; |
| 4312 | } |
| 4313 | min = simple_strtoul(buf, &e, 10); |
| 4314 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4315 | return -EINVAL; |
| 4316 | mddev->sync_speed_min = min; |
| 4317 | return len; |
| 4318 | } |
| 4319 | |
| 4320 | static struct md_sysfs_entry md_sync_min = |
| 4321 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4322 | |
| 4323 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4324 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4325 | { |
| 4326 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4327 | mddev->sync_speed_max ? "local": "system"); |
| 4328 | } |
| 4329 | |
| 4330 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4331 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4332 | { |
| 4333 | int max; |
| 4334 | char *e; |
| 4335 | if (strncmp(buf, "system", 6)==0) { |
| 4336 | mddev->sync_speed_max = 0; |
| 4337 | return len; |
| 4338 | } |
| 4339 | max = simple_strtoul(buf, &e, 10); |
| 4340 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4341 | return -EINVAL; |
| 4342 | mddev->sync_speed_max = max; |
| 4343 | return len; |
| 4344 | } |
| 4345 | |
| 4346 | static struct md_sysfs_entry md_sync_max = |
| 4347 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4348 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4349 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4350 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4351 | { |
| 4352 | return sprintf(page, "%d\n", mddev->degraded); |
| 4353 | } |
| 4354 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4355 | |
| 4356 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4357 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4358 | { |
| 4359 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4360 | } |
| 4361 | |
| 4362 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4363 | 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] | 4364 | { |
| 4365 | long n; |
| 4366 | |
| 4367 | if (strict_strtol(buf, 10, &n)) |
| 4368 | return -EINVAL; |
| 4369 | |
| 4370 | if (n != 0 && n != 1) |
| 4371 | return -EINVAL; |
| 4372 | |
| 4373 | mddev->parallel_resync = n; |
| 4374 | |
| 4375 | if (mddev->sync_thread) |
| 4376 | wake_up(&resync_wait); |
| 4377 | |
| 4378 | return len; |
| 4379 | } |
| 4380 | |
| 4381 | /* force parallel resync, even with shared block devices */ |
| 4382 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4383 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4384 | sync_force_parallel_show, sync_force_parallel_store); |
| 4385 | |
| 4386 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4387 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4388 | { |
| 4389 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4390 | if (mddev->curr_resync == 0) |
| 4391 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4392 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4393 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4394 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4395 | db = resync - mddev->resync_mark_cnt; |
| 4396 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4397 | } |
| 4398 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4399 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4400 | |
| 4401 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4402 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4403 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4404 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4405 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4406 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4407 | return sprintf(page, "none\n"); |
| 4408 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4409 | if (mddev->curr_resync == 1 || |
| 4410 | mddev->curr_resync == 2) |
| 4411 | return sprintf(page, "delayed\n"); |
| 4412 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4413 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4414 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4415 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4416 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4417 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4418 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4419 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4420 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4421 | } |
| 4422 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4423 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4424 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4425 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4426 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4427 | { |
| 4428 | return sprintf(page, "%llu\n", |
| 4429 | (unsigned long long)mddev->resync_min); |
| 4430 | } |
| 4431 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4432 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4433 | { |
| 4434 | unsigned long long min; |
| 4435 | if (strict_strtoull(buf, 10, &min)) |
| 4436 | return -EINVAL; |
| 4437 | if (min > mddev->resync_max) |
| 4438 | return -EINVAL; |
| 4439 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4440 | return -EBUSY; |
| 4441 | |
| 4442 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4443 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4444 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4445 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4446 | return -EINVAL; |
| 4447 | } |
| 4448 | mddev->resync_min = min; |
| 4449 | |
| 4450 | return len; |
| 4451 | } |
| 4452 | |
| 4453 | static struct md_sysfs_entry md_min_sync = |
| 4454 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4455 | |
| 4456 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4457 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4458 | { |
| 4459 | if (mddev->resync_max == MaxSector) |
| 4460 | return sprintf(page, "max\n"); |
| 4461 | else |
| 4462 | return sprintf(page, "%llu\n", |
| 4463 | (unsigned long long)mddev->resync_max); |
| 4464 | } |
| 4465 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4466 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4467 | { |
| 4468 | if (strncmp(buf, "max", 3) == 0) |
| 4469 | mddev->resync_max = MaxSector; |
| 4470 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4471 | unsigned long long max; |
| 4472 | if (strict_strtoull(buf, 10, &max)) |
| 4473 | return -EINVAL; |
| 4474 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4475 | return -EINVAL; |
| 4476 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4477 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4478 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4479 | return -EBUSY; |
| 4480 | |
| 4481 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4482 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4483 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4484 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4485 | return -EINVAL; |
| 4486 | } |
| 4487 | mddev->resync_max = max; |
| 4488 | } |
| 4489 | wake_up(&mddev->recovery_wait); |
| 4490 | return len; |
| 4491 | } |
| 4492 | |
| 4493 | static struct md_sysfs_entry md_max_sync = |
| 4494 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4495 | |
| 4496 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4497 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4498 | { |
| 4499 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4500 | } |
| 4501 | |
| 4502 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4503 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4504 | { |
| 4505 | char *e; |
| 4506 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4507 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4508 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4509 | if (mddev->pers == NULL || |
| 4510 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4511 | return -EINVAL; |
| 4512 | if (buf == e || (*e && *e != '\n')) |
| 4513 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4514 | |
| 4515 | mddev->suspend_lo = new; |
| 4516 | if (new >= old) |
| 4517 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4518 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4519 | else { |
| 4520 | /* Expanding suspended region - need to wait */ |
| 4521 | mddev->pers->quiesce(mddev, 1); |
| 4522 | mddev->pers->quiesce(mddev, 0); |
| 4523 | } |
| 4524 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4525 | } |
| 4526 | static struct md_sysfs_entry md_suspend_lo = |
| 4527 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4528 | |
| 4529 | |
| 4530 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4531 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4532 | { |
| 4533 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4534 | } |
| 4535 | |
| 4536 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4537 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4538 | { |
| 4539 | char *e; |
| 4540 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4541 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4542 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4543 | if (mddev->pers == NULL || |
| 4544 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4545 | return -EINVAL; |
| 4546 | if (buf == e || (*e && *e != '\n')) |
| 4547 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4548 | |
| 4549 | mddev->suspend_hi = new; |
| 4550 | if (new <= old) |
| 4551 | /* Shrinking suspended region */ |
| 4552 | mddev->pers->quiesce(mddev, 2); |
| 4553 | else { |
| 4554 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4555 | mddev->pers->quiesce(mddev, 1); |
| 4556 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4557 | } |
| 4558 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4559 | } |
| 4560 | static struct md_sysfs_entry md_suspend_hi = |
| 4561 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4562 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4563 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4564 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4565 | { |
| 4566 | if (mddev->reshape_position != MaxSector) |
| 4567 | return sprintf(page, "%llu\n", |
| 4568 | (unsigned long long)mddev->reshape_position); |
| 4569 | strcpy(page, "none\n"); |
| 4570 | return 5; |
| 4571 | } |
| 4572 | |
| 4573 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4574 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4575 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4576 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4577 | char *e; |
| 4578 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4579 | if (mddev->pers) |
| 4580 | return -EBUSY; |
| 4581 | if (buf == e || (*e && *e != '\n')) |
| 4582 | return -EINVAL; |
| 4583 | mddev->reshape_position = new; |
| 4584 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4585 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4586 | mddev->new_level = mddev->level; |
| 4587 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4588 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4589 | rdev_for_each(rdev, mddev) |
| 4590 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4591 | return len; |
| 4592 | } |
| 4593 | |
| 4594 | static struct md_sysfs_entry md_reshape_position = |
| 4595 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4596 | reshape_position_store); |
| 4597 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4598 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4599 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4600 | { |
| 4601 | return sprintf(page, "%s\n", |
| 4602 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4603 | } |
| 4604 | |
| 4605 | static ssize_t |
| 4606 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4607 | { |
| 4608 | int backwards = 0; |
| 4609 | if (cmd_match(buf, "forwards")) |
| 4610 | backwards = 0; |
| 4611 | else if (cmd_match(buf, "backwards")) |
| 4612 | backwards = 1; |
| 4613 | else |
| 4614 | return -EINVAL; |
| 4615 | if (mddev->reshape_backwards == backwards) |
| 4616 | return len; |
| 4617 | |
| 4618 | /* check if we are allowed to change */ |
| 4619 | if (mddev->delta_disks) |
| 4620 | return -EBUSY; |
| 4621 | |
| 4622 | if (mddev->persistent && |
| 4623 | mddev->major_version == 0) |
| 4624 | return -EINVAL; |
| 4625 | |
| 4626 | mddev->reshape_backwards = backwards; |
| 4627 | return len; |
| 4628 | } |
| 4629 | |
| 4630 | static struct md_sysfs_entry md_reshape_direction = |
| 4631 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4632 | reshape_direction_store); |
| 4633 | |
| 4634 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4635 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4636 | { |
| 4637 | if (mddev->external_size) |
| 4638 | return sprintf(page, "%llu\n", |
| 4639 | (unsigned long long)mddev->array_sectors/2); |
| 4640 | else |
| 4641 | return sprintf(page, "default\n"); |
| 4642 | } |
| 4643 | |
| 4644 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4645 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4646 | { |
| 4647 | sector_t sectors; |
| 4648 | |
| 4649 | if (strncmp(buf, "default", 7) == 0) { |
| 4650 | if (mddev->pers) |
| 4651 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4652 | else |
| 4653 | sectors = mddev->array_sectors; |
| 4654 | |
| 4655 | mddev->external_size = 0; |
| 4656 | } else { |
| 4657 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4658 | return -EINVAL; |
| 4659 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4660 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4661 | |
| 4662 | mddev->external_size = 1; |
| 4663 | } |
| 4664 | |
| 4665 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4666 | if (mddev->pers) { |
| 4667 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4668 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4669 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4670 | return len; |
| 4671 | } |
| 4672 | |
| 4673 | static struct md_sysfs_entry md_array_size = |
| 4674 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4675 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4676 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4677 | static struct attribute *md_default_attrs[] = { |
| 4678 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4679 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4680 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4681 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4682 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4683 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4684 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4685 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4686 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4687 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4688 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4689 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4690 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4691 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4692 | NULL, |
| 4693 | }; |
| 4694 | |
| 4695 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4696 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4697 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4698 | &md_sync_min.attr, |
| 4699 | &md_sync_max.attr, |
| 4700 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4701 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4702 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4703 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4704 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4705 | &md_suspend_lo.attr, |
| 4706 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4707 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4708 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4709 | NULL, |
| 4710 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4711 | static struct attribute_group md_redundancy_group = { |
| 4712 | .name = NULL, |
| 4713 | .attrs = md_redundancy_attrs, |
| 4714 | }; |
| 4715 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4716 | |
| 4717 | static ssize_t |
| 4718 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4719 | { |
| 4720 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4721 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4722 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4723 | |
| 4724 | if (!entry->show) |
| 4725 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4726 | spin_lock(&all_mddevs_lock); |
| 4727 | if (list_empty(&mddev->all_mddevs)) { |
| 4728 | spin_unlock(&all_mddevs_lock); |
| 4729 | return -EBUSY; |
| 4730 | } |
| 4731 | mddev_get(mddev); |
| 4732 | spin_unlock(&all_mddevs_lock); |
| 4733 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4734 | rv = mddev_lock(mddev); |
| 4735 | if (!rv) { |
| 4736 | rv = entry->show(mddev, page); |
| 4737 | mddev_unlock(mddev); |
| 4738 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4739 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4740 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | static ssize_t |
| 4744 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4745 | const char *page, size_t length) |
| 4746 | { |
| 4747 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4748 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4749 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4750 | |
| 4751 | if (!entry->store) |
| 4752 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4753 | if (!capable(CAP_SYS_ADMIN)) |
| 4754 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4755 | spin_lock(&all_mddevs_lock); |
| 4756 | if (list_empty(&mddev->all_mddevs)) { |
| 4757 | spin_unlock(&all_mddevs_lock); |
| 4758 | return -EBUSY; |
| 4759 | } |
| 4760 | mddev_get(mddev); |
| 4761 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 4762 | if (entry->store == new_dev_store) |
| 4763 | flush_workqueue(md_misc_wq); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4764 | rv = mddev_lock(mddev); |
| 4765 | if (!rv) { |
| 4766 | rv = entry->store(mddev, page, length); |
| 4767 | mddev_unlock(mddev); |
| 4768 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4769 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4770 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | static void md_free(struct kobject *ko) |
| 4774 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4775 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4776 | |
| 4777 | if (mddev->sysfs_state) |
| 4778 | sysfs_put(mddev->sysfs_state); |
| 4779 | |
| 4780 | if (mddev->gendisk) { |
| 4781 | del_gendisk(mddev->gendisk); |
| 4782 | put_disk(mddev->gendisk); |
| 4783 | } |
| 4784 | if (mddev->queue) |
| 4785 | blk_cleanup_queue(mddev->queue); |
| 4786 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4787 | kfree(mddev); |
| 4788 | } |
| 4789 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4790 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4791 | .show = md_attr_show, |
| 4792 | .store = md_attr_store, |
| 4793 | }; |
| 4794 | static struct kobj_type md_ktype = { |
| 4795 | .release = md_free, |
| 4796 | .sysfs_ops = &md_sysfs_ops, |
| 4797 | .default_attrs = md_default_attrs, |
| 4798 | }; |
| 4799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | int mdp_major = 0; |
| 4801 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4802 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4803 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4804 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4805 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4806 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4807 | kobject_del(&mddev->kobj); |
| 4808 | kobject_put(&mddev->kobj); |
| 4809 | } |
| 4810 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4811 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4813 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4814 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4816 | int partitioned; |
| 4817 | int shift; |
| 4818 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4819 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | |
| 4821 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4822 | return -ENODEV; |
| 4823 | |
| 4824 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4825 | shift = partitioned ? MdpMinorShift : 0; |
| 4826 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4828 | /* wait for any previous instance of this device to be |
| 4829 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4830 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4831 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4832 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4833 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4834 | error = -EEXIST; |
| 4835 | if (mddev->gendisk) |
| 4836 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4837 | |
| 4838 | if (name) { |
| 4839 | /* Need to ensure that 'name' is not a duplicate. |
| 4840 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4841 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4842 | spin_lock(&all_mddevs_lock); |
| 4843 | |
| 4844 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4845 | if (mddev2->gendisk && |
| 4846 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4847 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4848 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4849 | } |
| 4850 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4852 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4853 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4854 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4855 | if (!mddev->queue) |
| 4856 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4857 | mddev->queue->queuedata = mddev; |
| 4858 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4859 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4860 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | disk = alloc_disk(1 << shift); |
| 4863 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4864 | blk_cleanup_queue(mddev->queue); |
| 4865 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4866 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4868 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4870 | if (name) |
| 4871 | strcpy(disk->disk_name, name); |
| 4872 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4874 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | disk->fops = &md_fops; |
| 4877 | disk->private_data = mddev; |
| 4878 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4879 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4880 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4881 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4882 | * remove it now. |
| 4883 | */ |
| 4884 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4886 | /* As soon as we call add_disk(), another thread could get |
| 4887 | * through to md_open, so make sure it doesn't get too far |
| 4888 | */ |
| 4889 | mutex_lock(&mddev->open_mutex); |
| 4890 | add_disk(disk); |
| 4891 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4892 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4893 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4894 | if (error) { |
| 4895 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4896 | * __must_check, we must do something with the result |
| 4897 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4898 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4899 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4900 | error = 0; |
| 4901 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4902 | if (mddev->kobj.sd && |
| 4903 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4904 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4905 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4906 | abort: |
| 4907 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4908 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4909 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4910 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4911 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4912 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4913 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4917 | { |
| 4918 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | return NULL; |
| 4920 | } |
| 4921 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4922 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4923 | { |
| 4924 | /* val must be "md_*" where * is not all digits. |
| 4925 | * We allocate an array with a large free minor number, and |
| 4926 | * set the name to val. val must not already be an active name. |
| 4927 | */ |
| 4928 | int len = strlen(val); |
| 4929 | char buf[DISK_NAME_LEN]; |
| 4930 | |
| 4931 | while (len && val[len-1] == '\n') |
| 4932 | len--; |
| 4933 | if (len >= DISK_NAME_LEN) |
| 4934 | return -E2BIG; |
| 4935 | strlcpy(buf, val, len+1); |
| 4936 | if (strncmp(buf, "md_", 3) != 0) |
| 4937 | return -EINVAL; |
| 4938 | return md_alloc(0, buf); |
| 4939 | } |
| 4940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | static void md_safemode_timeout(unsigned long data) |
| 4942 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4943 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4945 | if (!atomic_read(&mddev->writes_pending)) { |
| 4946 | mddev->safemode = 1; |
| 4947 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4948 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | md_wakeup_thread(mddev->thread); |
| 4951 | } |
| 4952 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4953 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4955 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4957 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4958 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4959 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4961 | if (list_empty(&mddev->disks)) |
| 4962 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | |
| 4965 | if (mddev->pers) |
| 4966 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4967 | /* Cannot run until previous stop completes properly */ |
| 4968 | if (mddev->sysfs_active) |
| 4969 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | /* |
| 4972 | * Analyze all RAID superblock(s) |
| 4973 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4974 | if (!mddev->raid_disks) { |
| 4975 | if (!mddev->persistent) |
| 4976 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4977 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4980 | if (mddev->level != LEVEL_NONE) |
| 4981 | request_module("md-level-%d", mddev->level); |
| 4982 | else if (mddev->clevel[0]) |
| 4983 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | |
| 4985 | /* |
| 4986 | * Drop all container device buffers, from now on |
| 4987 | * the only valid external interface is through the md |
| 4988 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4990 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4991 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | continue; |
| 4993 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4994 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4995 | |
| 4996 | /* perform some consistency tests on the device. |
| 4997 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4998 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4999 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5000 | if (rdev->meta_bdev) { |
| 5001 | /* Nothing to check */; |
| 5002 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5003 | if (mddev->dev_sectors && |
| 5004 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5005 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5006 | printk("md: %s: data overlaps metadata\n", |
| 5007 | mdname(mddev)); |
| 5008 | return -EINVAL; |
| 5009 | } |
| 5010 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5011 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5012 | > rdev->data_offset) { |
| 5013 | printk("md: %s: metadata overlaps data\n", |
| 5014 | mdname(mddev)); |
| 5015 | return -EINVAL; |
| 5016 | } |
| 5017 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5018 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5021 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5022 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5025 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5026 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5028 | if (mddev->level != LEVEL_NONE) |
| 5029 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5030 | mddev->level); |
| 5031 | else |
| 5032 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5033 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | return -EINVAL; |
| 5035 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5036 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5038 | if (mddev->level != pers->level) { |
| 5039 | mddev->level = pers->level; |
| 5040 | mddev->new_level = pers->level; |
| 5041 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5042 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5044 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5045 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5046 | /* This personality cannot handle reshaping... */ |
| 5047 | mddev->pers = NULL; |
| 5048 | module_put(pers->owner); |
| 5049 | return -EINVAL; |
| 5050 | } |
| 5051 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5052 | if (pers->sync_request) { |
| 5053 | /* Warn if this is a potentially silly |
| 5054 | * configuration. |
| 5055 | */ |
| 5056 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5057 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5058 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5059 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5060 | rdev_for_each(rdev, mddev) |
| 5061 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5062 | if (rdev < rdev2 && |
| 5063 | rdev->bdev->bd_contains == |
| 5064 | rdev2->bdev->bd_contains) { |
| 5065 | printk(KERN_WARNING |
| 5066 | "%s: WARNING: %s appears to be" |
| 5067 | " on the same physical disk as" |
| 5068 | " %s.\n", |
| 5069 | mdname(mddev), |
| 5070 | bdevname(rdev->bdev,b), |
| 5071 | bdevname(rdev2->bdev,b2)); |
| 5072 | warned = 1; |
| 5073 | } |
| 5074 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5075 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5076 | if (warned) |
| 5077 | printk(KERN_WARNING |
| 5078 | "True protection against single-disk" |
| 5079 | " failure might be compromised.\n"); |
| 5080 | } |
| 5081 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5082 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5083 | /* may be over-ridden by personality */ |
| 5084 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5085 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5086 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5088 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5089 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5090 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5091 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5092 | if (err) |
| 5093 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5094 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5095 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5096 | " but 'external_size' not in effect?\n", __func__); |
| 5097 | printk(KERN_ERR |
| 5098 | "md: invalid array_size %llu > default size %llu\n", |
| 5099 | (unsigned long long)mddev->array_sectors / 2, |
| 5100 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5101 | err = -EINVAL; |
| 5102 | mddev->pers->stop(mddev); |
| 5103 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5104 | if (err == 0 && mddev->pers->sync_request && |
| 5105 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5106 | err = bitmap_create(mddev); |
| 5107 | if (err) { |
| 5108 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5109 | mdname(mddev), err); |
| 5110 | mddev->pers->stop(mddev); |
| 5111 | } |
| 5112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | module_put(mddev->pers->owner); |
| 5115 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5116 | bitmap_destroy(mddev); |
| 5117 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5119 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5120 | if (mddev->kobj.sd && |
| 5121 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5122 | printk(KERN_WARNING |
| 5123 | "md: cannot register extra attributes for %s\n", |
| 5124 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5125 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5126 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5127 | mddev->ro = 0; |
| 5128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5130 | atomic_set(&mddev->max_corr_read_errors, |
| 5131 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | mddev->safemode = 0; |
| 5133 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5134 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5135 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5137 | smp_wmb(); |
| 5138 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5139 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5140 | if (rdev->raid_disk >= 0) |
| 5141 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5142 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | |
| 5144 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5145 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5146 | if (mddev->flags) |
| 5147 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5149 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5150 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5151 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5152 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | return 0; |
| 5154 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5155 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5157 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5158 | { |
| 5159 | int err; |
| 5160 | |
| 5161 | err = md_run(mddev); |
| 5162 | if (err) |
| 5163 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5164 | err = bitmap_load(mddev); |
| 5165 | if (err) { |
| 5166 | bitmap_destroy(mddev); |
| 5167 | goto out; |
| 5168 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5169 | |
| 5170 | md_wakeup_thread(mddev->thread); |
| 5171 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5172 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5173 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5174 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5175 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5176 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5177 | out: |
| 5178 | return err; |
| 5179 | } |
| 5180 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5181 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | { |
| 5183 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5185 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5187 | return -ENXIO; |
| 5188 | if (!mddev->pers) |
| 5189 | return -EINVAL; |
| 5190 | if (!mddev->ro) |
| 5191 | return -EBUSY; |
| 5192 | mddev->safemode = 0; |
| 5193 | mddev->ro = 0; |
| 5194 | set_disk_ro(disk, 0); |
| 5195 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5196 | mdname(mddev)); |
| 5197 | /* Kick recovery or resync if necessary */ |
| 5198 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5199 | md_wakeup_thread(mddev->thread); |
| 5200 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5201 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | } |
| 5204 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5205 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5206 | * to the file ourselves */ |
| 5207 | static int deny_bitmap_write_access(struct file * file) |
| 5208 | { |
| 5209 | struct inode *inode = file->f_mapping->host; |
| 5210 | |
| 5211 | spin_lock(&inode->i_lock); |
| 5212 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5213 | spin_unlock(&inode->i_lock); |
| 5214 | return -ETXTBSY; |
| 5215 | } |
| 5216 | atomic_set(&inode->i_writecount, -1); |
| 5217 | spin_unlock(&inode->i_lock); |
| 5218 | |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5222 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5223 | { |
| 5224 | struct inode *inode = file->f_mapping->host; |
| 5225 | |
| 5226 | spin_lock(&inode->i_lock); |
| 5227 | atomic_set(&inode->i_writecount, 1); |
| 5228 | spin_unlock(&inode->i_lock); |
| 5229 | } |
| 5230 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5231 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5232 | { |
| 5233 | mddev->array_sectors = 0; |
| 5234 | mddev->external_size = 0; |
| 5235 | mddev->dev_sectors = 0; |
| 5236 | mddev->raid_disks = 0; |
| 5237 | mddev->recovery_cp = 0; |
| 5238 | mddev->resync_min = 0; |
| 5239 | mddev->resync_max = MaxSector; |
| 5240 | mddev->reshape_position = MaxSector; |
| 5241 | mddev->external = 0; |
| 5242 | mddev->persistent = 0; |
| 5243 | mddev->level = LEVEL_NONE; |
| 5244 | mddev->clevel[0] = 0; |
| 5245 | mddev->flags = 0; |
| 5246 | mddev->ro = 0; |
| 5247 | mddev->metadata_type[0] = 0; |
| 5248 | mddev->chunk_sectors = 0; |
| 5249 | mddev->ctime = mddev->utime = 0; |
| 5250 | mddev->layout = 0; |
| 5251 | mddev->max_disks = 0; |
| 5252 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5253 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5254 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5255 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5256 | mddev->new_level = LEVEL_NONE; |
| 5257 | mddev->new_layout = 0; |
| 5258 | mddev->new_chunk_sectors = 0; |
| 5259 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5260 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5261 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5262 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5263 | mddev->recovery = 0; |
| 5264 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5265 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5266 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5267 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5268 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5269 | mddev->bitmap_info.offset = 0; |
| 5270 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5271 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5272 | mddev->bitmap_info.chunksize = 0; |
| 5273 | mddev->bitmap_info.daemon_sleep = 0; |
| 5274 | mddev->bitmap_info.max_write_behind = 0; |
| 5275 | } |
| 5276 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5277 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5278 | { |
| 5279 | if (mddev->sync_thread) { |
| 5280 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5281 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5282 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | del_timer_sync(&mddev->safemode_timer); |
| 5286 | |
| 5287 | bitmap_flush(mddev); |
| 5288 | md_super_wait(mddev); |
| 5289 | |
| 5290 | if (!mddev->in_sync || mddev->flags) { |
| 5291 | /* mark array as shutdown cleanly */ |
| 5292 | mddev->in_sync = 1; |
| 5293 | md_update_sb(mddev, 1); |
| 5294 | } |
| 5295 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5296 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5297 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5298 | { |
| 5299 | mddev_lock(mddev); |
| 5300 | __md_stop_writes(mddev); |
| 5301 | mddev_unlock(mddev); |
| 5302 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5303 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5304 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5305 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5306 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5307 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5308 | mddev->pers->stop(mddev); |
| 5309 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5310 | mddev->to_remove = &md_redundancy_group; |
| 5311 | module_put(mddev->pers->owner); |
| 5312 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5313 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5314 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5315 | |
| 5316 | void md_stop(struct mddev *mddev) |
| 5317 | { |
| 5318 | /* stop the array and free an attached data structures. |
| 5319 | * This is called from dm-raid |
| 5320 | */ |
| 5321 | __md_stop(mddev); |
| 5322 | bitmap_destroy(mddev); |
| 5323 | if (mddev->bio_set) |
| 5324 | bioset_free(mddev->bio_set); |
| 5325 | } |
| 5326 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5327 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5328 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5329 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5330 | { |
| 5331 | int err = 0; |
| 5332 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5333 | if (atomic_read(&mddev->openers) > !!bdev) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5334 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5335 | err = -EBUSY; |
| 5336 | goto out; |
| 5337 | } |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5338 | if (bdev) |
| 5339 | sync_blockdev(bdev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5340 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5341 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5342 | |
| 5343 | err = -ENXIO; |
| 5344 | if (mddev->ro==1) |
| 5345 | goto out; |
| 5346 | mddev->ro = 1; |
| 5347 | set_disk_ro(mddev->gendisk, 1); |
| 5348 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5349 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5350 | err = 0; |
| 5351 | } |
| 5352 | out: |
| 5353 | mutex_unlock(&mddev->open_mutex); |
| 5354 | return err; |
| 5355 | } |
| 5356 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5357 | /* mode: |
| 5358 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5359 | * 2 - stop but do not disassemble array |
| 5360 | */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5361 | static int do_md_stop(struct mddev * mddev, int mode, |
| 5362 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5365 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5367 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5368 | if (atomic_read(&mddev->openers) > !!bdev || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5369 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5370 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5371 | mutex_unlock(&mddev->open_mutex); |
| 5372 | return -EBUSY; |
| 5373 | } |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5374 | if (bdev) |
| 5375 | /* It is possible IO was issued on some other |
| 5376 | * open file which was closed before we took ->open_mutex. |
| 5377 | * As that was not the last close __blkdev_put will not |
| 5378 | * have called sync_blockdev, so we must. |
| 5379 | */ |
| 5380 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5382 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5383 | if (mddev->ro) |
| 5384 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5385 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5386 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5387 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5388 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5389 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5390 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5391 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5392 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5393 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5394 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5395 | if (rdev->raid_disk >= 0) |
| 5396 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5397 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5398 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5399 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5400 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5401 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5402 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5403 | if (mddev->ro) |
| 5404 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5405 | } else |
| 5406 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * Free resources if final stop |
| 5409 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5410 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5412 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5413 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5414 | if (mddev->bitmap_info.file) { |
| 5415 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5416 | fput(mddev->bitmap_info.file); |
| 5417 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5418 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5419 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | export_array(mddev); |
| 5422 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5423 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5424 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5425 | if (mddev->hold_active == UNTIL_STOP) |
| 5426 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5427 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5428 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5429 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5430 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5431 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | } |
| 5433 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5434 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5435 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5437 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | int err; |
| 5439 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5440 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | |
| 5443 | printk(KERN_INFO "md: running: "); |
| 5444 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5445 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | char b[BDEVNAME_SIZE]; |
| 5447 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5448 | } |
| 5449 | printk("\n"); |
| 5450 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5451 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | if (err) { |
| 5453 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5454 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | /* |
| 5459 | * lets try to run arrays based on all disks that have arrived |
| 5460 | * until now. (those are in pending_raid_disks) |
| 5461 | * |
| 5462 | * the method: pick the first pending disk, collect all disks with |
| 5463 | * the same UUID, remove all from the pending list and put them into |
| 5464 | * the 'same_array' list. Then order this list based on superblock |
| 5465 | * update time (freshest comes first), kick out 'old' disks and |
| 5466 | * compare superblocks. If everything's fine then run it. |
| 5467 | * |
| 5468 | * If "unit" is allocated, then bump its reference count |
| 5469 | */ |
| 5470 | static void autorun_devices(int part) |
| 5471 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5472 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5473 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | char b[BDEVNAME_SIZE]; |
| 5475 | |
| 5476 | printk(KERN_INFO "md: autorun ...\n"); |
| 5477 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5478 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5480 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5482 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | |
| 5484 | printk(KERN_INFO "md: considering %s ...\n", |
| 5485 | bdevname(rdev0->bdev,b)); |
| 5486 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5487 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5489 | printk(KERN_INFO "md: adding %s ...\n", |
| 5490 | bdevname(rdev->bdev,b)); |
| 5491 | list_move(&rdev->same_set, &candidates); |
| 5492 | } |
| 5493 | /* |
| 5494 | * now we have a set of devices, with all of them having |
| 5495 | * mostly sane superblocks. It's time to allocate the |
| 5496 | * mddev. |
| 5497 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5498 | if (part) { |
| 5499 | dev = MKDEV(mdp_major, |
| 5500 | rdev0->preferred_minor << MdpMinorShift); |
| 5501 | unit = MINOR(dev) >> MdpMinorShift; |
| 5502 | } else { |
| 5503 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5504 | unit = MINOR(dev); |
| 5505 | } |
| 5506 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5508 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5509 | break; |
| 5510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | |
| 5512 | md_probe(dev, NULL, NULL); |
| 5513 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5514 | if (!mddev || !mddev->gendisk) { |
| 5515 | if (mddev) |
| 5516 | mddev_put(mddev); |
| 5517 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | "md: cannot allocate memory for md drive.\n"); |
| 5519 | break; |
| 5520 | } |
| 5521 | if (mddev_lock(mddev)) |
| 5522 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5523 | mdname(mddev)); |
| 5524 | else if (mddev->raid_disks || mddev->major_version |
| 5525 | || !list_empty(&mddev->disks)) { |
| 5526 | printk(KERN_WARNING |
| 5527 | "md: %s already running, cannot run %s\n", |
| 5528 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5529 | mddev_unlock(mddev); |
| 5530 | } else { |
| 5531 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5532 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5533 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | list_del_init(&rdev->same_set); |
| 5535 | if (bind_rdev_to_array(rdev, mddev)) |
| 5536 | export_rdev(rdev); |
| 5537 | } |
| 5538 | autorun_array(mddev); |
| 5539 | mddev_unlock(mddev); |
| 5540 | } |
| 5541 | /* on success, candidates will be empty, on error |
| 5542 | * it won't... |
| 5543 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5544 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5545 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | mddev_put(mddev); |
| 5549 | } |
| 5550 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5551 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5552 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | static int get_version(void __user * arg) |
| 5555 | { |
| 5556 | mdu_version_t ver; |
| 5557 | |
| 5558 | ver.major = MD_MAJOR_VERSION; |
| 5559 | ver.minor = MD_MINOR_VERSION; |
| 5560 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5561 | |
| 5562 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5563 | return -EFAULT; |
| 5564 | |
| 5565 | return 0; |
| 5566 | } |
| 5567 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5568 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | { |
| 5570 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5571 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5572 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5574 | nr = working = insync = failed = spare = 0; |
| 5575 | rcu_read_lock(); |
| 5576 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5578 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | failed++; |
| 5580 | else { |
| 5581 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5582 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5583 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | else |
| 5585 | spare++; |
| 5586 | } |
| 5587 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5588 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | |
| 5590 | info.major_version = mddev->major_version; |
| 5591 | info.minor_version = mddev->minor_version; |
| 5592 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5593 | info.ctime = mddev->ctime; |
| 5594 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5595 | info.size = mddev->dev_sectors / 2; |
| 5596 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5597 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | info.nr_disks = nr; |
| 5599 | info.raid_disks = mddev->raid_disks; |
| 5600 | info.md_minor = mddev->md_minor; |
| 5601 | info.not_persistent= !mddev->persistent; |
| 5602 | |
| 5603 | info.utime = mddev->utime; |
| 5604 | info.state = 0; |
| 5605 | if (mddev->in_sync) |
| 5606 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5607 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5608 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5609 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | info.working_disks = working; |
| 5611 | info.failed_disks = failed; |
| 5612 | info.spare_disks = spare; |
| 5613 | |
| 5614 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5615 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | |
| 5617 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5618 | return -EFAULT; |
| 5619 | |
| 5620 | return 0; |
| 5621 | } |
| 5622 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5623 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5624 | { |
| 5625 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5626 | char *ptr, *buf = NULL; |
| 5627 | int err = -ENOMEM; |
| 5628 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5629 | if (md_allow_write(mddev)) |
| 5630 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5631 | else |
| 5632 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5633 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5634 | if (!file) |
| 5635 | goto out; |
| 5636 | |
| 5637 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5638 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5639 | file->pathname[0] = '\0'; |
| 5640 | goto copy_out; |
| 5641 | } |
| 5642 | |
| 5643 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5644 | if (!buf) |
| 5645 | goto out; |
| 5646 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5647 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5648 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5649 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5650 | goto out; |
| 5651 | |
| 5652 | strcpy(file->pathname, ptr); |
| 5653 | |
| 5654 | copy_out: |
| 5655 | err = 0; |
| 5656 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5657 | err = -EFAULT; |
| 5658 | out: |
| 5659 | kfree(buf); |
| 5660 | kfree(file); |
| 5661 | return err; |
| 5662 | } |
| 5663 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5664 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | { |
| 5666 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5667 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | |
| 5669 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5670 | return -EFAULT; |
| 5671 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5672 | rcu_read_lock(); |
| 5673 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | if (rdev) { |
| 5675 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5676 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5677 | info.raid_disk = rdev->raid_disk; |
| 5678 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5679 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5681 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5683 | info.state |= (1<<MD_DISK_SYNC); |
| 5684 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5685 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5686 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | } else { |
| 5688 | info.major = info.minor = 0; |
| 5689 | info.raid_disk = -1; |
| 5690 | info.state = (1<<MD_DISK_REMOVED); |
| 5691 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5692 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | |
| 5694 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5695 | return -EFAULT; |
| 5696 | |
| 5697 | return 0; |
| 5698 | } |
| 5699 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5700 | 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] | 5701 | { |
| 5702 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5703 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | dev_t dev = MKDEV(info->major,info->minor); |
| 5705 | |
| 5706 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5707 | return -EOVERFLOW; |
| 5708 | |
| 5709 | if (!mddev->raid_disks) { |
| 5710 | int err; |
| 5711 | /* expecting a device which has a superblock */ |
| 5712 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5713 | if (IS_ERR(rdev)) { |
| 5714 | printk(KERN_WARNING |
| 5715 | "md: md_import_device returned %ld\n", |
| 5716 | PTR_ERR(rdev)); |
| 5717 | return PTR_ERR(rdev); |
| 5718 | } |
| 5719 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5720 | struct md_rdev *rdev0 |
| 5721 | = list_entry(mddev->disks.next, |
| 5722 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5723 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5725 | if (err < 0) { |
| 5726 | printk(KERN_WARNING |
| 5727 | "md: %s has different UUID to %s\n", |
| 5728 | bdevname(rdev->bdev,b), |
| 5729 | bdevname(rdev0->bdev,b2)); |
| 5730 | export_rdev(rdev); |
| 5731 | return -EINVAL; |
| 5732 | } |
| 5733 | } |
| 5734 | err = bind_rdev_to_array(rdev, mddev); |
| 5735 | if (err) |
| 5736 | export_rdev(rdev); |
| 5737 | return err; |
| 5738 | } |
| 5739 | |
| 5740 | /* |
| 5741 | * add_new_disk can be used once the array is assembled |
| 5742 | * to add "hot spares". They must already have a superblock |
| 5743 | * written |
| 5744 | */ |
| 5745 | if (mddev->pers) { |
| 5746 | int err; |
| 5747 | if (!mddev->pers->hot_add_disk) { |
| 5748 | printk(KERN_WARNING |
| 5749 | "%s: personality does not support diskops!\n", |
| 5750 | mdname(mddev)); |
| 5751 | return -EINVAL; |
| 5752 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5753 | if (mddev->persistent) |
| 5754 | rdev = md_import_device(dev, mddev->major_version, |
| 5755 | mddev->minor_version); |
| 5756 | else |
| 5757 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | if (IS_ERR(rdev)) { |
| 5759 | printk(KERN_WARNING |
| 5760 | "md: md_import_device returned %ld\n", |
| 5761 | PTR_ERR(rdev)); |
| 5762 | return PTR_ERR(rdev); |
| 5763 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5764 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5765 | if (!mddev->persistent) { |
| 5766 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5767 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5768 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5769 | set_bit(In_sync, &rdev->flags); |
| 5770 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5771 | rdev->raid_disk = -1; |
| 5772 | } else |
| 5773 | super_types[mddev->major_version]. |
| 5774 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5775 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5776 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5777 | /* This was a hot-add request, but events doesn't |
| 5778 | * match, so reject it. |
| 5779 | */ |
| 5780 | export_rdev(rdev); |
| 5781 | return -EINVAL; |
| 5782 | } |
| 5783 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5784 | if (test_bit(In_sync, &rdev->flags)) |
| 5785 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5786 | else |
| 5787 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5788 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5789 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5790 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5791 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5792 | else |
| 5793 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | rdev->raid_disk = -1; |
| 5796 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5797 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5798 | /* If there is hot_add_disk but no hot_remove_disk |
| 5799 | * then added disks for geometry changes, |
| 5800 | * and should be added immediately. |
| 5801 | */ |
| 5802 | super_types[mddev->major_version]. |
| 5803 | validate_super(mddev, rdev); |
| 5804 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5805 | if (err) |
| 5806 | unbind_rdev_from_array(rdev); |
| 5807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | if (err) |
| 5809 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5810 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5811 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5812 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5813 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5814 | if (mddev->degraded) |
| 5815 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5816 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5817 | if (!err) |
| 5818 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5819 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | return err; |
| 5821 | } |
| 5822 | |
| 5823 | /* otherwise, add_new_disk is only allowed |
| 5824 | * for major_version==0 superblocks |
| 5825 | */ |
| 5826 | if (mddev->major_version != 0) { |
| 5827 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5828 | mdname(mddev)); |
| 5829 | return -EINVAL; |
| 5830 | } |
| 5831 | |
| 5832 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5833 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5834 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | if (IS_ERR(rdev)) { |
| 5836 | printk(KERN_WARNING |
| 5837 | "md: error, md_import_device() returned %ld\n", |
| 5838 | PTR_ERR(rdev)); |
| 5839 | return PTR_ERR(rdev); |
| 5840 | } |
| 5841 | rdev->desc_nr = info->number; |
| 5842 | if (info->raid_disk < mddev->raid_disks) |
| 5843 | rdev->raid_disk = info->raid_disk; |
| 5844 | else |
| 5845 | rdev->raid_disk = -1; |
| 5846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5848 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5849 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5851 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5852 | set_bit(WriteMostly, &rdev->flags); |
| 5853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | if (!mddev->persistent) { |
| 5855 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5856 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5857 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5858 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5859 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5861 | err = bind_rdev_to_array(rdev, mddev); |
| 5862 | if (err) { |
| 5863 | export_rdev(rdev); |
| 5864 | return err; |
| 5865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | return 0; |
| 5869 | } |
| 5870 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5871 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | { |
| 5873 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5874 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | rdev = find_rdev(mddev, dev); |
| 5877 | if (!rdev) |
| 5878 | return -ENXIO; |
| 5879 | |
| 5880 | if (rdev->raid_disk >= 0) |
| 5881 | goto busy; |
| 5882 | |
| 5883 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5884 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5885 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
| 5887 | return 0; |
| 5888 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5889 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5891 | return -EBUSY; |
| 5892 | } |
| 5893 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5894 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | { |
| 5896 | char b[BDEVNAME_SIZE]; |
| 5897 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5898 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | |
| 5900 | if (!mddev->pers) |
| 5901 | return -ENODEV; |
| 5902 | |
| 5903 | if (mddev->major_version != 0) { |
| 5904 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5905 | " version-0 superblocks.\n", |
| 5906 | mdname(mddev)); |
| 5907 | return -EINVAL; |
| 5908 | } |
| 5909 | if (!mddev->pers->hot_add_disk) { |
| 5910 | printk(KERN_WARNING |
| 5911 | "%s: personality does not support diskops!\n", |
| 5912 | mdname(mddev)); |
| 5913 | return -EINVAL; |
| 5914 | } |
| 5915 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5916 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | if (IS_ERR(rdev)) { |
| 5918 | printk(KERN_WARNING |
| 5919 | "md: error, md_import_device() returned %ld\n", |
| 5920 | PTR_ERR(rdev)); |
| 5921 | return -EINVAL; |
| 5922 | } |
| 5923 | |
| 5924 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5925 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5927 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5929 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5931 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | printk(KERN_WARNING |
| 5933 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5934 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5935 | err = -EINVAL; |
| 5936 | goto abort_export; |
| 5937 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5938 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5940 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5941 | err = bind_rdev_to_array(rdev, mddev); |
| 5942 | if (err) |
| 5943 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | |
| 5945 | /* |
| 5946 | * The rest should better be atomic, we can have disk failures |
| 5947 | * noticed in interrupt contexts ... |
| 5948 | */ |
| 5949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | rdev->raid_disk = -1; |
| 5951 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5952 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
| 5954 | /* |
| 5955 | * Kick recovery, maybe this spare has to be added to the |
| 5956 | * array immediately. |
| 5957 | */ |
| 5958 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5959 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5960 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | return 0; |
| 5962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | abort_export: |
| 5964 | export_rdev(rdev); |
| 5965 | return err; |
| 5966 | } |
| 5967 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5968 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5969 | { |
| 5970 | int err; |
| 5971 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5972 | if (mddev->pers) { |
| 5973 | if (!mddev->pers->quiesce) |
| 5974 | return -EBUSY; |
| 5975 | if (mddev->recovery || mddev->sync_thread) |
| 5976 | return -EBUSY; |
| 5977 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5978 | } |
| 5979 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5980 | |
| 5981 | if (fd >= 0) { |
| 5982 | if (mddev->bitmap) |
| 5983 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5984 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5985 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5986 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5987 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5988 | mdname(mddev)); |
| 5989 | return -EBADF; |
| 5990 | } |
| 5991 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5992 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5993 | if (err) { |
| 5994 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5995 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5996 | fput(mddev->bitmap_info.file); |
| 5997 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5998 | return err; |
| 5999 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6000 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6001 | } else if (mddev->bitmap == NULL) |
| 6002 | return -ENOENT; /* cannot remove what isn't there */ |
| 6003 | err = 0; |
| 6004 | if (mddev->pers) { |
| 6005 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6006 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6007 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6008 | if (!err) |
| 6009 | err = bitmap_load(mddev); |
| 6010 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6011 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6012 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6013 | fd = -1; /* make sure to put the file */ |
| 6014 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6015 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6016 | } |
| 6017 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6018 | if (mddev->bitmap_info.file) { |
| 6019 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 6020 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 6021 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6022 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6023 | } |
| 6024 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6025 | return err; |
| 6026 | } |
| 6027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | /* |
| 6029 | * set_array_info is used two different ways |
| 6030 | * The original usage is when creating a new array. |
| 6031 | * In this usage, raid_disks is > 0 and it together with |
| 6032 | * level, size, not_persistent,layout,chunksize determine the |
| 6033 | * shape of the array. |
| 6034 | * This will always create an array with a type-0.90.0 superblock. |
| 6035 | * The newer usage is when assembling an array. |
| 6036 | * In this case raid_disks will be 0, and the major_version field is |
| 6037 | * use to determine which style super-blocks are to be found on the devices. |
| 6038 | * The minor and patch _version numbers are also kept incase the |
| 6039 | * super_block handler wishes to interpret them. |
| 6040 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6041 | 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] | 6042 | { |
| 6043 | |
| 6044 | if (info->raid_disks == 0) { |
| 6045 | /* just setting version number for superblock loading */ |
| 6046 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6047 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | super_types[info->major_version].name == NULL) { |
| 6049 | /* maybe try to auto-load a module? */ |
| 6050 | printk(KERN_INFO |
| 6051 | "md: superblock version %d not known\n", |
| 6052 | info->major_version); |
| 6053 | return -EINVAL; |
| 6054 | } |
| 6055 | mddev->major_version = info->major_version; |
| 6056 | mddev->minor_version = info->minor_version; |
| 6057 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6058 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6059 | /* ensure mddev_put doesn't delete this now that there |
| 6060 | * is some minimal configuration. |
| 6061 | */ |
| 6062 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | return 0; |
| 6064 | } |
| 6065 | mddev->major_version = MD_MAJOR_VERSION; |
| 6066 | mddev->minor_version = MD_MINOR_VERSION; |
| 6067 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6068 | mddev->ctime = get_seconds(); |
| 6069 | |
| 6070 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6071 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6072 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | mddev->raid_disks = info->raid_disks; |
| 6074 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6075 | * openned |
| 6076 | */ |
| 6077 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6078 | mddev->recovery_cp = MaxSector; |
| 6079 | else |
| 6080 | mddev->recovery_cp = 0; |
| 6081 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6082 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | |
| 6084 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6085 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | |
| 6087 | mddev->max_disks = MD_SB_DISKS; |
| 6088 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6089 | if (mddev->persistent) |
| 6090 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6091 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6093 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6094 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6095 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6096 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6097 | mddev->reshape_position = MaxSector; |
| 6098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | /* |
| 6100 | * Generate a 128 bit UUID |
| 6101 | */ |
| 6102 | get_random_bytes(mddev->uuid, 16); |
| 6103 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6104 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6105 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6106 | mddev->new_layout = mddev->layout; |
| 6107 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6108 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | return 0; |
| 6111 | } |
| 6112 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6113 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6114 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6115 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6116 | |
| 6117 | if (mddev->external_size) |
| 6118 | return; |
| 6119 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6120 | mddev->array_sectors = array_sectors; |
| 6121 | } |
| 6122 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6123 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6124 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6125 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6126 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6127 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6128 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6129 | |
| 6130 | if (mddev->pers->resize == NULL) |
| 6131 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6132 | /* The "num_sectors" is the number of sectors of each device that |
| 6133 | * is used. This can only make sense for arrays with redundancy. |
| 6134 | * linear and raid0 always use whatever space is available. We can only |
| 6135 | * consider changing this number if no resync or reconstruction is |
| 6136 | * 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] | 6137 | * 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] | 6138 | * of each device. If num_sectors is zero, we find the largest size |
| 6139 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6140 | */ |
| 6141 | if (mddev->sync_thread) |
| 6142 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6143 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6144 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6145 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6146 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6147 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6148 | num_sectors = avail; |
| 6149 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6150 | return -ENOSPC; |
| 6151 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6152 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6153 | if (!rv) |
| 6154 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6155 | return rv; |
| 6156 | } |
| 6157 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6158 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6159 | { |
| 6160 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6161 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6162 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6163 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6164 | return -EINVAL; |
| 6165 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6166 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6167 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6168 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6169 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6170 | |
| 6171 | rdev_for_each(rdev, mddev) { |
| 6172 | if (mddev->raid_disks < raid_disks && |
| 6173 | rdev->data_offset < rdev->new_data_offset) |
| 6174 | return -EINVAL; |
| 6175 | if (mddev->raid_disks > raid_disks && |
| 6176 | rdev->data_offset > rdev->new_data_offset) |
| 6177 | return -EINVAL; |
| 6178 | } |
| 6179 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6180 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6181 | if (mddev->delta_disks < 0) |
| 6182 | mddev->reshape_backwards = 1; |
| 6183 | else if (mddev->delta_disks > 0) |
| 6184 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6185 | |
| 6186 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6187 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6188 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6189 | mddev->reshape_backwards = 0; |
| 6190 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6191 | return rv; |
| 6192 | } |
| 6193 | |
| 6194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | /* |
| 6196 | * update_array_info is used to change the configuration of an |
| 6197 | * on-line array. |
| 6198 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6199 | * fields in the info are checked against the array. |
| 6200 | * Any differences that cannot be handled will cause an error. |
| 6201 | * Normally, only one change can be managed at a time. |
| 6202 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6203 | 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] | 6204 | { |
| 6205 | int rv = 0; |
| 6206 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6207 | int state = 0; |
| 6208 | |
| 6209 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6210 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6211 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | |
| 6213 | if (mddev->major_version != info->major_version || |
| 6214 | mddev->minor_version != info->minor_version || |
| 6215 | /* mddev->patch_version != info->patch_version || */ |
| 6216 | mddev->ctime != info->ctime || |
| 6217 | mddev->level != info->level || |
| 6218 | /* mddev->layout != info->layout || */ |
| 6219 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6220 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6221 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6222 | ((state^info->state) & 0xfffffe00) |
| 6223 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | return -EINVAL; |
| 6225 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6226 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6227 | cnt++; |
| 6228 | if (mddev->raid_disks != info->raid_disks) |
| 6229 | cnt++; |
| 6230 | if (mddev->layout != info->layout) |
| 6231 | cnt++; |
| 6232 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6233 | cnt++; |
| 6234 | if (cnt == 0) |
| 6235 | return 0; |
| 6236 | if (cnt > 1) |
| 6237 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | |
| 6239 | if (mddev->layout != info->layout) { |
| 6240 | /* Change layout |
| 6241 | * we don't need to do anything at the md level, the |
| 6242 | * personality will take care of it all. |
| 6243 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6244 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6246 | else { |
| 6247 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6248 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6249 | if (rv) |
| 6250 | mddev->new_layout = mddev->layout; |
| 6251 | return rv; |
| 6252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6254 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6255 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6257 | if (mddev->raid_disks != info->raid_disks) |
| 6258 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6260 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6261 | if (mddev->pers->quiesce == NULL) |
| 6262 | return -EINVAL; |
| 6263 | if (mddev->recovery || mddev->sync_thread) |
| 6264 | return -EBUSY; |
| 6265 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6266 | /* add the bitmap */ |
| 6267 | if (mddev->bitmap) |
| 6268 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6269 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6270 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6271 | mddev->bitmap_info.offset = |
| 6272 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6273 | mddev->bitmap_info.space = |
| 6274 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6275 | mddev->pers->quiesce(mddev, 1); |
| 6276 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6277 | if (!rv) |
| 6278 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6279 | if (rv) |
| 6280 | bitmap_destroy(mddev); |
| 6281 | mddev->pers->quiesce(mddev, 0); |
| 6282 | } else { |
| 6283 | /* remove the bitmap */ |
| 6284 | if (!mddev->bitmap) |
| 6285 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6286 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6287 | return -EINVAL; |
| 6288 | mddev->pers->quiesce(mddev, 1); |
| 6289 | bitmap_destroy(mddev); |
| 6290 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6291 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6292 | } |
| 6293 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6294 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | return rv; |
| 6296 | } |
| 6297 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6298 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6300 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6301 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | |
| 6303 | if (mddev->pers == NULL) |
| 6304 | return -ENODEV; |
| 6305 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6306 | rcu_read_lock(); |
| 6307 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6309 | err = -ENODEV; |
| 6310 | else { |
| 6311 | md_error(mddev, rdev); |
| 6312 | if (!test_bit(Faulty, &rdev->flags)) |
| 6313 | err = -EBUSY; |
| 6314 | } |
| 6315 | rcu_read_unlock(); |
| 6316 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | } |
| 6318 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6319 | /* |
| 6320 | * We have a problem here : there is no easy way to give a CHS |
| 6321 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6322 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6323 | * dosfs just mad... ;-) |
| 6324 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6325 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6326 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6327 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6328 | |
| 6329 | geo->heads = 2; |
| 6330 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6331 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6332 | return 0; |
| 6333 | } |
| 6334 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6335 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | unsigned int cmd, unsigned long arg) |
| 6337 | { |
| 6338 | int err = 0; |
| 6339 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6340 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6341 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6343 | switch (cmd) { |
| 6344 | case RAID_VERSION: |
| 6345 | case GET_ARRAY_INFO: |
| 6346 | case GET_DISK_INFO: |
| 6347 | break; |
| 6348 | default: |
| 6349 | if (!capable(CAP_SYS_ADMIN)) |
| 6350 | return -EACCES; |
| 6351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | |
| 6353 | /* |
| 6354 | * Commands dealing with the RAID driver but not any |
| 6355 | * particular array: |
| 6356 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6357 | switch (cmd) { |
| 6358 | case RAID_VERSION: |
| 6359 | err = get_version(argp); |
| 6360 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6362 | case PRINT_RAID_DEBUG: |
| 6363 | err = 0; |
| 6364 | md_print_devices(); |
| 6365 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | |
| 6367 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6368 | case RAID_AUTORUN: |
| 6369 | err = 0; |
| 6370 | autostart_arrays(arg); |
| 6371 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6373 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | /* |
| 6377 | * Commands creating/starting a new array: |
| 6378 | */ |
| 6379 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6380 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | |
| 6382 | if (!mddev) { |
| 6383 | BUG(); |
| 6384 | goto abort; |
| 6385 | } |
| 6386 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6387 | /* Some actions do not requires the mutex */ |
| 6388 | switch (cmd) { |
| 6389 | case GET_ARRAY_INFO: |
| 6390 | if (!mddev->raid_disks && !mddev->external) |
| 6391 | err = -ENODEV; |
| 6392 | else |
| 6393 | err = get_array_info(mddev, argp); |
| 6394 | goto abort; |
| 6395 | |
| 6396 | case GET_DISK_INFO: |
| 6397 | if (!mddev->raid_disks && !mddev->external) |
| 6398 | err = -ENODEV; |
| 6399 | else |
| 6400 | err = get_disk_info(mddev, argp); |
| 6401 | goto abort; |
| 6402 | |
| 6403 | case SET_DISK_FAULTY: |
| 6404 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6405 | goto abort; |
| 6406 | } |
| 6407 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6408 | if (cmd == ADD_NEW_DISK) |
| 6409 | /* need to ensure md_delayed_delete() has completed */ |
| 6410 | flush_workqueue(md_misc_wq); |
| 6411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | err = mddev_lock(mddev); |
| 6413 | if (err) { |
| 6414 | printk(KERN_INFO |
| 6415 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6416 | err, cmd); |
| 6417 | goto abort; |
| 6418 | } |
| 6419 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6420 | if (cmd == SET_ARRAY_INFO) { |
| 6421 | mdu_array_info_t info; |
| 6422 | if (!arg) |
| 6423 | memset(&info, 0, sizeof(info)); |
| 6424 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6425 | err = -EFAULT; |
| 6426 | goto abort_unlock; |
| 6427 | } |
| 6428 | if (mddev->pers) { |
| 6429 | err = update_array_info(mddev, &info); |
| 6430 | if (err) { |
| 6431 | printk(KERN_WARNING "md: couldn't update" |
| 6432 | " array info. %d\n", err); |
| 6433 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | } |
| 6435 | goto done_unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6436 | } |
| 6437 | if (!list_empty(&mddev->disks)) { |
| 6438 | printk(KERN_WARNING |
| 6439 | "md: array %s already has disks!\n", |
| 6440 | mdname(mddev)); |
| 6441 | err = -EBUSY; |
| 6442 | goto abort_unlock; |
| 6443 | } |
| 6444 | if (mddev->raid_disks) { |
| 6445 | printk(KERN_WARNING |
| 6446 | "md: array %s already initialised!\n", |
| 6447 | mdname(mddev)); |
| 6448 | err = -EBUSY; |
| 6449 | goto abort_unlock; |
| 6450 | } |
| 6451 | err = set_array_info(mddev, &info); |
| 6452 | if (err) { |
| 6453 | printk(KERN_WARNING "md: couldn't set" |
| 6454 | " array info. %d\n", err); |
| 6455 | goto abort_unlock; |
| 6456 | } |
| 6457 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | } |
| 6459 | |
| 6460 | /* |
| 6461 | * Commands querying/configuring an existing array: |
| 6462 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6463 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6464 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6465 | if ((!mddev->raid_disks && !mddev->external) |
| 6466 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6467 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6468 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | err = -ENODEV; |
| 6470 | goto abort_unlock; |
| 6471 | } |
| 6472 | |
| 6473 | /* |
| 6474 | * Commands even a read-only array can execute: |
| 6475 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6476 | switch (cmd) { |
| 6477 | case GET_BITMAP_FILE: |
| 6478 | err = get_bitmap_file(mddev, argp); |
| 6479 | goto done_unlock; |
| 6480 | |
| 6481 | case RESTART_ARRAY_RW: |
| 6482 | err = restart_array(mddev); |
| 6483 | goto done_unlock; |
| 6484 | |
| 6485 | case STOP_ARRAY: |
| 6486 | err = do_md_stop(mddev, 0, bdev); |
| 6487 | goto done_unlock; |
| 6488 | |
| 6489 | case STOP_ARRAY_RO: |
| 6490 | err = md_set_readonly(mddev, bdev); |
| 6491 | goto done_unlock; |
| 6492 | |
| 6493 | case BLKROSET: |
| 6494 | if (get_user(ro, (int __user *)(arg))) { |
| 6495 | err = -EFAULT; |
| 6496 | goto done_unlock; |
| 6497 | } |
| 6498 | err = -EINVAL; |
| 6499 | |
| 6500 | /* if the bdev is going readonly the value of mddev->ro |
| 6501 | * does not matter, no writes are coming |
| 6502 | */ |
| 6503 | if (ro) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6504 | goto done_unlock; |
| 6505 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6506 | /* are we are already prepared for writes? */ |
| 6507 | if (mddev->ro != 1) |
| 6508 | goto done_unlock; |
| 6509 | |
| 6510 | /* transitioning to readauto need only happen for |
| 6511 | * arrays that call md_write_start |
| 6512 | */ |
| 6513 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6515 | if (err == 0) { |
| 6516 | mddev->ro = 2; |
| 6517 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6518 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6519 | } |
| 6520 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | } |
| 6522 | |
| 6523 | /* |
| 6524 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6525 | * superblock, so we do not allow them on read-only arrays. |
| 6526 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6527 | * here and hit the 'default' below, so only disallow |
| 6528 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6530 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6531 | if (mddev->ro == 2) { |
| 6532 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6533 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6534 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6535 | /* mddev_unlock will wake thread */ |
| 6536 | /* If a device failed while we were read-only, we |
| 6537 | * need to make sure the metadata is updated now. |
| 6538 | */ |
| 6539 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6540 | mddev_unlock(mddev); |
| 6541 | wait_event(mddev->sb_wait, |
| 6542 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6543 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
| 6544 | mddev_lock(mddev); |
| 6545 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6546 | } else { |
| 6547 | err = -EROFS; |
| 6548 | goto abort_unlock; |
| 6549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | } |
| 6551 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6552 | switch (cmd) { |
| 6553 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6555 | mdu_disk_info_t info; |
| 6556 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6557 | err = -EFAULT; |
| 6558 | else |
| 6559 | err = add_new_disk(mddev, &info); |
| 6560 | goto done_unlock; |
| 6561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6563 | case HOT_REMOVE_DISK: |
| 6564 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6565 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6567 | case HOT_ADD_DISK: |
| 6568 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6569 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6571 | case RUN_ARRAY: |
| 6572 | err = do_md_run(mddev); |
| 6573 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6575 | case SET_BITMAP_FILE: |
| 6576 | err = set_bitmap_file(mddev, (int)arg); |
| 6577 | goto done_unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6578 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6579 | default: |
| 6580 | err = -EINVAL; |
| 6581 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | done_unlock: |
| 6585 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6586 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6587 | err != -EINVAL) |
| 6588 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | mddev_unlock(mddev); |
| 6590 | |
| 6591 | return err; |
| 6592 | done: |
| 6593 | if (err) |
| 6594 | MD_BUG(); |
| 6595 | abort: |
| 6596 | return err; |
| 6597 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6598 | #ifdef CONFIG_COMPAT |
| 6599 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6600 | unsigned int cmd, unsigned long arg) |
| 6601 | { |
| 6602 | switch (cmd) { |
| 6603 | case HOT_REMOVE_DISK: |
| 6604 | case HOT_ADD_DISK: |
| 6605 | case SET_DISK_FAULTY: |
| 6606 | case SET_BITMAP_FILE: |
| 6607 | /* These take in integer arg, do not convert */ |
| 6608 | break; |
| 6609 | default: |
| 6610 | arg = (unsigned long)compat_ptr(arg); |
| 6611 | break; |
| 6612 | } |
| 6613 | |
| 6614 | return md_ioctl(bdev, mode, cmd, arg); |
| 6615 | } |
| 6616 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6617 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6618 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | { |
| 6620 | /* |
| 6621 | * Succeed if we can lock the mddev, which confirms that |
| 6622 | * it isn't being stopped right now. |
| 6623 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6624 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | int err; |
| 6626 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6627 | if (!mddev) |
| 6628 | return -ENODEV; |
| 6629 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6630 | if (mddev->gendisk != bdev->bd_disk) { |
| 6631 | /* we are racing with mddev_put which is discarding this |
| 6632 | * bd_disk. |
| 6633 | */ |
| 6634 | mddev_put(mddev); |
| 6635 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6636 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6637 | /* Then retry the open from the top */ |
| 6638 | return -ERESTARTSYS; |
| 6639 | } |
| 6640 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6641 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6642 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | goto out; |
| 6644 | |
| 6645 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6646 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6647 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6649 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | out: |
| 6651 | return err; |
| 6652 | } |
| 6653 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6654 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6656 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6658 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6659 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | mddev_put(mddev); |
| 6661 | |
| 6662 | return 0; |
| 6663 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6664 | |
| 6665 | static int md_media_changed(struct gendisk *disk) |
| 6666 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6667 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6668 | |
| 6669 | return mddev->changed; |
| 6670 | } |
| 6671 | |
| 6672 | static int md_revalidate(struct gendisk *disk) |
| 6673 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6674 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6675 | |
| 6676 | mddev->changed = 0; |
| 6677 | return 0; |
| 6678 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6679 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | { |
| 6681 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6682 | .open = md_open, |
| 6683 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6684 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6685 | #ifdef CONFIG_COMPAT |
| 6686 | .compat_ioctl = md_compat_ioctl, |
| 6687 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6688 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6689 | .media_changed = md_media_changed, |
| 6690 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | }; |
| 6692 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6693 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6694 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6695 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | /* |
| 6698 | * md_thread is a 'system-thread', it's priority should be very |
| 6699 | * high. We avoid resource deadlocks individually in each |
| 6700 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6701 | * the very same RT priority as kswapd, thus we will never get |
| 6702 | * into a priority inversion deadlock. |
| 6703 | * |
| 6704 | * we definitely have to have equal or higher priority than |
| 6705 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6706 | * many dirty RAID5 blocks. |
| 6707 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6709 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6710 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6712 | /* We need to wait INTERRUPTIBLE so that |
| 6713 | * we don't add to the load-average. |
| 6714 | * That means we need to be sure no signals are |
| 6715 | * pending |
| 6716 | */ |
| 6717 | if (signal_pending(current)) |
| 6718 | flush_signals(current); |
| 6719 | |
| 6720 | wait_event_interruptible_timeout |
| 6721 | (thread->wqueue, |
| 6722 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6723 | || kthread_should_stop(), |
| 6724 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6726 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6727 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6728 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | return 0; |
| 6732 | } |
| 6733 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6734 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | { |
| 6736 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6737 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6739 | wake_up(&thread->wqueue); |
| 6740 | } |
| 6741 | } |
| 6742 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6743 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6744 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6745 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6746 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6748 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | if (!thread) |
| 6750 | return NULL; |
| 6751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | init_waitqueue_head(&thread->wqueue); |
| 6753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | thread->run = run; |
| 6755 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6756 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6757 | thread->tsk = kthread_run(md_thread, thread, |
| 6758 | "%s_%s", |
| 6759 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6760 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6761 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | kfree(thread); |
| 6763 | return NULL; |
| 6764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | return thread; |
| 6766 | } |
| 6767 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6768 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6770 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6771 | if (!thread) |
| 6772 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6773 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6774 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6775 | * non-existent thread |
| 6776 | */ |
| 6777 | spin_lock(&pers_lock); |
| 6778 | *threadp = NULL; |
| 6779 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6780 | |
| 6781 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | kfree(thread); |
| 6783 | } |
| 6784 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6785 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | { |
| 6787 | if (!mddev) { |
| 6788 | MD_BUG(); |
| 6789 | return; |
| 6790 | } |
| 6791 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6792 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6794 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6795 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | return; |
| 6797 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6798 | if (mddev->degraded) |
| 6799 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6800 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6802 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6803 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6804 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6805 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6806 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | } |
| 6808 | |
| 6809 | /* seq_file implementation /proc/mdstat */ |
| 6810 | |
| 6811 | static void status_unused(struct seq_file *seq) |
| 6812 | { |
| 6813 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6814 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | |
| 6816 | seq_printf(seq, "unused devices: "); |
| 6817 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6818 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | char b[BDEVNAME_SIZE]; |
| 6820 | i++; |
| 6821 | seq_printf(seq, "%s ", |
| 6822 | bdevname(rdev->bdev,b)); |
| 6823 | } |
| 6824 | if (!i) |
| 6825 | seq_printf(seq, "<none>"); |
| 6826 | |
| 6827 | seq_printf(seq, "\n"); |
| 6828 | } |
| 6829 | |
| 6830 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6831 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6833 | sector_t max_sectors, resync, res; |
| 6834 | unsigned long dt, db; |
| 6835 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6836 | int scale; |
| 6837 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6839 | if (mddev->curr_resync <= 3) |
| 6840 | resync = 0; |
| 6841 | else |
| 6842 | resync = mddev->curr_resync |
| 6843 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6845 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6846 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6847 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6849 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | |
| 6851 | /* |
| 6852 | * Should not happen. |
| 6853 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6854 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | MD_BUG(); |
| 6856 | return; |
| 6857 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6858 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6859 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6860 | * u32, as those are the requirements for sector_div. |
| 6861 | * Thus 'scale' must be at least 10 |
| 6862 | */ |
| 6863 | scale = 10; |
| 6864 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6865 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6866 | scale++; |
| 6867 | } |
| 6868 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6869 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6870 | |
| 6871 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6873 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | seq_printf(seq, "["); |
| 6875 | for (i = 0; i < x; i++) |
| 6876 | seq_printf(seq, "="); |
| 6877 | seq_printf(seq, ">"); |
| 6878 | for (i = 0; i < y; i++) |
| 6879 | seq_printf(seq, "."); |
| 6880 | seq_printf(seq, "] "); |
| 6881 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6882 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6883 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6884 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6885 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6886 | "check" : |
| 6887 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6888 | "resync" : "recovery"))), |
| 6889 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6890 | (unsigned long long) resync/2, |
| 6891 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | |
| 6893 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | * dt: time from mark until now |
| 6895 | * db: blocks written from mark until now |
| 6896 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6897 | * |
| 6898 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6899 | * So we divide before multiply in case it is 32bit and close |
| 6900 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6901 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6902 | * near the end of resync when the number of remaining sectors |
| 6903 | * is close to 'db'. |
| 6904 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6905 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | */ |
| 6907 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6908 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6909 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6910 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6912 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6913 | sector_div(rt, db/32+1); |
| 6914 | rt *= dt; |
| 6915 | rt >>= 5; |
| 6916 | |
| 6917 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6918 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6919 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6920 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6924 | { |
| 6925 | struct list_head *tmp; |
| 6926 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6927 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | |
| 6929 | if (l >= 0x10000) |
| 6930 | return NULL; |
| 6931 | if (!l--) |
| 6932 | /* header */ |
| 6933 | return (void*)1; |
| 6934 | |
| 6935 | spin_lock(&all_mddevs_lock); |
| 6936 | list_for_each(tmp,&all_mddevs) |
| 6937 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6938 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | mddev_get(mddev); |
| 6940 | spin_unlock(&all_mddevs_lock); |
| 6941 | return mddev; |
| 6942 | } |
| 6943 | spin_unlock(&all_mddevs_lock); |
| 6944 | if (!l--) |
| 6945 | return (void*)2;/* tail */ |
| 6946 | return NULL; |
| 6947 | } |
| 6948 | |
| 6949 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6950 | { |
| 6951 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6952 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | |
| 6954 | ++*pos; |
| 6955 | if (v == (void*)2) |
| 6956 | return NULL; |
| 6957 | |
| 6958 | spin_lock(&all_mddevs_lock); |
| 6959 | if (v == (void*)1) |
| 6960 | tmp = all_mddevs.next; |
| 6961 | else |
| 6962 | tmp = mddev->all_mddevs.next; |
| 6963 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6964 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | else { |
| 6966 | next_mddev = (void*)2; |
| 6967 | *pos = 0x10000; |
| 6968 | } |
| 6969 | spin_unlock(&all_mddevs_lock); |
| 6970 | |
| 6971 | if (v != (void*)1) |
| 6972 | mddev_put(mddev); |
| 6973 | return next_mddev; |
| 6974 | |
| 6975 | } |
| 6976 | |
| 6977 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6978 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6979 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | |
| 6981 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6982 | mddev_put(mddev); |
| 6983 | } |
| 6984 | |
| 6985 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6986 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6987 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6988 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6989 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | |
| 6991 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6992 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | seq_printf(seq, "Personalities : "); |
| 6994 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6995 | list_for_each_entry(pers, &pers_list, list) |
| 6996 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | |
| 6998 | spin_unlock(&pers_lock); |
| 6999 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7000 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | return 0; |
| 7002 | } |
| 7003 | if (v == (void*)2) { |
| 7004 | status_unused(seq); |
| 7005 | return 0; |
| 7006 | } |
| 7007 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7008 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7011 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7012 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7013 | mddev->pers ? "" : "in"); |
| 7014 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7015 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7017 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7018 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | seq_printf(seq, " %s", mddev->pers->name); |
| 7020 | } |
| 7021 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7022 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7023 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | char b[BDEVNAME_SIZE]; |
| 7025 | seq_printf(seq, " %s[%d]", |
| 7026 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7027 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7028 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7029 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | seq_printf(seq, "(F)"); |
| 7031 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7032 | } |
| 7033 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7034 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7035 | if (test_bit(Replacement, &rdev->flags)) |
| 7036 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7037 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | } |
| 7039 | |
| 7040 | if (!list_empty(&mddev->disks)) { |
| 7041 | if (mddev->pers) |
| 7042 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7043 | (unsigned long long) |
| 7044 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | else |
| 7046 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7047 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7049 | if (mddev->persistent) { |
| 7050 | if (mddev->major_version != 0 || |
| 7051 | mddev->minor_version != 90) { |
| 7052 | seq_printf(seq," super %d.%d", |
| 7053 | mddev->major_version, |
| 7054 | mddev->minor_version); |
| 7055 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7056 | } else if (mddev->external) |
| 7057 | seq_printf(seq, " super external:%s", |
| 7058 | mddev->metadata_type); |
| 7059 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7060 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | |
| 7062 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7063 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7065 | if (mddev->pers->sync_request) { |
| 7066 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7067 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7068 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7069 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7070 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7071 | else if (mddev->recovery_cp < MaxSector) |
| 7072 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7073 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7074 | } else |
| 7075 | seq_printf(seq, "\n "); |
| 7076 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7077 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | |
| 7079 | seq_printf(seq, "\n"); |
| 7080 | } |
| 7081 | mddev_unlock(mddev); |
| 7082 | |
| 7083 | return 0; |
| 7084 | } |
| 7085 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7086 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | .start = md_seq_start, |
| 7088 | .next = md_seq_next, |
| 7089 | .stop = md_seq_stop, |
| 7090 | .show = md_seq_show, |
| 7091 | }; |
| 7092 | |
| 7093 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7094 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7095 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | int error; |
| 7097 | |
| 7098 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7099 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7100 | return error; |
| 7101 | |
| 7102 | seq = file->private_data; |
| 7103 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | return error; |
| 7105 | } |
| 7106 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7107 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7108 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7109 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7110 | int mask; |
| 7111 | |
| 7112 | poll_wait(filp, &md_event_waiters, wait); |
| 7113 | |
| 7114 | /* always allow read */ |
| 7115 | mask = POLLIN | POLLRDNORM; |
| 7116 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7117 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7118 | mask |= POLLERR | POLLPRI; |
| 7119 | return mask; |
| 7120 | } |
| 7121 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7122 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7123 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | .open = md_seq_open, |
| 7125 | .read = seq_read, |
| 7126 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7127 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7128 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | }; |
| 7130 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7131 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7134 | list_add_tail(&p->list, &pers_list); |
| 7135 | 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] | 7136 | spin_unlock(&pers_lock); |
| 7137 | return 0; |
| 7138 | } |
| 7139 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7140 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7142 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7144 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | spin_unlock(&pers_lock); |
| 7146 | return 0; |
| 7147 | } |
| 7148 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7149 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7151 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7153 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | |
| 7155 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7156 | rcu_read_lock(); |
| 7157 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7159 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7160 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7161 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7162 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7163 | * as sync_io is counted when a request starts, and |
| 7164 | * disk_stats is counted when it completes. |
| 7165 | * So resync activity will cause curr_events to be smaller than |
| 7166 | * when there was no such activity. |
| 7167 | * non-sync IO will cause disk_stat to increase without |
| 7168 | * increasing sync_io so curr_events will (eventually) |
| 7169 | * be larger than it was before. Once it becomes |
| 7170 | * substantially larger, the test below will cause |
| 7171 | * the array to appear non-idle, and resync will slow |
| 7172 | * down. |
| 7173 | * If there is a lot of outstanding resync activity when |
| 7174 | * we set last_event to curr_events, then all that activity |
| 7175 | * completing might cause the array to appear non-idle |
| 7176 | * and resync will be slowed down even though there might |
| 7177 | * not have been non-resync activity. This will only |
| 7178 | * happen once though. 'last_events' will soon reflect |
| 7179 | * the state where there is little or no outstanding |
| 7180 | * resync requests, and further resync activity will |
| 7181 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7182 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7184 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | rdev->last_events = curr_events; |
| 7186 | idle = 0; |
| 7187 | } |
| 7188 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7189 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | return idle; |
| 7191 | } |
| 7192 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7193 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | { |
| 7195 | /* another "blocks" (512byte) blocks have been synced */ |
| 7196 | atomic_sub(blocks, &mddev->recovery_active); |
| 7197 | wake_up(&mddev->recovery_wait); |
| 7198 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7199 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7200 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | md_wakeup_thread(mddev->thread); |
| 7202 | // stop recovery, signal do_sync .... |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7207 | /* md_write_start(mddev, bi) |
| 7208 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7209 | * in superblock) before writing, schedule a superblock update |
| 7210 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7211 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7212 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7214 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7215 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7216 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7217 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7218 | BUG_ON(mddev->ro == 1); |
| 7219 | if (mddev->ro == 2) { |
| 7220 | /* need to switch to read/write */ |
| 7221 | mddev->ro = 0; |
| 7222 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7223 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7224 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7225 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7226 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7227 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7228 | if (mddev->safemode == 1) |
| 7229 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7230 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7231 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7232 | if (mddev->in_sync) { |
| 7233 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7234 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7235 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7236 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7237 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7238 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7239 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7240 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7241 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7242 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7243 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7244 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | } |
| 7246 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7247 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | { |
| 7249 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7250 | if (mddev->safemode == 2) |
| 7251 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7252 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7254 | } |
| 7255 | } |
| 7256 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7257 | /* md_allow_write(mddev) |
| 7258 | * Calling this ensures that the array is marked 'active' so that writes |
| 7259 | * may proceed without blocking. It is important to call this before |
| 7260 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7261 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7262 | * |
| 7263 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7264 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7265 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7266 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7267 | { |
| 7268 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7269 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7270 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7271 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7272 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7273 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7274 | |
| 7275 | spin_lock_irq(&mddev->write_lock); |
| 7276 | if (mddev->in_sync) { |
| 7277 | mddev->in_sync = 0; |
| 7278 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7279 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7280 | if (mddev->safemode_delay && |
| 7281 | mddev->safemode == 0) |
| 7282 | mddev->safemode = 1; |
| 7283 | spin_unlock_irq(&mddev->write_lock); |
| 7284 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7285 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7286 | } else |
| 7287 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7288 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7289 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7290 | return -EAGAIN; |
| 7291 | else |
| 7292 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7293 | } |
| 7294 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | #define SYNC_MARKS 10 |
| 7297 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7298 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7299 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7301 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7302 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | unsigned int currspeed = 0, |
| 7304 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7305 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7307 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | sector_t mark_cnt[SYNC_MARKS]; |
| 7309 | int last_mark,m; |
| 7310 | struct list_head *tmp; |
| 7311 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7312 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7313 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7314 | char *desc; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7315 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | |
| 7317 | /* just incase thread restarts... */ |
| 7318 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7319 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7320 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7321 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7323 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7324 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7325 | desc = "data-check"; |
| 7326 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7327 | desc = "requested-resync"; |
| 7328 | else |
| 7329 | desc = "resync"; |
| 7330 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7331 | desc = "reshape"; |
| 7332 | else |
| 7333 | desc = "recovery"; |
| 7334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | /* we overload curr_resync somewhat here. |
| 7336 | * 0 == not engaged in resync at all |
| 7337 | * 2 == checking that there is no conflict with another sync |
| 7338 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7339 | * commense |
| 7340 | * other == active in resync - this many blocks |
| 7341 | * |
| 7342 | * Before starting a resync we must have set curr_resync to |
| 7343 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7344 | * less than ours. When we find one that is the same or higher |
| 7345 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7346 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7347 | * This will mean we have to start checking from the beginning again. |
| 7348 | * |
| 7349 | */ |
| 7350 | |
| 7351 | do { |
| 7352 | mddev->curr_resync = 2; |
| 7353 | |
| 7354 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7355 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7356 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7357 | |
| 7358 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7360 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | if (mddev2 == mddev) |
| 7362 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7363 | if (!mddev->parallel_resync |
| 7364 | && mddev2->curr_resync |
| 7365 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7366 | DEFINE_WAIT(wq); |
| 7367 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7368 | /* arbitrarily yield */ |
| 7369 | mddev->curr_resync = 1; |
| 7370 | wake_up(&resync_wait); |
| 7371 | } |
| 7372 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7373 | /* no need to wait here, we can wait the next |
| 7374 | * time 'round when curr_resync == 2 |
| 7375 | */ |
| 7376 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7377 | /* We need to wait 'interruptible' so as not to |
| 7378 | * contribute to the load average, and not to |
| 7379 | * be caught by 'softlockup' |
| 7380 | */ |
| 7381 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7382 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7383 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7384 | printk(KERN_INFO "md: delaying %s of %s" |
| 7385 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7387 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7388 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7389 | if (signal_pending(current)) |
| 7390 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | schedule(); |
| 7392 | finish_wait(&resync_wait, &wq); |
| 7393 | goto try_again; |
| 7394 | } |
| 7395 | finish_wait(&resync_wait, &wq); |
| 7396 | } |
| 7397 | } |
| 7398 | } while (mddev->curr_resync < 2); |
| 7399 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7400 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7401 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7403 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7404 | */ |
| 7405 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7406 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7407 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7408 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7409 | j = mddev->resync_min; |
| 7410 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7411 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7412 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7413 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7414 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7415 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7416 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7417 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7418 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7419 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7420 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7421 | if (rdev->raid_disk >= 0 && |
| 7422 | !test_bit(Faulty, &rdev->flags) && |
| 7423 | !test_bit(In_sync, &rdev->flags) && |
| 7424 | rdev->recovery_offset < j) |
| 7425 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7426 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7429 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7430 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7431 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7432 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7433 | "(but not more than %d KB/sec) for %s.\n", |
| 7434 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7436 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7437 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7438 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7440 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7441 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7442 | } |
| 7443 | last_mark = 0; |
| 7444 | mddev->resync_mark = mark[last_mark]; |
| 7445 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7446 | |
| 7447 | /* |
| 7448 | * Tune reconstruction: |
| 7449 | */ |
| 7450 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7451 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7452 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | |
| 7454 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | last_check = 0; |
| 7456 | |
| 7457 | if (j>2) { |
| 7458 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7459 | "md: resuming %s of %s from checkpoint.\n", |
| 7460 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7461 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7462 | } else |
| 7463 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7464 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7465 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7466 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7467 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7469 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7471 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7473 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7474 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7475 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7476 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7477 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7478 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7479 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7480 | (j - mddev->curr_resync_completed)*2 |
| 7481 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7482 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7483 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7484 | wait_event(mddev->recovery_wait, |
| 7485 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7486 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7487 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7488 | j > mddev->recovery_cp) |
| 7489 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7490 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7491 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7492 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7493 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7494 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7495 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7496 | /* As this condition is controlled by user-space, |
| 7497 | * we can block indefinitely, so use '_interruptible' |
| 7498 | * to avoid triggering warnings. |
| 7499 | */ |
| 7500 | flush_signals(current); /* just in case */ |
| 7501 | wait_event_interruptible(mddev->recovery_wait, |
| 7502 | mddev->resync_max > j |
| 7503 | || kthread_should_stop()); |
| 7504 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7505 | |
| 7506 | if (kthread_should_stop()) |
| 7507 | goto interrupted; |
| 7508 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7509 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7510 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7511 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7512 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | goto out; |
| 7514 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7515 | |
| 7516 | if (!skipped) { /* actual IO requested */ |
| 7517 | io_sectors += sectors; |
| 7518 | atomic_add(sectors, &mddev->recovery_active); |
| 7519 | } |
| 7520 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7521 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7522 | break; |
| 7523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7524 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7525 | if (j > 2) |
| 7526 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7527 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7528 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7529 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7530 | * visible in /proc/mdstat |
| 7531 | */ |
| 7532 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7533 | |
| 7534 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7535 | continue; |
| 7536 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7537 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | repeat: |
| 7539 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7540 | /* step marks */ |
| 7541 | int next = (last_mark+1) % SYNC_MARKS; |
| 7542 | |
| 7543 | mddev->resync_mark = mark[next]; |
| 7544 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7545 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7546 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | last_mark = next; |
| 7548 | } |
| 7549 | |
| 7550 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7551 | if (kthread_should_stop()) |
| 7552 | goto interrupted; |
| 7553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7554 | |
| 7555 | /* |
| 7556 | * this loop exits only if either when we are slower than |
| 7557 | * the 'hard' speed limit, or the system was IO-idle for |
| 7558 | * a jiffy. |
| 7559 | * the system might be non-idle CPU-wise, but we only care |
| 7560 | * about not overloading the IO subsystem. (things like an |
| 7561 | * e2fsck being done on the RAID array should execute fast) |
| 7562 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | cond_resched(); |
| 7564 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7565 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7566 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7567 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7568 | if (currspeed > speed_min(mddev)) { |
| 7569 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7570 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7571 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | goto repeat; |
| 7573 | } |
| 7574 | } |
| 7575 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7576 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7577 | /* |
| 7578 | * this also signals 'finished resyncing' to md_stop |
| 7579 | */ |
| 7580 | out: |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7581 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7583 | |
| 7584 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7585 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7587 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7588 | mddev->curr_resync > 2) { |
| 7589 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7590 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7591 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7592 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7593 | "md: checkpointing %s of %s.\n", |
| 7594 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7595 | if (test_bit(MD_RECOVERY_ERROR, |
| 7596 | &mddev->recovery)) |
| 7597 | mddev->recovery_cp = |
| 7598 | mddev->curr_resync_completed; |
| 7599 | else |
| 7600 | mddev->recovery_cp = |
| 7601 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7602 | } |
| 7603 | } else |
| 7604 | mddev->recovery_cp = MaxSector; |
| 7605 | } else { |
| 7606 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7607 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7608 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7609 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7610 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7611 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7612 | !test_bit(Faulty, &rdev->flags) && |
| 7613 | !test_bit(In_sync, &rdev->flags) && |
| 7614 | rdev->recovery_offset < mddev->curr_resync) |
| 7615 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7616 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7619 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7620 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7621 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7622 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7623 | /* We completed so min/max setting can be forgotten if used. */ |
| 7624 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7625 | mddev->resync_min = 0; |
| 7626 | mddev->resync_max = MaxSector; |
| 7627 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7628 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | mddev->curr_resync = 0; |
| 7630 | wake_up(&resync_wait); |
| 7631 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7632 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7633 | return; |
| 7634 | |
| 7635 | interrupted: |
| 7636 | /* |
| 7637 | * got a signal, exit. |
| 7638 | */ |
| 7639 | printk(KERN_INFO |
| 7640 | "md: md_do_sync() got signal ... exiting\n"); |
| 7641 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7642 | goto out; |
| 7643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7644 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7645 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7646 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7647 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7648 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7649 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7650 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7651 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7652 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7653 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7654 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7655 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7656 | (test_bit(Faulty, &rdev->flags) || |
| 7657 | ! test_bit(In_sync, &rdev->flags)) && |
| 7658 | atomic_read(&rdev->nr_pending)==0) { |
| 7659 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7660 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7661 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7662 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7663 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7664 | } |
| 7665 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7666 | if (removed && mddev->kobj.sd) |
| 7667 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7668 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7669 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7670 | if (rdev->raid_disk >= 0 && |
| 7671 | !test_bit(In_sync, &rdev->flags) && |
| 7672 | !test_bit(Faulty, &rdev->flags)) |
| 7673 | spares++; |
| 7674 | if (rdev->raid_disk < 0 |
| 7675 | && !test_bit(Faulty, &rdev->flags)) { |
| 7676 | rdev->recovery_offset = 0; |
| 7677 | if (mddev->pers-> |
| 7678 | hot_add_disk(mddev, rdev) == 0) { |
| 7679 | if (sysfs_link_rdev(mddev, rdev)) |
| 7680 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7681 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7682 | md_new_event(mddev); |
| 7683 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7684 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7685 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7686 | } |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7687 | if (removed) |
| 7688 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7689 | return spares; |
| 7690 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7691 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7692 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7693 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7694 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7695 | |
| 7696 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7697 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7698 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7699 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7700 | /* success...*/ |
| 7701 | /* activate any spares */ |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7702 | if (mddev->pers->spare_active(mddev)) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7703 | sysfs_notify(&mddev->kobj, NULL, |
| 7704 | "degraded"); |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7705 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7706 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7707 | } |
| 7708 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7709 | mddev->pers->finish_reshape) |
| 7710 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7711 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7712 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7713 | * information must be scrapped. Also if any device is now |
| 7714 | * In_sync we must scrape the saved_raid_disk for that device |
| 7715 | * do the superblock for an incrementally recovered device |
| 7716 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7717 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7718 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7719 | if (!mddev->degraded || |
| 7720 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7721 | rdev->saved_raid_disk = -1; |
| 7722 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7723 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7724 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7725 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7726 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7727 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7728 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7729 | /* flag recovery needed just to double check */ |
| 7730 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7731 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7732 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7733 | if (mddev->event_work.func) |
| 7734 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7735 | } |
| 7736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | /* |
| 7738 | * This routine is regularly called by all per-raid-array threads to |
| 7739 | * deal with generic issues like resync and super-block update. |
| 7740 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7741 | * need this as they never do any recovery or update the superblock. |
| 7742 | * |
| 7743 | * It does not do any resync itself, but rather "forks" off other threads |
| 7744 | * to do that as needed. |
| 7745 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7746 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7747 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | * and wakeups up this thread which will reap the thread and finish up. |
| 7749 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7750 | * |
| 7751 | * The overall approach is: |
| 7752 | * 1/ if the superblock needs updating, update it. |
| 7753 | * 2/ If a recovery thread is running, don't do anything else. |
| 7754 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7755 | * 4/ If there are any faulty devices, remove them. |
| 7756 | * 5/ If array is degraded, try to add spares devices |
| 7757 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7758 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7759 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7760 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7761 | if (mddev->suspended) |
| 7762 | return; |
| 7763 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7764 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7765 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7766 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7767 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7768 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7769 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7770 | mdname(mddev)); |
| 7771 | mddev->safemode = 2; |
| 7772 | } |
| 7773 | flush_signals(current); |
| 7774 | } |
| 7775 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7776 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7777 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7779 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7780 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7781 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7782 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7783 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7784 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7785 | )) |
| 7786 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7787 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7788 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7789 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7790 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7791 | if (mddev->ro) { |
| 7792 | /* Only thing we do on a ro array is remove |
| 7793 | * failed devices. |
| 7794 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7795 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7796 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7797 | if (rdev->raid_disk >= 0 && |
| 7798 | !test_bit(Blocked, &rdev->flags) && |
| 7799 | test_bit(Faulty, &rdev->flags) && |
| 7800 | atomic_read(&rdev->nr_pending)==0) { |
| 7801 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7802 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7803 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7804 | rdev->raid_disk = -1; |
| 7805 | } |
| 7806 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7807 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7808 | goto unlock; |
| 7809 | } |
| 7810 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7811 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7812 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7813 | spin_lock_irq(&mddev->write_lock); |
| 7814 | if (mddev->safemode && |
| 7815 | !atomic_read(&mddev->writes_pending) && |
| 7816 | !mddev->in_sync && |
| 7817 | mddev->recovery_cp == MaxSector) { |
| 7818 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7819 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7820 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7821 | } |
| 7822 | if (mddev->safemode == 1) |
| 7823 | mddev->safemode = 0; |
| 7824 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7825 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7826 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7827 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7828 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7829 | if (mddev->flags) |
| 7830 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7833 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7834 | /* resync/recovery still happening */ |
| 7835 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7836 | goto unlock; |
| 7837 | } |
| 7838 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7839 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | goto unlock; |
| 7841 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7842 | /* Set RUNNING before clearing NEEDED to avoid |
| 7843 | * any transients in the value of "sync_action". |
| 7844 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7845 | mddev->curr_resync_completed = 0; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7846 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7847 | /* Clear some bits that don't mean anything, but |
| 7848 | * might be left set |
| 7849 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7850 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7851 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7852 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7853 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7854 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7855 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7856 | /* no recovery is running. |
| 7857 | * remove any failed drives, then |
| 7858 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7859 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7860 | * the personality to fail the re-add. |
| 7861 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7862 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7863 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7864 | if (mddev->pers->check_reshape == NULL || |
| 7865 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7866 | /* Cannot proceed */ |
| 7867 | goto unlock; |
| 7868 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7869 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7870 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7871 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7872 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7873 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7874 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7875 | } else if (mddev->recovery_cp < MaxSector) { |
| 7876 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7877 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7878 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7879 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7880 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7882 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7883 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7884 | /* We are adding a device or devices to an array |
| 7885 | * which has the bitmap stored on all devices. |
| 7886 | * So make sure all bitmap pages get written |
| 7887 | */ |
| 7888 | bitmap_write_all(mddev->bitmap); |
| 7889 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7891 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7892 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7893 | if (!mddev->sync_thread) { |
| 7894 | printk(KERN_ERR "%s: could not start resync" |
| 7895 | " thread...\n", |
| 7896 | mdname(mddev)); |
| 7897 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7898 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7899 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7900 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7901 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7902 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7903 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7904 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7905 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7906 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7907 | } |
| 7908 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7909 | if (!mddev->sync_thread) { |
| 7910 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7911 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7912 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7913 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7914 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7916 | mddev_unlock(mddev); |
| 7917 | } |
| 7918 | } |
| 7919 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7920 | 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] | 7921 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7922 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7923 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7924 | !test_bit(Blocked, &rdev->flags) && |
| 7925 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7926 | msecs_to_jiffies(5000)); |
| 7927 | rdev_dec_pending(rdev, mddev); |
| 7928 | } |
| 7929 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7930 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7931 | void md_finish_reshape(struct mddev *mddev) |
| 7932 | { |
| 7933 | /* called be personality module when reshape completes. */ |
| 7934 | struct md_rdev *rdev; |
| 7935 | |
| 7936 | rdev_for_each(rdev, mddev) { |
| 7937 | if (rdev->data_offset > rdev->new_data_offset) |
| 7938 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7939 | else |
| 7940 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7941 | rdev->data_offset = rdev->new_data_offset; |
| 7942 | } |
| 7943 | } |
| 7944 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7945 | |
| 7946 | /* Bad block management. |
| 7947 | * We can record which blocks on each device are 'bad' and so just |
| 7948 | * fail those blocks, or that stripe, rather than the whole device. |
| 7949 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7950 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7951 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7952 | * A 'shift' can be set so that larger blocks are tracked and |
| 7953 | * consequently larger devices can be covered. |
| 7954 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7955 | * |
| 7956 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7957 | * might need to retry if it is very unlucky. |
| 7958 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7959 | * so we use the write_seqlock_irq variant. |
| 7960 | * |
| 7961 | * When looking for a bad block we specify a range and want to |
| 7962 | * know if any block in the range is bad. So we binary-search |
| 7963 | * to the last range that starts at-or-before the given endpoint, |
| 7964 | * (or "before the sector after the target range") |
| 7965 | * then see if it ends after the given start. |
| 7966 | * We return |
| 7967 | * 0 if there are no known bad blocks in the range |
| 7968 | * 1 if there are known bad block which are all acknowledged |
| 7969 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7970 | * plus the start/length of the first bad section we overlap. |
| 7971 | */ |
| 7972 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7973 | sector_t *first_bad, int *bad_sectors) |
| 7974 | { |
| 7975 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7976 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7977 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7978 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7979 | sector_t target = s + sectors; |
| 7980 | unsigned seq; |
| 7981 | |
| 7982 | if (bb->shift > 0) { |
| 7983 | /* round the start down, and the end up */ |
| 7984 | s >>= bb->shift; |
| 7985 | target += (1<<bb->shift) - 1; |
| 7986 | target >>= bb->shift; |
| 7987 | sectors = target - s; |
| 7988 | } |
| 7989 | /* 'target' is now the first block after the bad range */ |
| 7990 | |
| 7991 | retry: |
| 7992 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7993 | lo = 0; |
| 7994 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7995 | hi = bb->count; |
| 7996 | |
| 7997 | /* Binary search between lo and hi for 'target' |
| 7998 | * i.e. for the last range that starts before 'target' |
| 7999 | */ |
| 8000 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8001 | * are known not to be the last range before target. |
| 8002 | * VARIANT: hi-lo is the number of possible |
| 8003 | * ranges, and decreases until it reaches 1 |
| 8004 | */ |
| 8005 | while (hi - lo > 1) { |
| 8006 | int mid = (lo + hi) / 2; |
| 8007 | sector_t a = BB_OFFSET(p[mid]); |
| 8008 | if (a < target) |
| 8009 | /* This could still be the one, earlier ranges |
| 8010 | * could not. */ |
| 8011 | lo = mid; |
| 8012 | else |
| 8013 | /* This and later ranges are definitely out. */ |
| 8014 | hi = mid; |
| 8015 | } |
| 8016 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8017 | if (hi > lo) { |
| 8018 | /* need to check all range that end after 's' to see if |
| 8019 | * any are unacknowledged. |
| 8020 | */ |
| 8021 | while (lo >= 0 && |
| 8022 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8023 | if (BB_OFFSET(p[lo]) < target) { |
| 8024 | /* starts before the end, and finishes after |
| 8025 | * the start, so they must overlap |
| 8026 | */ |
| 8027 | if (rv != -1 && BB_ACK(p[lo])) |
| 8028 | rv = 1; |
| 8029 | else |
| 8030 | rv = -1; |
| 8031 | *first_bad = BB_OFFSET(p[lo]); |
| 8032 | *bad_sectors = BB_LEN(p[lo]); |
| 8033 | } |
| 8034 | lo--; |
| 8035 | } |
| 8036 | } |
| 8037 | |
| 8038 | if (read_seqretry(&bb->lock, seq)) |
| 8039 | goto retry; |
| 8040 | |
| 8041 | return rv; |
| 8042 | } |
| 8043 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8044 | |
| 8045 | /* |
| 8046 | * Add a range of bad blocks to the table. |
| 8047 | * This might extend the table, or might contract it |
| 8048 | * if two adjacent ranges can be merged. |
| 8049 | * We binary-search to find the 'insertion' point, then |
| 8050 | * decide how best to handle it. |
| 8051 | */ |
| 8052 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8053 | int acknowledged) |
| 8054 | { |
| 8055 | u64 *p; |
| 8056 | int lo, hi; |
| 8057 | int rv = 1; |
| 8058 | |
| 8059 | if (bb->shift < 0) |
| 8060 | /* badblocks are disabled */ |
| 8061 | return 0; |
| 8062 | |
| 8063 | if (bb->shift) { |
| 8064 | /* round the start down, and the end up */ |
| 8065 | sector_t next = s + sectors; |
| 8066 | s >>= bb->shift; |
| 8067 | next += (1<<bb->shift) - 1; |
| 8068 | next >>= bb->shift; |
| 8069 | sectors = next - s; |
| 8070 | } |
| 8071 | |
| 8072 | write_seqlock_irq(&bb->lock); |
| 8073 | |
| 8074 | p = bb->page; |
| 8075 | lo = 0; |
| 8076 | hi = bb->count; |
| 8077 | /* Find the last range that starts at-or-before 's' */ |
| 8078 | while (hi - lo > 1) { |
| 8079 | int mid = (lo + hi) / 2; |
| 8080 | sector_t a = BB_OFFSET(p[mid]); |
| 8081 | if (a <= s) |
| 8082 | lo = mid; |
| 8083 | else |
| 8084 | hi = mid; |
| 8085 | } |
| 8086 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8087 | hi = lo; |
| 8088 | |
| 8089 | if (hi > lo) { |
| 8090 | /* we found a range that might merge with the start |
| 8091 | * of our new range |
| 8092 | */ |
| 8093 | sector_t a = BB_OFFSET(p[lo]); |
| 8094 | sector_t e = a + BB_LEN(p[lo]); |
| 8095 | int ack = BB_ACK(p[lo]); |
| 8096 | if (e >= s) { |
| 8097 | /* Yes, we can merge with a previous range */ |
| 8098 | if (s == a && s + sectors >= e) |
| 8099 | /* new range covers old */ |
| 8100 | ack = acknowledged; |
| 8101 | else |
| 8102 | ack = ack && acknowledged; |
| 8103 | |
| 8104 | if (e < s + sectors) |
| 8105 | e = s + sectors; |
| 8106 | if (e - a <= BB_MAX_LEN) { |
| 8107 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8108 | s = e; |
| 8109 | } else { |
| 8110 | /* does not all fit in one range, |
| 8111 | * make p[lo] maximal |
| 8112 | */ |
| 8113 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8114 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8115 | s = a + BB_MAX_LEN; |
| 8116 | } |
| 8117 | sectors = e - s; |
| 8118 | } |
| 8119 | } |
| 8120 | if (sectors && hi < bb->count) { |
| 8121 | /* 'hi' points to the first range that starts after 's'. |
| 8122 | * Maybe we can merge with the start of that range */ |
| 8123 | sector_t a = BB_OFFSET(p[hi]); |
| 8124 | sector_t e = a + BB_LEN(p[hi]); |
| 8125 | int ack = BB_ACK(p[hi]); |
| 8126 | if (a <= s + sectors) { |
| 8127 | /* merging is possible */ |
| 8128 | if (e <= s + sectors) { |
| 8129 | /* full overlap */ |
| 8130 | e = s + sectors; |
| 8131 | ack = acknowledged; |
| 8132 | } else |
| 8133 | ack = ack && acknowledged; |
| 8134 | |
| 8135 | a = s; |
| 8136 | if (e - a <= BB_MAX_LEN) { |
| 8137 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8138 | s = e; |
| 8139 | } else { |
| 8140 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8141 | s = a + BB_MAX_LEN; |
| 8142 | } |
| 8143 | sectors = e - s; |
| 8144 | lo = hi; |
| 8145 | hi++; |
| 8146 | } |
| 8147 | } |
| 8148 | if (sectors == 0 && hi < bb->count) { |
| 8149 | /* we might be able to combine lo and hi */ |
| 8150 | /* Note: 's' is at the end of 'lo' */ |
| 8151 | sector_t a = BB_OFFSET(p[hi]); |
| 8152 | int lolen = BB_LEN(p[lo]); |
| 8153 | int hilen = BB_LEN(p[hi]); |
| 8154 | int newlen = lolen + hilen - (s - a); |
| 8155 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8156 | /* yes, we can combine them */ |
| 8157 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8158 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8159 | memmove(p + hi, p + hi + 1, |
| 8160 | (bb->count - hi - 1) * 8); |
| 8161 | bb->count--; |
| 8162 | } |
| 8163 | } |
| 8164 | while (sectors) { |
| 8165 | /* didn't merge (it all). |
| 8166 | * Need to add a range just before 'hi' */ |
| 8167 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8168 | /* No room for more */ |
| 8169 | rv = 0; |
| 8170 | break; |
| 8171 | } else { |
| 8172 | int this_sectors = sectors; |
| 8173 | memmove(p + hi + 1, p + hi, |
| 8174 | (bb->count - hi) * 8); |
| 8175 | bb->count++; |
| 8176 | |
| 8177 | if (this_sectors > BB_MAX_LEN) |
| 8178 | this_sectors = BB_MAX_LEN; |
| 8179 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8180 | sectors -= this_sectors; |
| 8181 | s += this_sectors; |
| 8182 | } |
| 8183 | } |
| 8184 | |
| 8185 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8186 | if (!acknowledged) |
| 8187 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8188 | write_sequnlock_irq(&bb->lock); |
| 8189 | |
| 8190 | return rv; |
| 8191 | } |
| 8192 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8193 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8194 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8195 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8196 | int rv; |
| 8197 | if (is_new) |
| 8198 | s += rdev->new_data_offset; |
| 8199 | else |
| 8200 | s += rdev->data_offset; |
| 8201 | rv = md_set_badblocks(&rdev->badblocks, |
| 8202 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8203 | if (rv) { |
| 8204 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8205 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8206 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8207 | md_wakeup_thread(rdev->mddev->thread); |
| 8208 | } |
| 8209 | return rv; |
| 8210 | } |
| 8211 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8212 | |
| 8213 | /* |
| 8214 | * Remove a range of bad blocks from the table. |
| 8215 | * This may involve extending the table if we spilt a region, |
| 8216 | * but it must not fail. So if the table becomes full, we just |
| 8217 | * drop the remove request. |
| 8218 | */ |
| 8219 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8220 | { |
| 8221 | u64 *p; |
| 8222 | int lo, hi; |
| 8223 | sector_t target = s + sectors; |
| 8224 | int rv = 0; |
| 8225 | |
| 8226 | if (bb->shift > 0) { |
| 8227 | /* When clearing we round the start up and the end down. |
| 8228 | * This should not matter as the shift should align with |
| 8229 | * the block size and no rounding should ever be needed. |
| 8230 | * However it is better the think a block is bad when it |
| 8231 | * isn't than to think a block is not bad when it is. |
| 8232 | */ |
| 8233 | s += (1<<bb->shift) - 1; |
| 8234 | s >>= bb->shift; |
| 8235 | target >>= bb->shift; |
| 8236 | sectors = target - s; |
| 8237 | } |
| 8238 | |
| 8239 | write_seqlock_irq(&bb->lock); |
| 8240 | |
| 8241 | p = bb->page; |
| 8242 | lo = 0; |
| 8243 | hi = bb->count; |
| 8244 | /* Find the last range that starts before 'target' */ |
| 8245 | while (hi - lo > 1) { |
| 8246 | int mid = (lo + hi) / 2; |
| 8247 | sector_t a = BB_OFFSET(p[mid]); |
| 8248 | if (a < target) |
| 8249 | lo = mid; |
| 8250 | else |
| 8251 | hi = mid; |
| 8252 | } |
| 8253 | if (hi > lo) { |
| 8254 | /* p[lo] is the last range that could overlap the |
| 8255 | * current range. Earlier ranges could also overlap, |
| 8256 | * but only this one can overlap the end of the range. |
| 8257 | */ |
| 8258 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8259 | /* Partial overlap, leave the tail of this range */ |
| 8260 | int ack = BB_ACK(p[lo]); |
| 8261 | sector_t a = BB_OFFSET(p[lo]); |
| 8262 | sector_t end = a + BB_LEN(p[lo]); |
| 8263 | |
| 8264 | if (a < s) { |
| 8265 | /* we need to split this range */ |
| 8266 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8267 | rv = 0; |
| 8268 | goto out; |
| 8269 | } |
| 8270 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8271 | bb->count++; |
| 8272 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8273 | lo++; |
| 8274 | } |
| 8275 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8276 | /* there is no longer an overlap */ |
| 8277 | hi = lo; |
| 8278 | lo--; |
| 8279 | } |
| 8280 | while (lo >= 0 && |
| 8281 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8282 | /* This range does overlap */ |
| 8283 | if (BB_OFFSET(p[lo]) < s) { |
| 8284 | /* Keep the early parts of this range. */ |
| 8285 | int ack = BB_ACK(p[lo]); |
| 8286 | sector_t start = BB_OFFSET(p[lo]); |
| 8287 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8288 | /* now low doesn't overlap, so.. */ |
| 8289 | break; |
| 8290 | } |
| 8291 | lo--; |
| 8292 | } |
| 8293 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8294 | * anything between needs to be discarded |
| 8295 | */ |
| 8296 | if (hi - lo > 1) { |
| 8297 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8298 | bb->count -= (hi - lo - 1); |
| 8299 | } |
| 8300 | } |
| 8301 | |
| 8302 | bb->changed = 1; |
| 8303 | out: |
| 8304 | write_sequnlock_irq(&bb->lock); |
| 8305 | return rv; |
| 8306 | } |
| 8307 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8308 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8309 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8310 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8311 | if (is_new) |
| 8312 | s += rdev->new_data_offset; |
| 8313 | else |
| 8314 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8315 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8316 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8317 | } |
| 8318 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8319 | |
| 8320 | /* |
| 8321 | * Acknowledge all bad blocks in a list. |
| 8322 | * This only succeeds if ->changed is clear. It is used by |
| 8323 | * in-kernel metadata updates |
| 8324 | */ |
| 8325 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8326 | { |
| 8327 | if (bb->page == NULL || bb->changed) |
| 8328 | /* no point even trying */ |
| 8329 | return; |
| 8330 | write_seqlock_irq(&bb->lock); |
| 8331 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8332 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8333 | u64 *p = bb->page; |
| 8334 | int i; |
| 8335 | for (i = 0; i < bb->count ; i++) { |
| 8336 | if (!BB_ACK(p[i])) { |
| 8337 | sector_t start = BB_OFFSET(p[i]); |
| 8338 | int len = BB_LEN(p[i]); |
| 8339 | p[i] = BB_MAKE(start, len, 1); |
| 8340 | } |
| 8341 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8342 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8343 | } |
| 8344 | write_sequnlock_irq(&bb->lock); |
| 8345 | } |
| 8346 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8347 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8348 | /* sysfs access to bad-blocks list. |
| 8349 | * We present two files. |
| 8350 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8351 | * are recorded as bad. The list is truncated to fit within |
| 8352 | * the one-page limit of sysfs. |
| 8353 | * Writing "sector length" to this file adds an acknowledged |
| 8354 | * bad block list. |
| 8355 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8356 | * been acknowledged. Writing to this file adds bad blocks |
| 8357 | * without acknowledging them. This is largely for testing. |
| 8358 | */ |
| 8359 | |
| 8360 | static ssize_t |
| 8361 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8362 | { |
| 8363 | size_t len; |
| 8364 | int i; |
| 8365 | u64 *p = bb->page; |
| 8366 | unsigned seq; |
| 8367 | |
| 8368 | if (bb->shift < 0) |
| 8369 | return 0; |
| 8370 | |
| 8371 | retry: |
| 8372 | seq = read_seqbegin(&bb->lock); |
| 8373 | |
| 8374 | len = 0; |
| 8375 | i = 0; |
| 8376 | |
| 8377 | while (len < PAGE_SIZE && i < bb->count) { |
| 8378 | sector_t s = BB_OFFSET(p[i]); |
| 8379 | unsigned int length = BB_LEN(p[i]); |
| 8380 | int ack = BB_ACK(p[i]); |
| 8381 | i++; |
| 8382 | |
| 8383 | if (unack && ack) |
| 8384 | continue; |
| 8385 | |
| 8386 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8387 | (unsigned long long)s << bb->shift, |
| 8388 | length << bb->shift); |
| 8389 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8390 | if (unack && len == 0) |
| 8391 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8392 | |
| 8393 | if (read_seqretry(&bb->lock, seq)) |
| 8394 | goto retry; |
| 8395 | |
| 8396 | return len; |
| 8397 | } |
| 8398 | |
| 8399 | #define DO_DEBUG 1 |
| 8400 | |
| 8401 | static ssize_t |
| 8402 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8403 | { |
| 8404 | unsigned long long sector; |
| 8405 | int length; |
| 8406 | char newline; |
| 8407 | #ifdef DO_DEBUG |
| 8408 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8409 | * Normally only a successful write may clear a badblock |
| 8410 | */ |
| 8411 | int clear = 0; |
| 8412 | if (page[0] == '-') { |
| 8413 | clear = 1; |
| 8414 | page++; |
| 8415 | } |
| 8416 | #endif /* DO_DEBUG */ |
| 8417 | |
| 8418 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8419 | case 3: |
| 8420 | if (newline != '\n') |
| 8421 | return -EINVAL; |
| 8422 | case 2: |
| 8423 | if (length <= 0) |
| 8424 | return -EINVAL; |
| 8425 | break; |
| 8426 | default: |
| 8427 | return -EINVAL; |
| 8428 | } |
| 8429 | |
| 8430 | #ifdef DO_DEBUG |
| 8431 | if (clear) { |
| 8432 | md_clear_badblocks(bb, sector, length); |
| 8433 | return len; |
| 8434 | } |
| 8435 | #endif /* DO_DEBUG */ |
| 8436 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8437 | return len; |
| 8438 | else |
| 8439 | return -ENOSPC; |
| 8440 | } |
| 8441 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8442 | static int md_notify_reboot(struct notifier_block *this, |
| 8443 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8444 | { |
| 8445 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8446 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8447 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8448 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8449 | for_each_mddev(mddev, tmp) { |
| 8450 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8451 | if (mddev->pers) |
| 8452 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8453 | mddev->safemode = 2; |
| 8454 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8455 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8456 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8457 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8458 | /* |
| 8459 | * certain more exotic SCSI devices are known to be |
| 8460 | * volatile wrt too early system reboots. While the |
| 8461 | * right place to handle this issue is the given |
| 8462 | * driver, we do want to have a safe RAID driver ... |
| 8463 | */ |
| 8464 | if (need_delay) |
| 8465 | mdelay(1000*1); |
| 8466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8467 | return NOTIFY_DONE; |
| 8468 | } |
| 8469 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8470 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8471 | .notifier_call = md_notify_reboot, |
| 8472 | .next = NULL, |
| 8473 | .priority = INT_MAX, /* before any real devices */ |
| 8474 | }; |
| 8475 | |
| 8476 | static void md_geninit(void) |
| 8477 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8478 | 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] | 8479 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8480 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8481 | } |
| 8482 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8483 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8484 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8485 | int ret = -ENOMEM; |
| 8486 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8487 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8488 | if (!md_wq) |
| 8489 | goto err_wq; |
| 8490 | |
| 8491 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8492 | if (!md_misc_wq) |
| 8493 | goto err_misc_wq; |
| 8494 | |
| 8495 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8496 | goto err_md; |
| 8497 | |
| 8498 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8499 | goto err_mdp; |
| 8500 | mdp_major = ret; |
| 8501 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8502 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8503 | md_probe, NULL, NULL); |
| 8504 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8505 | md_probe, NULL, NULL); |
| 8506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8507 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8508 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8509 | |
| 8510 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8511 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8512 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8513 | err_mdp: |
| 8514 | unregister_blkdev(MD_MAJOR, "md"); |
| 8515 | err_md: |
| 8516 | destroy_workqueue(md_misc_wq); |
| 8517 | err_misc_wq: |
| 8518 | destroy_workqueue(md_wq); |
| 8519 | err_wq: |
| 8520 | return ret; |
| 8521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8522 | |
| 8523 | #ifndef MODULE |
| 8524 | |
| 8525 | /* |
| 8526 | * Searches all registered partitions for autorun RAID arrays |
| 8527 | * at boot time. |
| 8528 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8529 | |
| 8530 | static LIST_HEAD(all_detected_devices); |
| 8531 | struct detected_devices_node { |
| 8532 | struct list_head list; |
| 8533 | dev_t dev; |
| 8534 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8535 | |
| 8536 | void md_autodetect_dev(dev_t dev) |
| 8537 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8538 | struct detected_devices_node *node_detected_dev; |
| 8539 | |
| 8540 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8541 | if (node_detected_dev) { |
| 8542 | node_detected_dev->dev = dev; |
| 8543 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8544 | } else { |
| 8545 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8546 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8548 | } |
| 8549 | |
| 8550 | |
| 8551 | static void autostart_arrays(int part) |
| 8552 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8553 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8554 | struct detected_devices_node *node_detected_dev; |
| 8555 | dev_t dev; |
| 8556 | int i_scanned, i_passed; |
| 8557 | |
| 8558 | i_scanned = 0; |
| 8559 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8560 | |
| 8561 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8562 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8563 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8564 | i_scanned++; |
| 8565 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8566 | struct detected_devices_node, list); |
| 8567 | list_del(&node_detected_dev->list); |
| 8568 | dev = node_detected_dev->dev; |
| 8569 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8570 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8571 | if (IS_ERR(rdev)) |
| 8572 | continue; |
| 8573 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8574 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8575 | MD_BUG(); |
| 8576 | continue; |
| 8577 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8578 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8579 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8580 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8581 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8582 | |
| 8583 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8584 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8585 | |
| 8586 | autorun_devices(part); |
| 8587 | } |
| 8588 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8589 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | |
| 8591 | static __exit void md_exit(void) |
| 8592 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8593 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8594 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8595 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8596 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8597 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8598 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8599 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8600 | unregister_blkdev(mdp_major, "mdp"); |
| 8601 | unregister_reboot_notifier(&md_notifier); |
| 8602 | unregister_sysctl_table(raid_table_header); |
| 8603 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8604 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8605 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8606 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8607 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8608 | destroy_workqueue(md_misc_wq); |
| 8609 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8610 | } |
| 8611 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8612 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8613 | module_exit(md_exit) |
| 8614 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8615 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8616 | { |
| 8617 | return sprintf(buffer, "%d", start_readonly); |
| 8618 | } |
| 8619 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8620 | { |
| 8621 | char *e; |
| 8622 | int num = simple_strtoul(val, &e, 10); |
| 8623 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8624 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8625 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8626 | } |
| 8627 | return -EINVAL; |
| 8628 | } |
| 8629 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8630 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8631 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8632 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8633 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8635 | EXPORT_SYMBOL(register_md_personality); |
| 8636 | EXPORT_SYMBOL(unregister_md_personality); |
| 8637 | EXPORT_SYMBOL(md_error); |
| 8638 | EXPORT_SYMBOL(md_done_sync); |
| 8639 | EXPORT_SYMBOL(md_write_start); |
| 8640 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8641 | EXPORT_SYMBOL(md_register_thread); |
| 8642 | EXPORT_SYMBOL(md_unregister_thread); |
| 8643 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8644 | EXPORT_SYMBOL(md_check_recovery); |
| 8645 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8646 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8647 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8648 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |