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 | ff01bb4 | 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 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 310 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 311 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 312 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 313 | DEFINE_WAIT(__wait); |
| 314 | for (;;) { |
| 315 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 316 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 317 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | break; |
| 319 | rcu_read_unlock(); |
| 320 | schedule(); |
| 321 | rcu_read_lock(); |
| 322 | } |
| 323 | finish_wait(&mddev->sb_wait, &__wait); |
| 324 | } |
| 325 | atomic_inc(&mddev->active_io); |
| 326 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 327 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 328 | /* |
| 329 | * save the sectors now since our bio can |
| 330 | * go away inside make_request |
| 331 | */ |
| 332 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 333 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 334 | |
| 335 | cpu = part_stat_lock(); |
| 336 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 337 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 338 | part_stat_unlock(); |
| 339 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 340 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 341 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 344 | /* mddev_suspend makes sure no new requests are submitted |
| 345 | * to the device, and that any requests that have been submitted |
| 346 | * are completely handled. |
| 347 | * Once ->stop is called and completes, the module will be completely |
| 348 | * unused. |
| 349 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 350 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 351 | { |
| 352 | BUG_ON(mddev->suspended); |
| 353 | mddev->suspended = 1; |
| 354 | synchronize_rcu(); |
| 355 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 356 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 357 | |
| 358 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 359 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 360 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 361 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 362 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 363 | { |
| 364 | mddev->suspended = 0; |
| 365 | wake_up(&mddev->sb_wait); |
| 366 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 367 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 368 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 369 | md_wakeup_thread(mddev->thread); |
| 370 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 371 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 373 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 374 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 375 | { |
| 376 | return mddev->suspended; |
| 377 | } |
| 378 | EXPORT_SYMBOL(mddev_congested); |
| 379 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 381 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 382 | */ |
| 383 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 384 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 385 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 386 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 387 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 388 | |
| 389 | rdev_dec_pending(rdev, mddev); |
| 390 | |
| 391 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 392 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 393 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 394 | } |
| 395 | bio_put(bio); |
| 396 | } |
| 397 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 398 | static void md_submit_flush_data(struct work_struct *ws); |
| 399 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 400 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 402 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 403 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 404 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 405 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 406 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 408 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | if (rdev->raid_disk >= 0 && |
| 410 | !test_bit(Faulty, &rdev->flags)) { |
| 411 | /* Take two references, one is dropped |
| 412 | * when request finishes, one after |
| 413 | * we reclaim rcu_read_lock |
| 414 | */ |
| 415 | struct bio *bi; |
| 416 | atomic_inc(&rdev->nr_pending); |
| 417 | atomic_inc(&rdev->nr_pending); |
| 418 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 419 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 420 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | bi->bi_private = rdev; |
| 422 | bi->bi_bdev = rdev->bdev; |
| 423 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | rcu_read_lock(); |
| 426 | rdev_dec_pending(rdev, mddev); |
| 427 | } |
| 428 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 429 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 430 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
| 432 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 433 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 434 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 435 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 436 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 437 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 438 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | /* an empty barrier - all done */ |
| 440 | bio_endio(bio, 0); |
| 441 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 442 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 443 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 445 | |
| 446 | mddev->flush_bio = NULL; |
| 447 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 448 | } |
| 449 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 450 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 451 | { |
| 452 | spin_lock_irq(&mddev->write_lock); |
| 453 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 454 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 455 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 456 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 457 | spin_unlock_irq(&mddev->write_lock); |
| 458 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 459 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 460 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 461 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 462 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 463 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 464 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 465 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 466 | struct mddev *mddev = cb->data; |
| 467 | md_wakeup_thread(mddev->thread); |
| 468 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 469 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 470 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 471 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 472 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
| 474 | atomic_inc(&mddev->active); |
| 475 | return mddev; |
| 476 | } |
| 477 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 478 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 479 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 480 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 482 | struct bio_set *bs = NULL; |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 485 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 486 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 487 | mddev->ctime == 0 && !mddev->hold_active) { |
| 488 | /* Array is not configured at all, and not held active, |
| 489 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 490 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 491 | bs = mddev->bio_set; |
| 492 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 493 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 494 | /* We did a probe so need to clean up. Call |
| 495 | * queue_work inside the spinlock so that |
| 496 | * flush_workqueue() after mddev_find will |
| 497 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 498 | */ |
| 499 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 500 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 501 | } else |
| 502 | kfree(mddev); |
| 503 | } |
| 504 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 505 | if (bs) |
| 506 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 509 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 510 | { |
| 511 | mutex_init(&mddev->open_mutex); |
| 512 | mutex_init(&mddev->reconfig_mutex); |
| 513 | mutex_init(&mddev->bitmap_info.mutex); |
| 514 | INIT_LIST_HEAD(&mddev->disks); |
| 515 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 516 | init_timer(&mddev->safemode_timer); |
| 517 | atomic_set(&mddev->active, 1); |
| 518 | atomic_set(&mddev->openers, 0); |
| 519 | atomic_set(&mddev->active_io, 0); |
| 520 | spin_lock_init(&mddev->write_lock); |
| 521 | atomic_set(&mddev->flush_pending, 0); |
| 522 | init_waitqueue_head(&mddev->sb_wait); |
| 523 | init_waitqueue_head(&mddev->recovery_wait); |
| 524 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 525 | mddev->reshape_backwards = 0; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 526 | mddev->resync_min = 0; |
| 527 | mddev->resync_max = MaxSector; |
| 528 | mddev->level = LEVEL_NONE; |
| 529 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 530 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 531 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 532 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 534 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 536 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 537 | unit &= ~((1<<MdpMinorShift)-1); |
| 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | retry: |
| 540 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 542 | if (unit) { |
| 543 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 544 | if (mddev->unit == unit) { |
| 545 | mddev_get(mddev); |
| 546 | spin_unlock(&all_mddevs_lock); |
| 547 | kfree(new); |
| 548 | return mddev; |
| 549 | } |
| 550 | |
| 551 | if (new) { |
| 552 | list_add(&new->all_mddevs, &all_mddevs); |
| 553 | spin_unlock(&all_mddevs_lock); |
| 554 | new->hold_active = UNTIL_IOCTL; |
| 555 | return new; |
| 556 | } |
| 557 | } else if (new) { |
| 558 | /* find an unused unit number */ |
| 559 | static int next_minor = 512; |
| 560 | int start = next_minor; |
| 561 | int is_free = 0; |
| 562 | int dev = 0; |
| 563 | while (!is_free) { |
| 564 | dev = MKDEV(MD_MAJOR, next_minor); |
| 565 | next_minor++; |
| 566 | if (next_minor > MINORMASK) |
| 567 | next_minor = 0; |
| 568 | if (next_minor == start) { |
| 569 | /* Oh dear, all in use. */ |
| 570 | spin_unlock(&all_mddevs_lock); |
| 571 | kfree(new); |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | is_free = 1; |
| 576 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 577 | if (mddev->unit == dev) { |
| 578 | is_free = 0; |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | new->unit = dev; |
| 583 | new->md_minor = MINOR(dev); |
| 584 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | list_add(&new->all_mddevs, &all_mddevs); |
| 586 | spin_unlock(&all_mddevs_lock); |
| 587 | return new; |
| 588 | } |
| 589 | spin_unlock(&all_mddevs_lock); |
| 590 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 591 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if (!new) |
| 593 | return NULL; |
| 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | new->unit = unit; |
| 596 | if (MAJOR(unit) == MD_MAJOR) |
| 597 | new->md_minor = MINOR(unit); |
| 598 | else |
| 599 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 600 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 601 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | goto retry; |
| 604 | } |
| 605 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 606 | static inline int mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 608 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 611 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 612 | { |
| 613 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 614 | } |
| 615 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 616 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 618 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 621 | static struct attribute_group md_redundancy_group; |
| 622 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 623 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 625 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 626 | /* These cannot be removed under reconfig_mutex as |
| 627 | * an access to the files will try to take reconfig_mutex |
| 628 | * while holding the file unremovable, which leads to |
| 629 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 630 | * So hold set sysfs_active while the remove in happeing, |
| 631 | * and anything else which might set ->to_remove or my |
| 632 | * otherwise change the sysfs namespace will fail with |
| 633 | * -EBUSY if sysfs_active is still set. |
| 634 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 635 | * test it under the same mutex to ensure its correct value |
| 636 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 637 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 638 | struct attribute_group *to_remove = mddev->to_remove; |
| 639 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 640 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 641 | mutex_unlock(&mddev->reconfig_mutex); |
| 642 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 643 | if (mddev->kobj.sd) { |
| 644 | if (to_remove != &md_redundancy_group) |
| 645 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 646 | if (mddev->pers == NULL || |
| 647 | mddev->pers->sync_request == NULL) { |
| 648 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 649 | if (mddev->sysfs_action) |
| 650 | sysfs_put(mddev->sysfs_action); |
| 651 | mddev->sysfs_action = NULL; |
| 652 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 653 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 654 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 655 | } else |
| 656 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 658 | /* As we've dropped the mutex we need a spinlock to |
| 659 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 660 | */ |
| 661 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 662 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 663 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 666 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 668 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 670 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (rdev->desc_nr == nr) |
| 672 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
| 676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 677 | static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 679 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 681 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (rdev->bdev->bd_dev == dev) |
| 683 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | return NULL; |
| 686 | } |
| 687 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 688 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 689 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 690 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 691 | list_for_each_entry(pers, &pers_list, list) { |
| 692 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 693 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 694 | if (strcmp(pers->name, clevel)==0) |
| 695 | return pers; |
| 696 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 697 | return NULL; |
| 698 | } |
| 699 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 700 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 701 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 703 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 704 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 707 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
| 709 | if (rdev->sb_page) |
| 710 | MD_BUG(); |
| 711 | |
| 712 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 713 | if (!rdev->sb_page) { |
| 714 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 715 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 721 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 724 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | rdev->sb_loaded = 0; |
| 726 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 727 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 728 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 730 | if (rdev->bb_page) { |
| 731 | put_page(rdev->bb_page); |
| 732 | rdev->bb_page = NULL; |
| 733 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 734 | kfree(rdev->badblocks.page); |
| 735 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 737 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 739 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 741 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 742 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 743 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 744 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 745 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 746 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 747 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 748 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 749 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 750 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 751 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 752 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 753 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 754 | } |
| 755 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 756 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 757 | sector_t sector, int size, struct page *page) |
| 758 | { |
| 759 | /* write first size bytes of page to sector of rdev |
| 760 | * Increment mddev->pending_writes before returning |
| 761 | * and decrement it on completion, waking up sb_wait |
| 762 | * if zero is reached. |
| 763 | * If an error occurred, call md_error |
| 764 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 765 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 766 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 767 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 768 | bio->bi_sector = sector; |
| 769 | bio_add_page(bio, page, size, 0); |
| 770 | bio->bi_private = rdev; |
| 771 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 772 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 773 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 774 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 775 | } |
| 776 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 777 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 778 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 779 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 780 | DEFINE_WAIT(wq); |
| 781 | for(;;) { |
| 782 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 783 | if (atomic_read(&mddev->pending_writes)==0) |
| 784 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 785 | schedule(); |
| 786 | } |
| 787 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 788 | } |
| 789 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 790 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 795 | 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] | 796 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 798 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | struct completion event; |
| 800 | int ret; |
| 801 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 802 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 804 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 805 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 806 | if (metadata_op) |
| 807 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 808 | else if (rdev->mddev->reshape_position != MaxSector && |
| 809 | (rdev->mddev->reshape_backwards == |
| 810 | (sector >= rdev->mddev->reshape_position))) |
| 811 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 812 | else |
| 813 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | bio_add_page(bio, page, size, 0); |
| 815 | init_completion(&event); |
| 816 | bio->bi_private = &event; |
| 817 | bio->bi_end_io = bi_complete; |
| 818 | submit_bio(rw, bio); |
| 819 | wait_for_completion(&event); |
| 820 | |
| 821 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 822 | bio_put(bio); |
| 823 | return ret; |
| 824 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 825 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 827 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
| 829 | char b[BDEVNAME_SIZE]; |
| 830 | if (!rdev->sb_page) { |
| 831 | MD_BUG(); |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | if (rdev->sb_loaded) |
| 835 | return 0; |
| 836 | |
| 837 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 838 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | goto fail; |
| 840 | rdev->sb_loaded = 1; |
| 841 | return 0; |
| 842 | |
| 843 | fail: |
| 844 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 845 | bdevname(rdev->bdev,b)); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 850 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 851 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 852 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 853 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 854 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 858 | { |
| 859 | int ret; |
| 860 | mdp_super_t *tmp1, *tmp2; |
| 861 | |
| 862 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 863 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 864 | |
| 865 | if (!tmp1 || !tmp2) { |
| 866 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 867 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | goto abort; |
| 869 | } |
| 870 | |
| 871 | *tmp1 = *sb1; |
| 872 | *tmp2 = *sb2; |
| 873 | |
| 874 | /* |
| 875 | * nr_disks is not constant |
| 876 | */ |
| 877 | tmp1->nr_disks = 0; |
| 878 | tmp2->nr_disks = 0; |
| 879 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 880 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 882 | kfree(tmp1); |
| 883 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return ret; |
| 885 | } |
| 886 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 887 | |
| 888 | static u32 md_csum_fold(u32 csum) |
| 889 | { |
| 890 | csum = (csum & 0xffff) + (csum >> 16); |
| 891 | return (csum & 0xffff) + (csum >> 16); |
| 892 | } |
| 893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 895 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 896 | u64 newcsum = 0; |
| 897 | u32 *sb32 = (u32*)sb; |
| 898 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | unsigned int disk_csum, csum; |
| 900 | |
| 901 | disk_csum = sb->sb_csum; |
| 902 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 903 | |
| 904 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 905 | newcsum += sb32[i]; |
| 906 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 907 | |
| 908 | |
| 909 | #ifdef CONFIG_ALPHA |
| 910 | /* This used to use csum_partial, which was wrong for several |
| 911 | * reasons including that different results are returned on |
| 912 | * different architectures. It isn't critical that we get exactly |
| 913 | * the same return value as before (we always csum_fold before |
| 914 | * testing, and that removes any differences). However as we |
| 915 | * know that csum_partial always returned a 16bit value on |
| 916 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 917 | */ |
| 918 | sb->sb_csum = md_csum_fold(disk_csum); |
| 919 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 921 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | return csum; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | /* |
| 927 | * Handle superblock details. |
| 928 | * We want to be able to handle multiple superblock formats |
| 929 | * so we have a common interface to them all, and an array of |
| 930 | * different handlers. |
| 931 | * We rely on user-space to write the initial superblock, and support |
| 932 | * reading and updating of superblocks. |
| 933 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 934 | * 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] | 935 | * loads and validates a superblock on dev. |
| 936 | * if refdev != NULL, compare superblocks on both devices |
| 937 | * Return: |
| 938 | * 0 - dev has a superblock that is compatible with refdev |
| 939 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 940 | * so dev should be used as the refdev in future |
| 941 | * -EINVAL superblock incompatible or invalid |
| 942 | * -othererror e.g. -EIO |
| 943 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 944 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | * Verify that dev is acceptable into mddev. |
| 946 | * The first time, mddev->raid_disks will be 0, and data from |
| 947 | * dev should be merged in. Subsequent calls check that dev |
| 948 | * is new enough. Return 0 or -EINVAL |
| 949 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 950 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | * Update the superblock for rdev with data in mddev |
| 952 | * This does not write to disc. |
| 953 | * |
| 954 | */ |
| 955 | |
| 956 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 957 | char *name; |
| 958 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 959 | int (*load_super)(struct md_rdev *rdev, |
| 960 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 961 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 962 | int (*validate_super)(struct mddev *mddev, |
| 963 | struct md_rdev *rdev); |
| 964 | void (*sync_super)(struct mddev *mddev, |
| 965 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 966 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 967 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 968 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 969 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 973 | * Check that the given mddev has no bitmap. |
| 974 | * |
| 975 | * This function is called from the run method of all personalities that do not |
| 976 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 977 | * has a bitmap. Otherwise, it returns 0. |
| 978 | * |
| 979 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 980 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 981 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 982 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 983 | return 0; |
| 984 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 985 | mdname(mddev), mddev->pers->name); |
| 986 | return 1; |
| 987 | } |
| 988 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 989 | |
| 990 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | * load_super for 0.90.0 |
| 992 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 993 | 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] | 994 | { |
| 995 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 996 | mdp_super_t *sb; |
| 997 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1000 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | * it's at the end of the disk. |
| 1002 | * |
| 1003 | * It also happens to be a multiple of 4Kb. |
| 1004 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1005 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1007 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (ret) return ret; |
| 1009 | |
| 1010 | ret = -EINVAL; |
| 1011 | |
| 1012 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1013 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1016 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1017 | b); |
| 1018 | goto abort; |
| 1019 | } |
| 1020 | |
| 1021 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1022 | sb->minor_version < 90 || |
| 1023 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1025 | sb->major_version, sb->minor_version, |
| 1026 | b); |
| 1027 | goto abort; |
| 1028 | } |
| 1029 | |
| 1030 | if (sb->raid_disks <= 0) |
| 1031 | goto abort; |
| 1032 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1033 | 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] | 1034 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1035 | b); |
| 1036 | goto abort; |
| 1037 | } |
| 1038 | |
| 1039 | rdev->preferred_minor = sb->md_minor; |
| 1040 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1041 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1042 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1043 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | if (sb->level == LEVEL_MULTIPATH) |
| 1046 | rdev->desc_nr = -1; |
| 1047 | else |
| 1048 | rdev->desc_nr = sb->this_disk.number; |
| 1049 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1050 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1052 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1054 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (!uuid_equal(refsb, sb)) { |
| 1056 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1057 | b, bdevname(refdev->bdev,b2)); |
| 1058 | goto abort; |
| 1059 | } |
| 1060 | if (!sb_equal(refsb, sb)) { |
| 1061 | printk(KERN_WARNING "md: %s has same UUID" |
| 1062 | " but different superblock to %s\n", |
| 1063 | b, bdevname(refdev->bdev, b2)); |
| 1064 | goto abort; |
| 1065 | } |
| 1066 | ev1 = md_event(sb); |
| 1067 | ev2 = md_event(refsb); |
| 1068 | if (ev1 > ev2) |
| 1069 | ret = 1; |
| 1070 | else |
| 1071 | ret = 0; |
| 1072 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1073 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1074 | /* Limit to 4TB as metadata cannot record more than that. |
| 1075 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1076 | * record this size) |
| 1077 | */ |
| 1078 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1079 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1081 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1082 | /* "this cannot possibly happen" ... */ |
| 1083 | ret = -EINVAL; |
| 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | abort: |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * validate_super for 0.90.0 |
| 1091 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1092 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
| 1094 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1095 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1096 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1098 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1099 | clear_bit(Faulty, &rdev->flags); |
| 1100 | clear_bit(In_sync, &rdev->flags); |
| 1101 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (mddev->raid_disks == 0) { |
| 1104 | mddev->major_version = 0; |
| 1105 | mddev->minor_version = sb->minor_version; |
| 1106 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1107 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1108 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | mddev->ctime = sb->ctime; |
| 1110 | mddev->utime = sb->utime; |
| 1111 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1112 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | mddev->layout = sb->layout; |
| 1114 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1115 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1116 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1117 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1118 | mddev->bitmap_info.space = 0; |
| 1119 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1120 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1121 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1122 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1124 | if (mddev->minor_version >= 91) { |
| 1125 | mddev->reshape_position = sb->reshape_position; |
| 1126 | mddev->delta_disks = sb->delta_disks; |
| 1127 | mddev->new_level = sb->new_level; |
| 1128 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1129 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1130 | if (mddev->delta_disks < 0) |
| 1131 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1132 | } else { |
| 1133 | mddev->reshape_position = MaxSector; |
| 1134 | mddev->delta_disks = 0; |
| 1135 | mddev->new_level = mddev->level; |
| 1136 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1137 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1141 | mddev->recovery_cp = MaxSector; |
| 1142 | else { |
| 1143 | if (sb->events_hi == sb->cp_events_hi && |
| 1144 | sb->events_lo == sb->cp_events_lo) { |
| 1145 | mddev->recovery_cp = sb->recovery_cp; |
| 1146 | } else |
| 1147 | mddev->recovery_cp = 0; |
| 1148 | } |
| 1149 | |
| 1150 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1151 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1152 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1153 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1154 | |
| 1155 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1156 | |
| 1157 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1158 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1159 | mddev->bitmap_info.offset = |
| 1160 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1161 | mddev->bitmap_info.space = |
| 1162 | mddev->bitmap_info.space; |
| 1163 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1164 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1165 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1166 | /* Insist on good event counter while assembling, except |
| 1167 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1169 | if (sb->disks[rdev->desc_nr].state & ( |
| 1170 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1171 | if (ev1 < mddev->events) |
| 1172 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1173 | } else if (mddev->bitmap) { |
| 1174 | /* if adding to array with a bitmap, then we can accept an |
| 1175 | * older device ... but not too old. |
| 1176 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1177 | if (ev1 < mddev->bitmap->events_cleared) |
| 1178 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1179 | } else { |
| 1180 | if (ev1 < mddev->events) |
| 1181 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1182 | return 0; |
| 1183 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | desc = sb->disks + rdev->desc_nr; |
| 1187 | |
| 1188 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1189 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1190 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1191 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1192 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1194 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1195 | /* active but not in sync implies recovery up to |
| 1196 | * reshape position. We don't know exactly where |
| 1197 | * that is, so set to zero for now */ |
| 1198 | if (mddev->minor_version >= 91) { |
| 1199 | rdev->recovery_offset = 0; |
| 1200 | rdev->raid_disk = desc->raid_disk; |
| 1201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1203 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1204 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1205 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1206 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * sync_super for 0.90.0 |
| 1212 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1213 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
| 1215 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1216 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | /* make rdev->sb match mddev data.. |
| 1221 | * |
| 1222 | * 1/ zero out disks |
| 1223 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1224 | * 3/ any empty disks < next_spare become removed |
| 1225 | * |
| 1226 | * disks[0] gets initialised to REMOVED because |
| 1227 | * we cannot be sure from other fields if it has |
| 1228 | * been initialised or not. |
| 1229 | */ |
| 1230 | int i; |
| 1231 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1232 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1233 | rdev->sb_size = MD_SB_BYTES; |
| 1234 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1235 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
| 1237 | memset(sb, 0, sizeof(*sb)); |
| 1238 | |
| 1239 | sb->md_magic = MD_SB_MAGIC; |
| 1240 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | sb->patch_version = mddev->patch_version; |
| 1242 | sb->gvalid_words = 0; /* ignored */ |
| 1243 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1244 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1245 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1246 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1247 | |
| 1248 | sb->ctime = mddev->ctime; |
| 1249 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1250 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | sb->raid_disks = mddev->raid_disks; |
| 1252 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1253 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | sb->utime = mddev->utime; |
| 1255 | sb->state = 0; |
| 1256 | sb->events_hi = (mddev->events>>32); |
| 1257 | sb->events_lo = (u32)mddev->events; |
| 1258 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1259 | if (mddev->reshape_position == MaxSector) |
| 1260 | sb->minor_version = 90; |
| 1261 | else { |
| 1262 | sb->minor_version = 91; |
| 1263 | sb->reshape_position = mddev->reshape_position; |
| 1264 | sb->new_level = mddev->new_level; |
| 1265 | sb->delta_disks = mddev->delta_disks; |
| 1266 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1267 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1268 | } |
| 1269 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | if (mddev->in_sync) |
| 1271 | { |
| 1272 | sb->recovery_cp = mddev->recovery_cp; |
| 1273 | sb->cp_events_hi = (mddev->events>>32); |
| 1274 | sb->cp_events_lo = (u32)mddev->events; |
| 1275 | if (mddev->recovery_cp == MaxSector) |
| 1276 | sb->state = (1<< MD_SB_CLEAN); |
| 1277 | } else |
| 1278 | sb->recovery_cp = 0; |
| 1279 | |
| 1280 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1281 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1283 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1284 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1287 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1289 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1290 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1291 | |
| 1292 | if (rdev2->raid_disk >= 0 && |
| 1293 | sb->minor_version >= 91) |
| 1294 | /* we have nowhere to store the recovery_offset, |
| 1295 | * but if it is not below the reshape_position, |
| 1296 | * we can piggy-back on that. |
| 1297 | */ |
| 1298 | is_active = 1; |
| 1299 | if (rdev2->raid_disk < 0 || |
| 1300 | test_bit(Faulty, &rdev2->flags)) |
| 1301 | is_active = 0; |
| 1302 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1303 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1305 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1306 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | d = &sb->disks[rdev2->desc_nr]; |
| 1308 | nr_disks++; |
| 1309 | d->number = rdev2->desc_nr; |
| 1310 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1311 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1312 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | d->raid_disk = rdev2->raid_disk; |
| 1314 | else |
| 1315 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1316 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1318 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1320 | if (test_bit(In_sync, &rdev2->flags)) |
| 1321 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | active++; |
| 1323 | working++; |
| 1324 | } else { |
| 1325 | d->state = 0; |
| 1326 | spare++; |
| 1327 | working++; |
| 1328 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1329 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1330 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1333 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1334 | mdp_disk_t *d = &sb->disks[i]; |
| 1335 | if (d->state == 0 && d->number == 0) { |
| 1336 | d->number = i; |
| 1337 | d->raid_disk = i; |
| 1338 | d->state = (1<<MD_DISK_REMOVED); |
| 1339 | d->state |= (1<<MD_DISK_FAULTY); |
| 1340 | failed++; |
| 1341 | } |
| 1342 | } |
| 1343 | sb->nr_disks = nr_disks; |
| 1344 | sb->active_disks = active; |
| 1345 | sb->working_disks = working; |
| 1346 | sb->failed_disks = failed; |
| 1347 | sb->spare_disks = spare; |
| 1348 | |
| 1349 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1350 | sb->sb_csum = calc_sb_csum(sb); |
| 1351 | } |
| 1352 | |
| 1353 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1354 | * rdev_size_change for 0.90.0 |
| 1355 | */ |
| 1356 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1357 | 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] | 1358 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1359 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1360 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1361 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1362 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1363 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1364 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1365 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1366 | /* Limit to 4TB as metadata cannot record more than that. |
| 1367 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1368 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1369 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1370 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1371 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1372 | rdev->sb_page); |
| 1373 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1374 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1375 | } |
| 1376 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1377 | static int |
| 1378 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1379 | { |
| 1380 | /* non-zero offset changes not possible with v0.90 */ |
| 1381 | return new_offset == 0; |
| 1382 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1383 | |
| 1384 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | * version 1 superblock |
| 1386 | */ |
| 1387 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1388 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1390 | __le32 disk_csum; |
| 1391 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | unsigned long long newcsum; |
| 1393 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1394 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | int i; |
| 1396 | |
| 1397 | disk_csum = sb->sb_csum; |
| 1398 | sb->sb_csum = 0; |
| 1399 | newcsum = 0; |
| 1400 | for (i=0; size>=4; size -= 4 ) |
| 1401 | newcsum += le32_to_cpu(*isuper++); |
| 1402 | |
| 1403 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1404 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1407 | sb->sb_csum = disk_csum; |
| 1408 | return cpu_to_le32(csum); |
| 1409 | } |
| 1410 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1411 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1412 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1413 | 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] | 1414 | { |
| 1415 | struct mdp_superblock_1 *sb; |
| 1416 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1417 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1418 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1420 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1423 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | * It is always aligned to a 4K boundary and |
| 1425 | * depeding on minor_version, it can be: |
| 1426 | * 0: At least 8K, but less than 12K, from end of device |
| 1427 | * 1: At start of device |
| 1428 | * 2: 4K from start of device. |
| 1429 | */ |
| 1430 | switch(minor_version) { |
| 1431 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1432 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1433 | sb_start -= 8*2; |
| 1434 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | break; |
| 1436 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1437 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | break; |
| 1439 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1440 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | break; |
| 1442 | default: |
| 1443 | return -EINVAL; |
| 1444 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1447 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1448 | * and it is safe to read 4k, so we do that |
| 1449 | */ |
| 1450 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | if (ret) return ret; |
| 1452 | |
| 1453 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1454 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
| 1456 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1457 | sb->major_version != cpu_to_le32(1) || |
| 1458 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1459 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1460 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | return -EINVAL; |
| 1462 | |
| 1463 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1464 | printk("md: invalid superblock checksum on %s\n", |
| 1465 | bdevname(rdev->bdev,b)); |
| 1466 | return -EINVAL; |
| 1467 | } |
| 1468 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1469 | printk("md: data_size too small on %s\n", |
| 1470 | bdevname(rdev->bdev,b)); |
| 1471 | return -EINVAL; |
| 1472 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1473 | if (sb->pad0 || |
| 1474 | sb->pad3[0] || |
| 1475 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1476 | /* Some padding is non-zero, might be a new feature */ |
| 1477 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | rdev->preferred_minor = 0xffff; |
| 1480 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1481 | rdev->new_data_offset = rdev->data_offset; |
| 1482 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1483 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1484 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1485 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1487 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1488 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1489 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1490 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1491 | |
| 1492 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1493 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1494 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1495 | if (minor_version |
| 1496 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1497 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1498 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1499 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1500 | rdev->desc_nr = -1; |
| 1501 | else |
| 1502 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1503 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1504 | if (!rdev->bb_page) { |
| 1505 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1506 | if (!rdev->bb_page) |
| 1507 | return -ENOMEM; |
| 1508 | } |
| 1509 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1510 | rdev->badblocks.count == 0) { |
| 1511 | /* need to load the bad block list. |
| 1512 | * Currently we limit it to one page. |
| 1513 | */ |
| 1514 | s32 offset; |
| 1515 | sector_t bb_sector; |
| 1516 | u64 *bbp; |
| 1517 | int i; |
| 1518 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1519 | if (sectors > (PAGE_SIZE / 512)) |
| 1520 | return -EINVAL; |
| 1521 | offset = le32_to_cpu(sb->bblog_offset); |
| 1522 | if (offset == 0) |
| 1523 | return -EINVAL; |
| 1524 | bb_sector = (long long)offset; |
| 1525 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1526 | rdev->bb_page, READ, true)) |
| 1527 | return -EIO; |
| 1528 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1529 | rdev->badblocks.shift = sb->bblog_shift; |
| 1530 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1531 | u64 bb = le64_to_cpu(*bbp); |
| 1532 | int count = bb & (0x3ff); |
| 1533 | u64 sector = bb >> 10; |
| 1534 | sector <<= sb->bblog_shift; |
| 1535 | count <<= sb->bblog_shift; |
| 1536 | if (bb + 1 == 0) |
| 1537 | break; |
| 1538 | if (md_set_badblocks(&rdev->badblocks, |
| 1539 | sector, count, 1) == 0) |
| 1540 | return -EINVAL; |
| 1541 | } |
| 1542 | } else if (sb->bblog_offset == 0) |
| 1543 | rdev->badblocks.shift = -1; |
| 1544 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1545 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1546 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1547 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1549 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1552 | sb->level != refsb->level || |
| 1553 | sb->layout != refsb->layout || |
| 1554 | sb->chunksize != refsb->chunksize) { |
| 1555 | printk(KERN_WARNING "md: %s has strangely different" |
| 1556 | " superblock to %s\n", |
| 1557 | bdevname(rdev->bdev,b), |
| 1558 | bdevname(refdev->bdev,b2)); |
| 1559 | return -EINVAL; |
| 1560 | } |
| 1561 | ev1 = le64_to_cpu(sb->events); |
| 1562 | ev2 = le64_to_cpu(refsb->events); |
| 1563 | |
| 1564 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1565 | ret = 1; |
| 1566 | else |
| 1567 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1569 | if (minor_version) { |
| 1570 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1571 | sectors -= rdev->data_offset; |
| 1572 | } else |
| 1573 | sectors = rdev->sb_start; |
| 1574 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1576 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1577 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1580 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1582 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1583 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1585 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1586 | clear_bit(Faulty, &rdev->flags); |
| 1587 | clear_bit(In_sync, &rdev->flags); |
| 1588 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | if (mddev->raid_disks == 0) { |
| 1591 | mddev->major_version = 1; |
| 1592 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1593 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1594 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1596 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1597 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1598 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | mddev->layout = le32_to_cpu(sb->layout); |
| 1600 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1601 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1602 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1603 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1604 | mddev->bitmap_info.space = 0; |
| 1605 | /* Default location for bitmap is 1K after superblock |
| 1606 | * using 3K - total of 4K |
| 1607 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1608 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1609 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1610 | mddev->reshape_backwards = 0; |
| 1611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1613 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1614 | |
| 1615 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1616 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1617 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1618 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1619 | mddev->bitmap_info.offset = |
| 1620 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1621 | /* Metadata doesn't record how much space is available. |
| 1622 | * For 1.0, we assume we can use up to the superblock |
| 1623 | * if before, else to 4K beyond superblock. |
| 1624 | * For others, assume no change is possible. |
| 1625 | */ |
| 1626 | if (mddev->minor_version > 0) |
| 1627 | mddev->bitmap_info.space = 0; |
| 1628 | else if (mddev->bitmap_info.offset > 0) |
| 1629 | mddev->bitmap_info.space = |
| 1630 | 8 - mddev->bitmap_info.offset; |
| 1631 | else |
| 1632 | mddev->bitmap_info.space = |
| 1633 | -mddev->bitmap_info.offset; |
| 1634 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1635 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1636 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1637 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1638 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1639 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1640 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1641 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1642 | if (mddev->delta_disks < 0 || |
| 1643 | (mddev->delta_disks == 0 && |
| 1644 | (le32_to_cpu(sb->feature_map) |
| 1645 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1646 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1647 | } else { |
| 1648 | mddev->reshape_position = MaxSector; |
| 1649 | mddev->delta_disks = 0; |
| 1650 | mddev->new_level = mddev->level; |
| 1651 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1652 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1655 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1656 | /* Insist of good event counter while assembling, except for |
| 1657 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1659 | if (rdev->desc_nr >= 0 && |
| 1660 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1661 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1662 | if (ev1 < mddev->events) |
| 1663 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1664 | } else if (mddev->bitmap) { |
| 1665 | /* If adding to array with a bitmap, then we can accept an |
| 1666 | * older device, but not too old. |
| 1667 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1668 | if (ev1 < mddev->bitmap->events_cleared) |
| 1669 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1670 | } else { |
| 1671 | if (ev1 < mddev->events) |
| 1672 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1673 | return 0; |
| 1674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1676 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1677 | if (rdev->desc_nr < 0 || |
| 1678 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1679 | role = 0xffff; |
| 1680 | rdev->desc_nr = -1; |
| 1681 | } else |
| 1682 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | switch(role) { |
| 1684 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | break; |
| 1686 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1687 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | break; |
| 1689 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1690 | if ((le32_to_cpu(sb->feature_map) & |
| 1691 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1692 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1693 | else |
| 1694 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | rdev->raid_disk = role; |
| 1696 | break; |
| 1697 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1698 | if (sb->devflags & WriteMostly1) |
| 1699 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1700 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1701 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1702 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1703 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | return 0; |
| 1706 | } |
| 1707 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1708 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | { |
| 1710 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1711 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | int max_dev, i; |
| 1713 | /* make rdev->sb match mddev and rdev data. */ |
| 1714 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1715 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
| 1717 | sb->feature_map = 0; |
| 1718 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1719 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1721 | |
| 1722 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1723 | sb->events = cpu_to_le64(mddev->events); |
| 1724 | if (mddev->in_sync) |
| 1725 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1726 | else |
| 1727 | sb->resync_offset = cpu_to_le64(0); |
| 1728 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1729 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1730 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1731 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1732 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1733 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1734 | sb->level = cpu_to_le32(mddev->level); |
| 1735 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1736 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1737 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1738 | sb->devflags |= WriteMostly1; |
| 1739 | else |
| 1740 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1741 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1742 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1743 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1744 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1745 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1746 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1747 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1748 | |
| 1749 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1750 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1751 | sb->feature_map |= |
| 1752 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1753 | sb->recovery_offset = |
| 1754 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1755 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1756 | if (test_bit(Replacement, &rdev->flags)) |
| 1757 | sb->feature_map |= |
| 1758 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1759 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1760 | if (mddev->reshape_position != MaxSector) { |
| 1761 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1762 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1763 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1764 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1765 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1766 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1767 | if (mddev->delta_disks == 0 && |
| 1768 | mddev->reshape_backwards) |
| 1769 | sb->feature_map |
| 1770 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1771 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1772 | sb->feature_map |
| 1773 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1774 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1775 | - rdev->data_offset)); |
| 1776 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1777 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1778 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | if (rdev->badblocks.count == 0) |
| 1780 | /* Nothing to do for bad blocks*/ ; |
| 1781 | else if (sb->bblog_offset == 0) |
| 1782 | /* Cannot record bad blocks on this device */ |
| 1783 | md_error(mddev, rdev); |
| 1784 | else { |
| 1785 | struct badblocks *bb = &rdev->badblocks; |
| 1786 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1787 | u64 *p = bb->page; |
| 1788 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1789 | if (bb->changed) { |
| 1790 | unsigned seq; |
| 1791 | |
| 1792 | retry: |
| 1793 | seq = read_seqbegin(&bb->lock); |
| 1794 | |
| 1795 | memset(bbp, 0xff, PAGE_SIZE); |
| 1796 | |
| 1797 | for (i = 0 ; i < bb->count ; i++) { |
| 1798 | u64 internal_bb = *p++; |
| 1799 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1800 | | BB_LEN(internal_bb)); |
| 1801 | *bbp++ = cpu_to_le64(store_bb); |
| 1802 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1803 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1804 | if (read_seqretry(&bb->lock, seq)) |
| 1805 | goto retry; |
| 1806 | |
| 1807 | bb->sector = (rdev->sb_start + |
| 1808 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1809 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1814 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | if (rdev2->desc_nr+1 > max_dev) |
| 1816 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1817 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1818 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1819 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1820 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1821 | rdev->sb_size = max_dev * 2 + 256; |
| 1822 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1823 | if (rdev->sb_size & bmask) |
| 1824 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1825 | } else |
| 1826 | max_dev = le32_to_cpu(sb->max_dev); |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | for (i=0; i<max_dev;i++) |
| 1829 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1830 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1831 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1833 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1835 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1837 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1838 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | else |
| 1840 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1841 | } |
| 1842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1844 | } |
| 1845 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1846 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1847 | 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] | 1848 | { |
| 1849 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1850 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1851 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1852 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1853 | if (rdev->data_offset != rdev->new_data_offset) |
| 1854 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1855 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1856 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1857 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1858 | max_sectors -= rdev->data_offset; |
| 1859 | if (!num_sectors || num_sectors > max_sectors) |
| 1860 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1861 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1862 | /* minor version 0 with bitmap we can't move */ |
| 1863 | return 0; |
| 1864 | } else { |
| 1865 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1866 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1867 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1868 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1869 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1870 | if (!num_sectors || num_sectors > max_sectors) |
| 1871 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1872 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1873 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1874 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1875 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1876 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1877 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1878 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1879 | rdev->sb_page); |
| 1880 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1881 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1882 | |
| 1883 | } |
| 1884 | |
| 1885 | static int |
| 1886 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1887 | unsigned long long new_offset) |
| 1888 | { |
| 1889 | /* All necessary checks on new >= old have been done */ |
| 1890 | struct bitmap *bitmap; |
| 1891 | if (new_offset >= rdev->data_offset) |
| 1892 | return 1; |
| 1893 | |
| 1894 | /* with 1.0 metadata, there is no metadata to tread on |
| 1895 | * so we can always move back */ |
| 1896 | if (rdev->mddev->minor_version == 0) |
| 1897 | return 1; |
| 1898 | |
| 1899 | /* otherwise we must be sure not to step on |
| 1900 | * any metadata, so stay: |
| 1901 | * 36K beyond start of superblock |
| 1902 | * beyond end of badblocks |
| 1903 | * beyond write-intent bitmap |
| 1904 | */ |
| 1905 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1906 | return 0; |
| 1907 | bitmap = rdev->mddev->bitmap; |
| 1908 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1909 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1910 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1911 | return 0; |
| 1912 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1913 | return 0; |
| 1914 | |
| 1915 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1918 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | [0] = { |
| 1920 | .name = "0.90.0", |
| 1921 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1922 | .load_super = super_90_load, |
| 1923 | .validate_super = super_90_validate, |
| 1924 | .sync_super = super_90_sync, |
| 1925 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1926 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | }, |
| 1928 | [1] = { |
| 1929 | .name = "md-1", |
| 1930 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1931 | .load_super = super_1_load, |
| 1932 | .validate_super = super_1_validate, |
| 1933 | .sync_super = super_1_sync, |
| 1934 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1935 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | }, |
| 1937 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1939 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1940 | { |
| 1941 | if (mddev->sync_super) { |
| 1942 | mddev->sync_super(mddev, rdev); |
| 1943 | return; |
| 1944 | } |
| 1945 | |
| 1946 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1947 | |
| 1948 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1949 | } |
| 1950 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1951 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1953 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1955 | rcu_read_lock(); |
| 1956 | rdev_for_each_rcu(rdev, mddev1) |
| 1957 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1958 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1959 | rdev2->bdev->bd_contains) { |
| 1960 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1961 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1962 | } |
| 1963 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static LIST_HEAD(pending_raid_disks); |
| 1968 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1969 | /* |
| 1970 | * Try to register data integrity profile for an mddev |
| 1971 | * |
| 1972 | * This is called when an array is started and after a disk has been kicked |
| 1973 | * from the array. It only succeeds if all working and active component devices |
| 1974 | * are integrity capable with matching profiles. |
| 1975 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1976 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1977 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1978 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1979 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1980 | if (list_empty(&mddev->disks)) |
| 1981 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1982 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1983 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1984 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1985 | /* skip spares and non-functional disks */ |
| 1986 | if (test_bit(Faulty, &rdev->flags)) |
| 1987 | continue; |
| 1988 | if (rdev->raid_disk < 0) |
| 1989 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | if (!reference) { |
| 1991 | /* Use the first rdev as the reference */ |
| 1992 | reference = rdev; |
| 1993 | continue; |
| 1994 | } |
| 1995 | /* does this rdev's profile match the reference profile? */ |
| 1996 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1997 | rdev->bdev->bd_disk) < 0) |
| 1998 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1999 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2000 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2001 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2002 | /* |
| 2003 | * All component devices are integrity capable and have matching |
| 2004 | * profiles, register the common profile for the md device. |
| 2005 | */ |
| 2006 | if (blk_integrity_register(mddev->gendisk, |
| 2007 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2008 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2009 | mdname(mddev)); |
| 2010 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2011 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2012 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2013 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2014 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2015 | mdname(mddev)); |
| 2016 | return -EINVAL; |
| 2017 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2018 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2019 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2020 | EXPORT_SYMBOL(md_integrity_register); |
| 2021 | |
| 2022 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2023 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2024 | { |
| 2025 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2026 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 2027 | |
| 2028 | if (!bi_mddev) /* nothing to do */ |
| 2029 | return; |
| 2030 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2031 | return; |
| 2032 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2033 | rdev->bdev->bd_disk) >= 0) |
| 2034 | return; |
| 2035 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2036 | blk_integrity_unregister(mddev->gendisk); |
| 2037 | } |
| 2038 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2039 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2040 | 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] | 2041 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2042 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2043 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2044 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2045 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
| 2047 | if (rdev->mddev) { |
| 2048 | MD_BUG(); |
| 2049 | return -EINVAL; |
| 2050 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2051 | |
| 2052 | /* prevent duplicates */ |
| 2053 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2054 | return -EEXIST; |
| 2055 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2056 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2057 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2058 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2059 | if (mddev->pers) { |
| 2060 | /* Cannot change size, so fail |
| 2061 | * If mddev->level <= 0, then we don't care |
| 2062 | * about aligning sizes (e.g. linear) |
| 2063 | */ |
| 2064 | if (mddev->level > 0) |
| 2065 | return -ENOSPC; |
| 2066 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2067 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
| 2070 | /* Verify rdev->desc_nr is unique. |
| 2071 | * If it is -1, assign a free number, else |
| 2072 | * check number is not in use |
| 2073 | */ |
| 2074 | if (rdev->desc_nr < 0) { |
| 2075 | int choice = 0; |
| 2076 | if (mddev->pers) choice = mddev->raid_disks; |
| 2077 | while (find_rdev_nr(mddev, choice)) |
| 2078 | choice++; |
| 2079 | rdev->desc_nr = choice; |
| 2080 | } else { |
| 2081 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2082 | return -EBUSY; |
| 2083 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2084 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2085 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2086 | mdname(mddev), mddev->max_disks); |
| 2087 | return -EBUSY; |
| 2088 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2089 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2090 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2091 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2094 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2095 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2096 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2097 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2098 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2099 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2100 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2101 | /* failure here is OK */; |
| 2102 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2103 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2104 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2105 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2106 | |
| 2107 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2108 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2111 | |
| 2112 | fail: |
| 2113 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2114 | b, mdname(mddev)); |
| 2115 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2118 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2119 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2120 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2121 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2122 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2125 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
| 2127 | char b[BDEVNAME_SIZE]; |
| 2128 | if (!rdev->mddev) { |
| 2129 | MD_BUG(); |
| 2130 | return; |
| 2131 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2132 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2133 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2135 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2136 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2137 | sysfs_put(rdev->sysfs_state); |
| 2138 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2139 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2140 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2141 | * writing to 'remove' to "dev/state". We also need |
| 2142 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2143 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2144 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2145 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2146 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2147 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * prevent the device from being mounted, repartitioned or |
| 2152 | * otherwise reused by a RAID array (or any other kernel |
| 2153 | * subsystem), by bd_claiming the device. |
| 2154 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2155 | 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] | 2156 | { |
| 2157 | int err = 0; |
| 2158 | struct block_device *bdev; |
| 2159 | char b[BDEVNAME_SIZE]; |
| 2160 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2161 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2162 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | if (IS_ERR(bdev)) { |
| 2164 | printk(KERN_ERR "md: could not open %s.\n", |
| 2165 | __bdevname(dev, b)); |
| 2166 | return PTR_ERR(bdev); |
| 2167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | rdev->bdev = bdev; |
| 2169 | return err; |
| 2170 | } |
| 2171 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2172 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { |
| 2174 | struct block_device *bdev = rdev->bdev; |
| 2175 | rdev->bdev = NULL; |
| 2176 | if (!bdev) |
| 2177 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2178 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | void md_autodetect_dev(dev_t dev); |
| 2182 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2183 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | { |
| 2185 | char b[BDEVNAME_SIZE]; |
| 2186 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2187 | bdevname(rdev->bdev,b)); |
| 2188 | if (rdev->mddev) |
| 2189 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2190 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2192 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2193 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | #endif |
| 2195 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2196 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2199 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | { |
| 2201 | unbind_rdev_from_array(rdev); |
| 2202 | export_rdev(rdev); |
| 2203 | } |
| 2204 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2205 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2207 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2209 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | if (!rdev->mddev) { |
| 2211 | MD_BUG(); |
| 2212 | continue; |
| 2213 | } |
| 2214 | kick_rdev_from_array(rdev); |
| 2215 | } |
| 2216 | if (!list_empty(&mddev->disks)) |
| 2217 | MD_BUG(); |
| 2218 | mddev->raid_disks = 0; |
| 2219 | mddev->major_version = 0; |
| 2220 | } |
| 2221 | |
| 2222 | static void print_desc(mdp_disk_t *desc) |
| 2223 | { |
| 2224 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2225 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2226 | } |
| 2227 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2228 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | { |
| 2230 | int i; |
| 2231 | |
| 2232 | printk(KERN_INFO |
| 2233 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2234 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2235 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2236 | sb->ctime); |
| 2237 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2238 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2239 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2240 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2241 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2242 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2243 | sb->failed_disks, sb->spare_disks, |
| 2244 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2245 | |
| 2246 | printk(KERN_INFO); |
| 2247 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2248 | mdp_disk_t *desc; |
| 2249 | |
| 2250 | desc = sb->disks + i; |
| 2251 | if (desc->number || desc->major || desc->minor || |
| 2252 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2253 | printk(" D %2d: ", i); |
| 2254 | print_desc(desc); |
| 2255 | } |
| 2256 | } |
| 2257 | printk(KERN_INFO "md: THIS: "); |
| 2258 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | } |
| 2260 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2261 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2262 | { |
| 2263 | __u8 *uuid; |
| 2264 | |
| 2265 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2266 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2267 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2268 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2269 | le32_to_cpu(sb->major_version), |
| 2270 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2271 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2272 | sb->set_name, |
| 2273 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2274 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2275 | |
| 2276 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2277 | printk(KERN_INFO |
| 2278 | "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] | 2279 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2280 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2281 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2282 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2283 | le32_to_cpu(sb->level), |
| 2284 | (unsigned long long)le64_to_cpu(sb->size), |
| 2285 | le32_to_cpu(sb->raid_disks), |
| 2286 | le32_to_cpu(sb->layout), |
| 2287 | le32_to_cpu(sb->chunksize), |
| 2288 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2289 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2290 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2291 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2292 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2293 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2294 | sb->devflags, |
| 2295 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2296 | (unsigned long long)le64_to_cpu(sb->events), |
| 2297 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2298 | le32_to_cpu(sb->sb_csum), |
| 2299 | le32_to_cpu(sb->max_dev) |
| 2300 | ); |
| 2301 | } |
| 2302 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2303 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | { |
| 2305 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2306 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2307 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2308 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2309 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2311 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2312 | switch (major_version) { |
| 2313 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2314 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2315 | break; |
| 2316 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2317 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2318 | break; |
| 2319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | } else |
| 2321 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2322 | } |
| 2323 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2324 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2326 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2327 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2328 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | char b[BDEVNAME_SIZE]; |
| 2330 | |
| 2331 | printk("\n"); |
| 2332 | printk("md: **********************************\n"); |
| 2333 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2334 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2335 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2337 | if (mddev->bitmap) |
| 2338 | bitmap_print_sb(mddev->bitmap); |
| 2339 | else |
| 2340 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2341 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2343 | printk("\n"); |
| 2344 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2345 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2346 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | } |
| 2348 | printk("md: **********************************\n"); |
| 2349 | printk("\n"); |
| 2350 | } |
| 2351 | |
| 2352 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2353 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2355 | /* Update each superblock (in-memory image), but |
| 2356 | * if we are allowed to, skip spares which already |
| 2357 | * have the right event counter, or have one earlier |
| 2358 | * (which would mean they aren't being marked as dirty |
| 2359 | * with the rest of the array) |
| 2360 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2361 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2362 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2363 | if (rdev->sb_events == mddev->events || |
| 2364 | (nospares && |
| 2365 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2366 | rdev->sb_events+1 == mddev->events)) { |
| 2367 | /* Don't update this superblock */ |
| 2368 | rdev->sb_loaded = 2; |
| 2369 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2370 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2371 | rdev->sb_loaded = 1; |
| 2372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2376 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2378 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2379 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2380 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2381 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2384 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2385 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2386 | if (rdev->raid_disk >= 0 && |
| 2387 | mddev->delta_disks >= 0 && |
| 2388 | !test_bit(In_sync, &rdev->flags) && |
| 2389 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2390 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2391 | |
| 2392 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2393 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2394 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2395 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2396 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2397 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2398 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2399 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2400 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2401 | md_ack_all_badblocks(&rdev->badblocks); |
| 2402 | md_error(mddev, rdev); |
| 2403 | } |
| 2404 | clear_bit(Blocked, &rdev->flags); |
| 2405 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2406 | wake_up(&rdev->blocked_wait); |
| 2407 | } |
| 2408 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2409 | wake_up(&mddev->sb_wait); |
| 2410 | return; |
| 2411 | } |
| 2412 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2413 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2414 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2415 | mddev->utime = get_seconds(); |
| 2416 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2417 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2418 | force_change = 1; |
| 2419 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2420 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2421 | * though if events isn't the right even/odd, we will have to do |
| 2422 | * spares after all |
| 2423 | */ |
| 2424 | nospares = 1; |
| 2425 | if (force_change) |
| 2426 | nospares = 0; |
| 2427 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2428 | /* If the array is degraded, then skipping spares is both |
| 2429 | * dangerous and fairly pointless. |
| 2430 | * Dangerous because a device that was removed from the array |
| 2431 | * might have a event_count that still looks up-to-date, |
| 2432 | * so it can be re-added without a resync. |
| 2433 | * Pointless because if there are any spares to skip, |
| 2434 | * then a recovery will happen and soon that array won't |
| 2435 | * be degraded any more and the spare can go back to sleep then. |
| 2436 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2437 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2438 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2439 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2440 | |
| 2441 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2442 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2443 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2444 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2445 | && mddev->can_decrease_events |
| 2446 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2447 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2448 | mddev->can_decrease_events = 0; |
| 2449 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2450 | /* otherwise we have to go forward and ... */ |
| 2451 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2452 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | |
| 2455 | if (!mddev->events) { |
| 2456 | /* |
| 2457 | * oops, this 64-bit counter should never wrap. |
| 2458 | * Either we are in around ~1 trillion A.C., assuming |
| 2459 | * 1 reboot per second, or we have a bug: |
| 2460 | */ |
| 2461 | MD_BUG(); |
| 2462 | mddev->events --; |
| 2463 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2464 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2465 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2466 | if (rdev->badblocks.changed) |
| 2467 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2468 | if (test_bit(Faulty, &rdev->flags)) |
| 2469 | set_bit(FaultRecorded, &rdev->flags); |
| 2470 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2471 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2472 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2473 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2475 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2476 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2478 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2479 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2481 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2482 | if (rdev->sb_loaded != 1) |
| 2483 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2485 | if (!test_bit(Faulty, &rdev->flags) && |
| 2486 | rdev->saved_raid_disk == -1) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2487 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2488 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2489 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2490 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2491 | bdevname(rdev->bdev, b), |
| 2492 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2493 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2494 | if (rdev->badblocks.size) { |
| 2495 | md_super_write(mddev, rdev, |
| 2496 | rdev->badblocks.sector, |
| 2497 | rdev->badblocks.size << 9, |
| 2498 | rdev->bb_page); |
| 2499 | rdev->badblocks.size = 0; |
| 2500 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2501 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2502 | } else if (test_bit(Faulty, &rdev->flags)) |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2503 | pr_debug("md: %s (skipping faulty)\n", |
| 2504 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2505 | else |
| 2506 | pr_debug("(skipping incremental s/r "); |
| 2507 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2508 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | /* only need to write one superblock... */ |
| 2510 | break; |
| 2511 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2512 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2513 | /* 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] | 2514 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2515 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2516 | if (mddev->in_sync != sync_req || |
| 2517 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2518 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2519 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2520 | goto repeat; |
| 2521 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2522 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2523 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2524 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2525 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2526 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2527 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2528 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2529 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2530 | clear_bit(Blocked, &rdev->flags); |
| 2531 | |
| 2532 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2533 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2534 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2535 | wake_up(&rdev->blocked_wait); |
| 2536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2539 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2540 | * We want to accept with case. For this we use cmd_match. |
| 2541 | */ |
| 2542 | static int cmd_match(const char *cmd, const char *str) |
| 2543 | { |
| 2544 | /* See if cmd, written into a sysfs file, matches |
| 2545 | * str. They must either be the same, or cmd can |
| 2546 | * have a trailing newline |
| 2547 | */ |
| 2548 | while (*cmd && *str && *cmd == *str) { |
| 2549 | cmd++; |
| 2550 | str++; |
| 2551 | } |
| 2552 | if (*cmd == '\n') |
| 2553 | cmd++; |
| 2554 | if (*str || *cmd) |
| 2555 | return 0; |
| 2556 | return 1; |
| 2557 | } |
| 2558 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2559 | struct rdev_sysfs_entry { |
| 2560 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2561 | ssize_t (*show)(struct md_rdev *, char *); |
| 2562 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2563 | }; |
| 2564 | |
| 2565 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2566 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2567 | { |
| 2568 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2569 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2570 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2571 | if (test_bit(Faulty, &rdev->flags) || |
| 2572 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2573 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2574 | sep = ","; |
| 2575 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2576 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2577 | len += sprintf(page+len, "%sin_sync",sep); |
| 2578 | sep = ","; |
| 2579 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2580 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2581 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2582 | sep = ","; |
| 2583 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2584 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2585 | (rdev->badblocks.unacked_exist |
| 2586 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2587 | len += sprintf(page+len, "%sblocked", sep); |
| 2588 | sep = ","; |
| 2589 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2590 | if (!test_bit(Faulty, &rdev->flags) && |
| 2591 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2592 | len += sprintf(page+len, "%sspare", sep); |
| 2593 | sep = ","; |
| 2594 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2595 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2596 | len += sprintf(page+len, "%swrite_error", sep); |
| 2597 | sep = ","; |
| 2598 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2599 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2600 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2601 | sep = ","; |
| 2602 | } |
| 2603 | if (test_bit(Replacement, &rdev->flags)) { |
| 2604 | len += sprintf(page+len, "%sreplacement", sep); |
| 2605 | sep = ","; |
| 2606 | } |
| 2607 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2608 | return len+sprintf(page+len, "\n"); |
| 2609 | } |
| 2610 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2611 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2612 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2613 | { |
| 2614 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2615 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2616 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2617 | * writemostly - sets write_mostly |
| 2618 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2619 | * blocked - sets the Blocked flags |
| 2620 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2621 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2622 | * write_error - sets WriteErrorSeen |
| 2623 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2624 | */ |
| 2625 | int err = -EINVAL; |
| 2626 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2627 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2628 | if (test_bit(Faulty, &rdev->flags)) |
| 2629 | err = 0; |
| 2630 | else |
| 2631 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2632 | } else if (cmd_match(buf, "remove")) { |
| 2633 | if (rdev->raid_disk >= 0) |
| 2634 | err = -EBUSY; |
| 2635 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2636 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2637 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2638 | if (mddev->pers) |
| 2639 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2640 | md_new_event(mddev); |
| 2641 | err = 0; |
| 2642 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2643 | } else if (cmd_match(buf, "writemostly")) { |
| 2644 | set_bit(WriteMostly, &rdev->flags); |
| 2645 | err = 0; |
| 2646 | } else if (cmd_match(buf, "-writemostly")) { |
| 2647 | clear_bit(WriteMostly, &rdev->flags); |
| 2648 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2649 | } else if (cmd_match(buf, "blocked")) { |
| 2650 | set_bit(Blocked, &rdev->flags); |
| 2651 | err = 0; |
| 2652 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2653 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2654 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2655 | /* metadata handler doesn't understand badblocks, |
| 2656 | * so we need to fail the device |
| 2657 | */ |
| 2658 | md_error(rdev->mddev, rdev); |
| 2659 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2660 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2661 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2662 | wake_up(&rdev->blocked_wait); |
| 2663 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2664 | md_wakeup_thread(rdev->mddev->thread); |
| 2665 | |
| 2666 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2667 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2668 | set_bit(In_sync, &rdev->flags); |
| 2669 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2670 | } else if (cmd_match(buf, "write_error")) { |
| 2671 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2672 | err = 0; |
| 2673 | } else if (cmd_match(buf, "-write_error")) { |
| 2674 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2675 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2676 | } else if (cmd_match(buf, "want_replacement")) { |
| 2677 | /* Any non-spare device that is not a replacement can |
| 2678 | * become want_replacement at any time, but we then need to |
| 2679 | * check if recovery is needed. |
| 2680 | */ |
| 2681 | if (rdev->raid_disk >= 0 && |
| 2682 | !test_bit(Replacement, &rdev->flags)) |
| 2683 | set_bit(WantReplacement, &rdev->flags); |
| 2684 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2685 | md_wakeup_thread(rdev->mddev->thread); |
| 2686 | err = 0; |
| 2687 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2688 | /* Clearing 'want_replacement' is always allowed. |
| 2689 | * Once replacements starts it is too late though. |
| 2690 | */ |
| 2691 | err = 0; |
| 2692 | clear_bit(WantReplacement, &rdev->flags); |
| 2693 | } else if (cmd_match(buf, "replacement")) { |
| 2694 | /* Can only set a device as a replacement when array has not |
| 2695 | * yet been started. Once running, replacement is automatic |
| 2696 | * from spares, or by assigning 'slot'. |
| 2697 | */ |
| 2698 | if (rdev->mddev->pers) |
| 2699 | err = -EBUSY; |
| 2700 | else { |
| 2701 | set_bit(Replacement, &rdev->flags); |
| 2702 | err = 0; |
| 2703 | } |
| 2704 | } else if (cmd_match(buf, "-replacement")) { |
| 2705 | /* Similarly, can only clear Replacement before start */ |
| 2706 | if (rdev->mddev->pers) |
| 2707 | err = -EBUSY; |
| 2708 | else { |
| 2709 | clear_bit(Replacement, &rdev->flags); |
| 2710 | err = 0; |
| 2711 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2712 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2713 | if (!err) |
| 2714 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2715 | return err ? err : len; |
| 2716 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2717 | static struct rdev_sysfs_entry rdev_state = |
| 2718 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2719 | |
| 2720 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2721 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2722 | { |
| 2723 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2724 | } |
| 2725 | |
| 2726 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2727 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2728 | { |
| 2729 | char *e; |
| 2730 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2731 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2732 | atomic_set(&rdev->corrected_errors, n); |
| 2733 | return len; |
| 2734 | } |
| 2735 | return -EINVAL; |
| 2736 | } |
| 2737 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2738 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2739 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2740 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2741 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2742 | { |
| 2743 | if (rdev->raid_disk < 0) |
| 2744 | return sprintf(page, "none\n"); |
| 2745 | else |
| 2746 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2747 | } |
| 2748 | |
| 2749 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2750 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2751 | { |
| 2752 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2753 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2754 | int slot = simple_strtoul(buf, &e, 10); |
| 2755 | if (strncmp(buf, "none", 4)==0) |
| 2756 | slot = -1; |
| 2757 | else if (e==buf || (*e && *e!= '\n')) |
| 2758 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2759 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2760 | /* Setting 'slot' on an active array requires also |
| 2761 | * updating the 'rd%d' link, and communicating |
| 2762 | * with the personality with ->hot_*_disk. |
| 2763 | * For now we only support removing |
| 2764 | * failed/spare devices. This normally happens automatically, |
| 2765 | * but not when the metadata is externally managed. |
| 2766 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2767 | if (rdev->raid_disk == -1) |
| 2768 | return -EEXIST; |
| 2769 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2770 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2771 | return -EINVAL; |
| 2772 | err = rdev->mddev->pers-> |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2773 | hot_remove_disk(rdev->mddev, rdev); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2774 | if (err) |
| 2775 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2776 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2777 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2778 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2779 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2780 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2781 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2782 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2783 | */ |
| 2784 | |
| 2785 | if (rdev->raid_disk != -1) |
| 2786 | return -EBUSY; |
| 2787 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2788 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2789 | return -EBUSY; |
| 2790 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2791 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2792 | return -EINVAL; |
| 2793 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2794 | if (slot >= rdev->mddev->raid_disks && |
| 2795 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2796 | return -ENOSPC; |
| 2797 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2798 | rdev->raid_disk = slot; |
| 2799 | if (test_bit(In_sync, &rdev->flags)) |
| 2800 | rdev->saved_raid_disk = slot; |
| 2801 | else |
| 2802 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2803 | clear_bit(In_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2804 | err = rdev->mddev->pers-> |
| 2805 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2806 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2807 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2808 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2809 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2810 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2811 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2812 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2813 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2814 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2815 | if (slot >= rdev->mddev->raid_disks && |
| 2816 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2817 | return -ENOSPC; |
| 2818 | rdev->raid_disk = slot; |
| 2819 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2820 | clear_bit(Faulty, &rdev->flags); |
| 2821 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2822 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2823 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2824 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2825 | return len; |
| 2826 | } |
| 2827 | |
| 2828 | |
| 2829 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2830 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2831 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2832 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2833 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2834 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2835 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2839 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2840 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2841 | unsigned long long offset; |
| 2842 | if (strict_strtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2843 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2844 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2845 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2846 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2847 | /* Must set offset before size, so overlap checks |
| 2848 | * can be sane */ |
| 2849 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2850 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2851 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2852 | return len; |
| 2853 | } |
| 2854 | |
| 2855 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2856 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2857 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2858 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2859 | { |
| 2860 | return sprintf(page, "%llu\n", |
| 2861 | (unsigned long long)rdev->new_data_offset); |
| 2862 | } |
| 2863 | |
| 2864 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2865 | const char *buf, size_t len) |
| 2866 | { |
| 2867 | unsigned long long new_offset; |
| 2868 | struct mddev *mddev = rdev->mddev; |
| 2869 | |
| 2870 | if (strict_strtoull(buf, 10, &new_offset) < 0) |
| 2871 | return -EINVAL; |
| 2872 | |
| 2873 | if (mddev->sync_thread) |
| 2874 | return -EBUSY; |
| 2875 | if (new_offset == rdev->data_offset) |
| 2876 | /* reset is always permitted */ |
| 2877 | ; |
| 2878 | else if (new_offset > rdev->data_offset) { |
| 2879 | /* must not push array size beyond rdev_sectors */ |
| 2880 | if (new_offset - rdev->data_offset |
| 2881 | + mddev->dev_sectors > rdev->sectors) |
| 2882 | return -E2BIG; |
| 2883 | } |
| 2884 | /* Metadata worries about other space details. */ |
| 2885 | |
| 2886 | /* decreasing the offset is inconsistent with a backwards |
| 2887 | * reshape. |
| 2888 | */ |
| 2889 | if (new_offset < rdev->data_offset && |
| 2890 | mddev->reshape_backwards) |
| 2891 | return -EINVAL; |
| 2892 | /* Increasing offset is inconsistent with forwards |
| 2893 | * reshape. reshape_direction should be set to |
| 2894 | * 'backwards' first. |
| 2895 | */ |
| 2896 | if (new_offset > rdev->data_offset && |
| 2897 | !mddev->reshape_backwards) |
| 2898 | return -EINVAL; |
| 2899 | |
| 2900 | if (mddev->pers && mddev->persistent && |
| 2901 | !super_types[mddev->major_version] |
| 2902 | .allow_new_offset(rdev, new_offset)) |
| 2903 | return -E2BIG; |
| 2904 | rdev->new_data_offset = new_offset; |
| 2905 | if (new_offset > rdev->data_offset) |
| 2906 | mddev->reshape_backwards = 1; |
| 2907 | else if (new_offset < rdev->data_offset) |
| 2908 | mddev->reshape_backwards = 0; |
| 2909 | |
| 2910 | return len; |
| 2911 | } |
| 2912 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2913 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2914 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2915 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2916 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2917 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2918 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2921 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2922 | { |
| 2923 | /* check if two start/length pairs overlap */ |
| 2924 | if (s1+l1 <= s2) |
| 2925 | return 0; |
| 2926 | if (s2+l2 <= s1) |
| 2927 | return 0; |
| 2928 | return 1; |
| 2929 | } |
| 2930 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2931 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2932 | { |
| 2933 | unsigned long long blocks; |
| 2934 | sector_t new; |
| 2935 | |
| 2936 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2937 | return -EINVAL; |
| 2938 | |
| 2939 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2940 | return -EINVAL; /* sector conversion overflow */ |
| 2941 | |
| 2942 | new = blocks * 2; |
| 2943 | if (new != blocks * 2) |
| 2944 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2945 | |
| 2946 | *sectors = new; |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2950 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2951 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2952 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2953 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2954 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2955 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2956 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2957 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2958 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2959 | if (rdev->data_offset != rdev->new_data_offset) |
| 2960 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2961 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2962 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2963 | sectors = super_types[my_mddev->major_version]. |
| 2964 | rdev_size_change(rdev, sectors); |
| 2965 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2966 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2967 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2968 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2969 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2970 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2971 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2972 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2973 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2974 | rdev->sectors = sectors; |
| 2975 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2976 | /* need to check that all other rdevs with the same ->bdev |
| 2977 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2978 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2979 | * we have to change it back, we will have the lock again. |
| 2980 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2981 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2982 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2983 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2984 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2985 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2986 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2987 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2988 | |
| 2989 | mddev_lock(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2990 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2991 | if (rdev->bdev == rdev2->bdev && |
| 2992 | rdev != rdev2 && |
| 2993 | overlaps(rdev->data_offset, rdev->sectors, |
| 2994 | rdev2->data_offset, |
| 2995 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2996 | overlap = 1; |
| 2997 | break; |
| 2998 | } |
| 2999 | mddev_unlock(mddev); |
| 3000 | if (overlap) { |
| 3001 | mddev_put(mddev); |
| 3002 | break; |
| 3003 | } |
| 3004 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3005 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3006 | if (overlap) { |
| 3007 | /* Someone else could have slipped in a size |
| 3008 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3009 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3010 | * safe, and trust userspace not to race with |
| 3011 | * itself |
| 3012 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3013 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3014 | return -EBUSY; |
| 3015 | } |
| 3016 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3017 | return len; |
| 3018 | } |
| 3019 | |
| 3020 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3021 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3022 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3023 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3024 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3025 | { |
| 3026 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3027 | |
| 3028 | if (test_bit(In_sync, &rdev->flags) || |
| 3029 | recovery_start == MaxSector) |
| 3030 | return sprintf(page, "none\n"); |
| 3031 | |
| 3032 | return sprintf(page, "%llu\n", recovery_start); |
| 3033 | } |
| 3034 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3035 | 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] | 3036 | { |
| 3037 | unsigned long long recovery_start; |
| 3038 | |
| 3039 | if (cmd_match(buf, "none")) |
| 3040 | recovery_start = MaxSector; |
| 3041 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 3042 | return -EINVAL; |
| 3043 | |
| 3044 | if (rdev->mddev->pers && |
| 3045 | rdev->raid_disk >= 0) |
| 3046 | return -EBUSY; |
| 3047 | |
| 3048 | rdev->recovery_offset = recovery_start; |
| 3049 | if (recovery_start == MaxSector) |
| 3050 | set_bit(In_sync, &rdev->flags); |
| 3051 | else |
| 3052 | clear_bit(In_sync, &rdev->flags); |
| 3053 | return len; |
| 3054 | } |
| 3055 | |
| 3056 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3057 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3058 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3059 | |
| 3060 | static ssize_t |
| 3061 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3062 | static ssize_t |
| 3063 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3064 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3065 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3066 | { |
| 3067 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3068 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3069 | 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] | 3070 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3071 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3072 | /* Maybe that ack was all we needed */ |
| 3073 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3074 | wake_up(&rdev->blocked_wait); |
| 3075 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3076 | } |
| 3077 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3078 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3079 | |
| 3080 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3081 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3082 | { |
| 3083 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3084 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3085 | 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] | 3086 | { |
| 3087 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3088 | } |
| 3089 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3090 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3091 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3092 | static struct attribute *rdev_default_attrs[] = { |
| 3093 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3094 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3095 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3096 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3097 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3098 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3099 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3100 | &rdev_bad_blocks.attr, |
| 3101 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3102 | NULL, |
| 3103 | }; |
| 3104 | static ssize_t |
| 3105 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3106 | { |
| 3107 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3108 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3109 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3110 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3111 | |
| 3112 | if (!entry->show) |
| 3113 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3114 | |
| 3115 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3116 | if (!rv) { |
| 3117 | if (rdev->mddev == NULL) |
| 3118 | rv = -EBUSY; |
| 3119 | else |
| 3120 | rv = entry->show(rdev, page); |
| 3121 | mddev_unlock(mddev); |
| 3122 | } |
| 3123 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | static ssize_t |
| 3127 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3128 | const char *page, size_t length) |
| 3129 | { |
| 3130 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3131 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3132 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3133 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3134 | |
| 3135 | if (!entry->store) |
| 3136 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3137 | if (!capable(CAP_SYS_ADMIN)) |
| 3138 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3139 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3140 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3141 | if (rdev->mddev == NULL) |
| 3142 | rv = -EBUSY; |
| 3143 | else |
| 3144 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3145 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3146 | } |
| 3147 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | static void rdev_free(struct kobject *ko) |
| 3151 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3152 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3153 | kfree(rdev); |
| 3154 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3155 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3156 | .show = rdev_attr_show, |
| 3157 | .store = rdev_attr_store, |
| 3158 | }; |
| 3159 | static struct kobj_type rdev_ktype = { |
| 3160 | .release = rdev_free, |
| 3161 | .sysfs_ops = &rdev_sysfs_ops, |
| 3162 | .default_attrs = rdev_default_attrs, |
| 3163 | }; |
| 3164 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3165 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3166 | { |
| 3167 | rdev->desc_nr = -1; |
| 3168 | rdev->saved_raid_disk = -1; |
| 3169 | rdev->raid_disk = -1; |
| 3170 | rdev->flags = 0; |
| 3171 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3172 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3173 | rdev->sb_events = 0; |
| 3174 | rdev->last_read_error.tv_sec = 0; |
| 3175 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3176 | rdev->sb_loaded = 0; |
| 3177 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3178 | atomic_set(&rdev->nr_pending, 0); |
| 3179 | atomic_set(&rdev->read_errors, 0); |
| 3180 | atomic_set(&rdev->corrected_errors, 0); |
| 3181 | |
| 3182 | INIT_LIST_HEAD(&rdev->same_set); |
| 3183 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3184 | |
| 3185 | /* Add space to store bad block list. |
| 3186 | * This reserves the space even on arrays where it cannot |
| 3187 | * be used - I wonder if that matters |
| 3188 | */ |
| 3189 | rdev->badblocks.count = 0; |
| 3190 | rdev->badblocks.shift = 0; |
| 3191 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3192 | seqlock_init(&rdev->badblocks.lock); |
| 3193 | if (rdev->badblocks.page == NULL) |
| 3194 | return -ENOMEM; |
| 3195 | |
| 3196 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3197 | } |
| 3198 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | /* |
| 3200 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3201 | * |
| 3202 | * mark the device faulty if: |
| 3203 | * |
| 3204 | * - the device is nonexistent (zero size) |
| 3205 | * - the device has no valid superblock |
| 3206 | * |
| 3207 | * a faulty rdev _never_ has rdev->sb set. |
| 3208 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3209 | 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] | 3210 | { |
| 3211 | char b[BDEVNAME_SIZE]; |
| 3212 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3213 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | sector_t size; |
| 3215 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3216 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | if (!rdev) { |
| 3218 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3219 | return ERR_PTR(-ENOMEM); |
| 3220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3222 | err = md_rdev_init(rdev); |
| 3223 | if (err) |
| 3224 | goto abort_free; |
| 3225 | err = alloc_disk_sb(rdev); |
| 3226 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | goto abort_free; |
| 3228 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3229 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | if (err) |
| 3231 | goto abort_free; |
| 3232 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3233 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3234 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3235 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | if (!size) { |
| 3237 | printk(KERN_WARNING |
| 3238 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3239 | bdevname(rdev->bdev,b)); |
| 3240 | err = -EINVAL; |
| 3241 | goto abort_free; |
| 3242 | } |
| 3243 | |
| 3244 | if (super_format >= 0) { |
| 3245 | err = super_types[super_format]. |
| 3246 | load_super(rdev, NULL, super_minor); |
| 3247 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3248 | printk(KERN_WARNING |
| 3249 | "md: %s does not have a valid v%d.%d " |
| 3250 | "superblock, not importing!\n", |
| 3251 | bdevname(rdev->bdev,b), |
| 3252 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | goto abort_free; |
| 3254 | } |
| 3255 | if (err < 0) { |
| 3256 | printk(KERN_WARNING |
| 3257 | "md: could not read %s's sb, not importing!\n", |
| 3258 | bdevname(rdev->bdev,b)); |
| 3259 | goto abort_free; |
| 3260 | } |
| 3261 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3262 | if (super_format == -1) |
| 3263 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3264 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | return rdev; |
| 3267 | |
| 3268 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3269 | if (rdev->bdev) |
| 3270 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3271 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | kfree(rdev); |
| 3273 | return ERR_PTR(err); |
| 3274 | } |
| 3275 | |
| 3276 | /* |
| 3277 | * Check a full RAID array for plausibility |
| 3278 | */ |
| 3279 | |
| 3280 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3281 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | { |
| 3283 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3284 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | char b[BDEVNAME_SIZE]; |
| 3286 | |
| 3287 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3288 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | switch (super_types[mddev->major_version]. |
| 3290 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3291 | case 1: |
| 3292 | freshest = rdev; |
| 3293 | break; |
| 3294 | case 0: |
| 3295 | break; |
| 3296 | default: |
| 3297 | printk( KERN_ERR \ |
| 3298 | "md: fatal superblock inconsistency in %s" |
| 3299 | " -- removing from array\n", |
| 3300 | bdevname(rdev->bdev,b)); |
| 3301 | kick_rdev_from_array(rdev); |
| 3302 | } |
| 3303 | |
| 3304 | |
| 3305 | super_types[mddev->major_version]. |
| 3306 | validate_super(mddev, freshest); |
| 3307 | |
| 3308 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3309 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3310 | if (mddev->max_disks && |
| 3311 | (rdev->desc_nr >= mddev->max_disks || |
| 3312 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3313 | printk(KERN_WARNING |
| 3314 | "md: %s: %s: only %d devices permitted\n", |
| 3315 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3316 | mddev->max_disks); |
| 3317 | kick_rdev_from_array(rdev); |
| 3318 | continue; |
| 3319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | if (rdev != freshest) |
| 3321 | if (super_types[mddev->major_version]. |
| 3322 | validate_super(mddev, rdev)) { |
| 3323 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3324 | " from array!\n", |
| 3325 | bdevname(rdev->bdev,b)); |
| 3326 | kick_rdev_from_array(rdev); |
| 3327 | continue; |
| 3328 | } |
| 3329 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3330 | rdev->desc_nr = i++; |
| 3331 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3332 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3333 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3334 | rdev->raid_disk = -1; |
| 3335 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | } |
| 3337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | } |
| 3339 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3340 | /* Read a fixed-point number. |
| 3341 | * Numbers in sysfs attributes should be in "standard" units where |
| 3342 | * possible, so time should be in seconds. |
| 3343 | * However we internally use a a much smaller unit such as |
| 3344 | * milliseconds or jiffies. |
| 3345 | * This function takes a decimal number with a possible fractional |
| 3346 | * component, and produces an integer which is the result of |
| 3347 | * multiplying that number by 10^'scale'. |
| 3348 | * all without any floating-point arithmetic. |
| 3349 | */ |
| 3350 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3351 | { |
| 3352 | unsigned long result = 0; |
| 3353 | long decimals = -1; |
| 3354 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3355 | if (*cp == '.') |
| 3356 | decimals = 0; |
| 3357 | else if (decimals < scale) { |
| 3358 | unsigned int value; |
| 3359 | value = *cp - '0'; |
| 3360 | result = result * 10 + value; |
| 3361 | if (decimals >= 0) |
| 3362 | decimals++; |
| 3363 | } |
| 3364 | cp++; |
| 3365 | } |
| 3366 | if (*cp == '\n') |
| 3367 | cp++; |
| 3368 | if (*cp) |
| 3369 | return -EINVAL; |
| 3370 | if (decimals < 0) |
| 3371 | decimals = 0; |
| 3372 | while (decimals < scale) { |
| 3373 | result *= 10; |
| 3374 | decimals ++; |
| 3375 | } |
| 3376 | *res = result; |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
| 3380 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3381 | static void md_safemode_timeout(unsigned long data); |
| 3382 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3383 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3384 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3385 | { |
| 3386 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3387 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3388 | } |
| 3389 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3390 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3391 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3392 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3393 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3394 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3395 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3396 | if (msec == 0) |
| 3397 | mddev->safemode_delay = 0; |
| 3398 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3399 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3400 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3401 | if (mddev->safemode_delay == 0) |
| 3402 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3403 | if (mddev->safemode_delay < old_delay) |
| 3404 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3405 | } |
| 3406 | return len; |
| 3407 | } |
| 3408 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3409 | __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] | 3410 | |
| 3411 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3412 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3413 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3414 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3415 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3416 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3417 | else if (mddev->clevel[0]) |
| 3418 | return sprintf(page, "%s\n", mddev->clevel); |
| 3419 | else if (mddev->level != LEVEL_NONE) |
| 3420 | return sprintf(page, "%d\n", mddev->level); |
| 3421 | else |
| 3422 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3423 | } |
| 3424 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3425 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3426 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3427 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3428 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3429 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3430 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3431 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3432 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3433 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3434 | |
| 3435 | if (mddev->pers == NULL) { |
| 3436 | if (len == 0) |
| 3437 | return 0; |
| 3438 | if (len >= sizeof(mddev->clevel)) |
| 3439 | return -ENOSPC; |
| 3440 | strncpy(mddev->clevel, buf, len); |
| 3441 | if (mddev->clevel[len-1] == '\n') |
| 3442 | len--; |
| 3443 | mddev->clevel[len] = 0; |
| 3444 | mddev->level = LEVEL_NONE; |
| 3445 | return rv; |
| 3446 | } |
| 3447 | |
| 3448 | /* request to change the personality. Need to ensure: |
| 3449 | * - array is not engaged in resync/recovery/reshape |
| 3450 | * - old personality can be suspended |
| 3451 | * - new personality will access other array. |
| 3452 | */ |
| 3453 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3454 | if (mddev->sync_thread || |
| 3455 | mddev->reshape_position != MaxSector || |
| 3456 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3457 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3458 | |
| 3459 | if (!mddev->pers->quiesce) { |
| 3460 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3461 | mdname(mddev), mddev->pers->name); |
| 3462 | return -EINVAL; |
| 3463 | } |
| 3464 | |
| 3465 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3466 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3467 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3468 | strncpy(clevel, buf, len); |
| 3469 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3470 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3471 | clevel[len] = 0; |
| 3472 | if (strict_strtol(clevel, 10, &level)) |
| 3473 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3474 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3475 | if (request_module("md-%s", clevel) != 0) |
| 3476 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3477 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3478 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3479 | if (!pers || !try_module_get(pers->owner)) { |
| 3480 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3481 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3482 | return -EINVAL; |
| 3483 | } |
| 3484 | spin_unlock(&pers_lock); |
| 3485 | |
| 3486 | if (pers == mddev->pers) { |
| 3487 | /* Nothing to do! */ |
| 3488 | module_put(pers->owner); |
| 3489 | return rv; |
| 3490 | } |
| 3491 | if (!pers->takeover) { |
| 3492 | module_put(pers->owner); |
| 3493 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3494 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3495 | return -EINVAL; |
| 3496 | } |
| 3497 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3498 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3499 | rdev->new_raid_disk = rdev->raid_disk; |
| 3500 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3501 | /* ->takeover must set new_* and/or delta_disks |
| 3502 | * if it succeeds, and may set them when it fails. |
| 3503 | */ |
| 3504 | priv = pers->takeover(mddev); |
| 3505 | if (IS_ERR(priv)) { |
| 3506 | mddev->new_level = mddev->level; |
| 3507 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3508 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3509 | mddev->raid_disks -= mddev->delta_disks; |
| 3510 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3511 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3512 | module_put(pers->owner); |
| 3513 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3514 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3515 | return PTR_ERR(priv); |
| 3516 | } |
| 3517 | |
| 3518 | /* Looks like we have a winner */ |
| 3519 | mddev_suspend(mddev); |
| 3520 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3521 | |
| 3522 | if (mddev->pers->sync_request == NULL && |
| 3523 | pers->sync_request != NULL) { |
| 3524 | /* need to add the md_redundancy_group */ |
| 3525 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3526 | printk(KERN_WARNING |
| 3527 | "md: cannot register extra attributes for %s\n", |
| 3528 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3529 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3530 | } |
| 3531 | if (mddev->pers->sync_request != NULL && |
| 3532 | pers->sync_request == NULL) { |
| 3533 | /* need to remove the md_redundancy_group */ |
| 3534 | if (mddev->to_remove == NULL) |
| 3535 | mddev->to_remove = &md_redundancy_group; |
| 3536 | } |
| 3537 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3538 | if (mddev->pers->sync_request == NULL && |
| 3539 | mddev->external) { |
| 3540 | /* We are converting from a no-redundancy array |
| 3541 | * to a redundancy array and metadata is managed |
| 3542 | * externally so we need to be sure that writes |
| 3543 | * won't block due to a need to transition |
| 3544 | * clean->dirty |
| 3545 | * until external management is started. |
| 3546 | */ |
| 3547 | mddev->in_sync = 0; |
| 3548 | mddev->safemode_delay = 0; |
| 3549 | mddev->safemode = 0; |
| 3550 | } |
| 3551 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3552 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3553 | if (rdev->raid_disk < 0) |
| 3554 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3555 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3556 | rdev->new_raid_disk = -1; |
| 3557 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3558 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3559 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3560 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3561 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3562 | if (rdev->raid_disk < 0) |
| 3563 | continue; |
| 3564 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3565 | continue; |
| 3566 | rdev->raid_disk = rdev->new_raid_disk; |
| 3567 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3568 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3569 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3570 | if (sysfs_link_rdev(mddev, rdev)) |
| 3571 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3572 | " for %s after level change\n", |
| 3573 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3574 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3578 | mddev->pers = pers; |
| 3579 | mddev->private = priv; |
| 3580 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3581 | mddev->level = mddev->new_level; |
| 3582 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3583 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3584 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3585 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3586 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3587 | if (mddev->pers->sync_request == NULL) { |
| 3588 | /* this is now an array without redundancy, so |
| 3589 | * it must always be in_sync |
| 3590 | */ |
| 3591 | mddev->in_sync = 1; |
| 3592 | del_timer_sync(&mddev->safemode_timer); |
| 3593 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3594 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3595 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3596 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3597 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3598 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3599 | return rv; |
| 3600 | } |
| 3601 | |
| 3602 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3603 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3604 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3605 | |
| 3606 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3607 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3608 | { |
| 3609 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3610 | if (mddev->reshape_position != MaxSector && |
| 3611 | mddev->layout != mddev->new_layout) |
| 3612 | return sprintf(page, "%d (%d)\n", |
| 3613 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3614 | return sprintf(page, "%d\n", mddev->layout); |
| 3615 | } |
| 3616 | |
| 3617 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3618 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3619 | { |
| 3620 | char *e; |
| 3621 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3622 | |
| 3623 | if (!*buf || (*e && *e != '\n')) |
| 3624 | return -EINVAL; |
| 3625 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3626 | if (mddev->pers) { |
| 3627 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3628 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3629 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3630 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3631 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3632 | if (err) { |
| 3633 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3634 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3635 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3636 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3637 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3638 | if (mddev->reshape_position == MaxSector) |
| 3639 | mddev->layout = n; |
| 3640 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3641 | return len; |
| 3642 | } |
| 3643 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3644 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3645 | |
| 3646 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3647 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3648 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3649 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3650 | if (mddev->raid_disks == 0) |
| 3651 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3652 | if (mddev->reshape_position != MaxSector && |
| 3653 | mddev->delta_disks != 0) |
| 3654 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3655 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3656 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3657 | } |
| 3658 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3659 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3660 | |
| 3661 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3662 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3663 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3664 | char *e; |
| 3665 | int rv = 0; |
| 3666 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3667 | |
| 3668 | if (!*buf || (*e && *e != '\n')) |
| 3669 | return -EINVAL; |
| 3670 | |
| 3671 | if (mddev->pers) |
| 3672 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3673 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3674 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3675 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3676 | |
| 3677 | rdev_for_each(rdev, mddev) { |
| 3678 | if (olddisks < n && |
| 3679 | rdev->data_offset < rdev->new_data_offset) |
| 3680 | return -EINVAL; |
| 3681 | if (olddisks > n && |
| 3682 | rdev->data_offset > rdev->new_data_offset) |
| 3683 | return -EINVAL; |
| 3684 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3685 | mddev->delta_disks = n - olddisks; |
| 3686 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3687 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3688 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3689 | mddev->raid_disks = n; |
| 3690 | return rv ? rv : len; |
| 3691 | } |
| 3692 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3693 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3694 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3695 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3696 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3697 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3698 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3699 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3700 | return sprintf(page, "%d (%d)\n", |
| 3701 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3702 | mddev->chunk_sectors << 9); |
| 3703 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3707 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3708 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3709 | char *e; |
| 3710 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3711 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3712 | if (!*buf || (*e && *e != '\n')) |
| 3713 | return -EINVAL; |
| 3714 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3715 | if (mddev->pers) { |
| 3716 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3717 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3718 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3719 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3720 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3721 | if (err) { |
| 3722 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3723 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3724 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3725 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3726 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3727 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3728 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3729 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3730 | return len; |
| 3731 | } |
| 3732 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3733 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3734 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3735 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3736 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3737 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3738 | if (mddev->recovery_cp == MaxSector) |
| 3739 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3740 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3741 | } |
| 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3745 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3746 | char *e; |
| 3747 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3748 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3749 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3750 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3751 | if (cmd_match(buf, "none")) |
| 3752 | n = MaxSector; |
| 3753 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3754 | return -EINVAL; |
| 3755 | |
| 3756 | mddev->recovery_cp = n; |
| 3757 | return len; |
| 3758 | } |
| 3759 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3760 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3761 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3762 | /* |
| 3763 | * The array state can be: |
| 3764 | * |
| 3765 | * clear |
| 3766 | * No devices, no size, no level |
| 3767 | * Equivalent to STOP_ARRAY ioctl |
| 3768 | * inactive |
| 3769 | * May have some settings, but array is not active |
| 3770 | * all IO results in error |
| 3771 | * When written, doesn't tear down array, but just stops it |
| 3772 | * suspended (not supported yet) |
| 3773 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3774 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | * readonly |
| 3776 | * no resync can happen. no superblocks get written. |
| 3777 | * write requests fail |
| 3778 | * read-auto |
| 3779 | * like readonly, but behaves like 'clean' on a write request. |
| 3780 | * |
| 3781 | * clean - no pending writes, but otherwise active. |
| 3782 | * When written to inactive array, starts without resync |
| 3783 | * If a write request arrives then |
| 3784 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3785 | * if not known, block and switch to write-pending |
| 3786 | * If written to an active array that has pending writes, then fails. |
| 3787 | * active |
| 3788 | * fully active: IO and resync can be happening. |
| 3789 | * When written to inactive array, starts with resync |
| 3790 | * |
| 3791 | * write-pending |
| 3792 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3793 | * |
| 3794 | * active-idle |
| 3795 | * like active, but no writes have been seen for a while (100msec). |
| 3796 | * |
| 3797 | */ |
| 3798 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3799 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3800 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3801 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3802 | "write-pending", "active-idle", NULL }; |
| 3803 | |
| 3804 | static int match_word(const char *word, char **list) |
| 3805 | { |
| 3806 | int n; |
| 3807 | for (n=0; list[n]; n++) |
| 3808 | if (cmd_match(word, list[n])) |
| 3809 | break; |
| 3810 | return n; |
| 3811 | } |
| 3812 | |
| 3813 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3814 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3815 | { |
| 3816 | enum array_state st = inactive; |
| 3817 | |
| 3818 | if (mddev->pers) |
| 3819 | switch(mddev->ro) { |
| 3820 | case 1: |
| 3821 | st = readonly; |
| 3822 | break; |
| 3823 | case 2: |
| 3824 | st = read_auto; |
| 3825 | break; |
| 3826 | case 0: |
| 3827 | if (mddev->in_sync) |
| 3828 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3829 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3830 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3831 | else if (mddev->safemode) |
| 3832 | st = active_idle; |
| 3833 | else |
| 3834 | st = active; |
| 3835 | } |
| 3836 | else { |
| 3837 | if (list_empty(&mddev->disks) && |
| 3838 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3839 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3840 | st = clear; |
| 3841 | else |
| 3842 | st = inactive; |
| 3843 | } |
| 3844 | return sprintf(page, "%s\n", array_states[st]); |
| 3845 | } |
| 3846 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3847 | static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev); |
| 3848 | static int md_set_readonly(struct mddev * mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3849 | static int do_md_run(struct mddev * mddev); |
| 3850 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3851 | |
| 3852 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3853 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3854 | { |
| 3855 | int err = -EINVAL; |
| 3856 | enum array_state st = match_word(buf, array_states); |
| 3857 | switch(st) { |
| 3858 | case bad_word: |
| 3859 | break; |
| 3860 | case clear: |
| 3861 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3862 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3863 | break; |
| 3864 | case inactive: |
| 3865 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3866 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3867 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3868 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3869 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3870 | break; |
| 3871 | case suspended: |
| 3872 | break; /* not supported yet */ |
| 3873 | case readonly: |
| 3874 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3875 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3876 | else { |
| 3877 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3878 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3879 | err = do_md_run(mddev); |
| 3880 | } |
| 3881 | break; |
| 3882 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3883 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3884 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3885 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3886 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3887 | err = restart_array(mddev); |
| 3888 | if (err == 0) { |
| 3889 | mddev->ro = 2; |
| 3890 | set_disk_ro(mddev->gendisk, 0); |
| 3891 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3892 | } else { |
| 3893 | mddev->ro = 2; |
| 3894 | err = do_md_run(mddev); |
| 3895 | } |
| 3896 | break; |
| 3897 | case clean: |
| 3898 | if (mddev->pers) { |
| 3899 | restart_array(mddev); |
| 3900 | spin_lock_irq(&mddev->write_lock); |
| 3901 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3902 | if (mddev->in_sync == 0) { |
| 3903 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3904 | if (mddev->safemode == 1) |
| 3905 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3906 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3907 | } |
| 3908 | err = 0; |
| 3909 | } else |
| 3910 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3911 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3912 | } else |
| 3913 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3914 | break; |
| 3915 | case active: |
| 3916 | if (mddev->pers) { |
| 3917 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3918 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3919 | wake_up(&mddev->sb_wait); |
| 3920 | err = 0; |
| 3921 | } else { |
| 3922 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3923 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3924 | err = do_md_run(mddev); |
| 3925 | } |
| 3926 | break; |
| 3927 | case write_pending: |
| 3928 | case active_idle: |
| 3929 | /* these cannot be set */ |
| 3930 | break; |
| 3931 | } |
| 3932 | if (err) |
| 3933 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3934 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3935 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3936 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3937 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3938 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3939 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3940 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3941 | static struct md_sysfs_entry md_array_state = |
| 3942 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3943 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3944 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3945 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3946 | return sprintf(page, "%d\n", |
| 3947 | atomic_read(&mddev->max_corr_read_errors)); |
| 3948 | } |
| 3949 | |
| 3950 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3951 | 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] | 3952 | { |
| 3953 | char *e; |
| 3954 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3955 | |
| 3956 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3957 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3958 | return len; |
| 3959 | } |
| 3960 | return -EINVAL; |
| 3961 | } |
| 3962 | |
| 3963 | static struct md_sysfs_entry max_corr_read_errors = |
| 3964 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3965 | max_corrected_read_errors_store); |
| 3966 | |
| 3967 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3968 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3969 | { |
| 3970 | return -EINVAL; |
| 3971 | } |
| 3972 | |
| 3973 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3974 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3975 | { |
| 3976 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3977 | /* The new device is added to the array. |
| 3978 | * If the array has a persistent superblock, we read the |
| 3979 | * superblock to initialise info and check validity. |
| 3980 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3981 | * which mainly checks size. |
| 3982 | */ |
| 3983 | char *e; |
| 3984 | int major = simple_strtoul(buf, &e, 10); |
| 3985 | int minor; |
| 3986 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3987 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3988 | int err; |
| 3989 | |
| 3990 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3991 | return -EINVAL; |
| 3992 | minor = simple_strtoul(e+1, &e, 10); |
| 3993 | if (*e && *e != '\n') |
| 3994 | return -EINVAL; |
| 3995 | dev = MKDEV(major, minor); |
| 3996 | if (major != MAJOR(dev) || |
| 3997 | minor != MINOR(dev)) |
| 3998 | return -EOVERFLOW; |
| 3999 | |
| 4000 | |
| 4001 | if (mddev->persistent) { |
| 4002 | rdev = md_import_device(dev, mddev->major_version, |
| 4003 | mddev->minor_version); |
| 4004 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4005 | struct md_rdev *rdev0 |
| 4006 | = list_entry(mddev->disks.next, |
| 4007 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4008 | err = super_types[mddev->major_version] |
| 4009 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4010 | if (err < 0) |
| 4011 | goto out; |
| 4012 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4013 | } else if (mddev->external) |
| 4014 | rdev = md_import_device(dev, -2, -1); |
| 4015 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4016 | rdev = md_import_device(dev, -1, -1); |
| 4017 | |
| 4018 | if (IS_ERR(rdev)) |
| 4019 | return PTR_ERR(rdev); |
| 4020 | err = bind_rdev_to_array(rdev, mddev); |
| 4021 | out: |
| 4022 | if (err) |
| 4023 | export_rdev(rdev); |
| 4024 | return err ? err : len; |
| 4025 | } |
| 4026 | |
| 4027 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4028 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4029 | |
| 4030 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4031 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4032 | { |
| 4033 | char *end; |
| 4034 | unsigned long chunk, end_chunk; |
| 4035 | |
| 4036 | if (!mddev->bitmap) |
| 4037 | goto out; |
| 4038 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4039 | while (*buf) { |
| 4040 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4041 | if (buf == end) break; |
| 4042 | if (*end == '-') { /* range */ |
| 4043 | buf = end + 1; |
| 4044 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4045 | if (buf == end) break; |
| 4046 | } |
| 4047 | if (*end && !isspace(*end)) break; |
| 4048 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4049 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4050 | } |
| 4051 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4052 | out: |
| 4053 | return len; |
| 4054 | } |
| 4055 | |
| 4056 | static struct md_sysfs_entry md_bitmap = |
| 4057 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4058 | |
| 4059 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4060 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4061 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4062 | return sprintf(page, "%llu\n", |
| 4063 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4064 | } |
| 4065 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4066 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4067 | |
| 4068 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4069 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4070 | { |
| 4071 | /* If array is inactive, we can reduce the component size, but |
| 4072 | * not increase it (except from 0). |
| 4073 | * If array is active, we can try an on-line resize |
| 4074 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4075 | sector_t sectors; |
| 4076 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4077 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4078 | if (err < 0) |
| 4079 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4080 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4081 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4082 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4083 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4084 | if (mddev->dev_sectors == 0 || |
| 4085 | mddev->dev_sectors > sectors) |
| 4086 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4087 | else |
| 4088 | err = -ENOSPC; |
| 4089 | } |
| 4090 | return err ? err : len; |
| 4091 | } |
| 4092 | |
| 4093 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4094 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4095 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4096 | |
| 4097 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4098 | * This is one of |
| 4099 | * 'none' for arrays with no metadata (good luck...) |
| 4100 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4101 | * or N.M for internally known formats |
| 4102 | */ |
| 4103 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4104 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4105 | { |
| 4106 | if (mddev->persistent) |
| 4107 | return sprintf(page, "%d.%d\n", |
| 4108 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4109 | else if (mddev->external) |
| 4110 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4111 | else |
| 4112 | return sprintf(page, "none\n"); |
| 4113 | } |
| 4114 | |
| 4115 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4116 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4117 | { |
| 4118 | int major, minor; |
| 4119 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4120 | /* Changing the details of 'external' metadata is |
| 4121 | * always permitted. Otherwise there must be |
| 4122 | * no devices attached to the array. |
| 4123 | */ |
| 4124 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4125 | ; |
| 4126 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4127 | return -EBUSY; |
| 4128 | |
| 4129 | if (cmd_match(buf, "none")) { |
| 4130 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4131 | mddev->external = 0; |
| 4132 | mddev->major_version = 0; |
| 4133 | mddev->minor_version = 90; |
| 4134 | return len; |
| 4135 | } |
| 4136 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4137 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4138 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4139 | namelen = sizeof(mddev->metadata_type)-1; |
| 4140 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4141 | mddev->metadata_type[namelen] = 0; |
| 4142 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4143 | mddev->metadata_type[--namelen] = 0; |
| 4144 | mddev->persistent = 0; |
| 4145 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4146 | mddev->major_version = 0; |
| 4147 | mddev->minor_version = 90; |
| 4148 | return len; |
| 4149 | } |
| 4150 | major = simple_strtoul(buf, &e, 10); |
| 4151 | if (e==buf || *e != '.') |
| 4152 | return -EINVAL; |
| 4153 | buf = e+1; |
| 4154 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4155 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4156 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4157 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4158 | return -ENOENT; |
| 4159 | mddev->major_version = major; |
| 4160 | mddev->minor_version = minor; |
| 4161 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4162 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4163 | return len; |
| 4164 | } |
| 4165 | |
| 4166 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4167 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4168 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4169 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4170 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4171 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4172 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4173 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4174 | type = "frozen"; |
| 4175 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4176 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4177 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4178 | type = "reshape"; |
| 4179 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4180 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4181 | type = "resync"; |
| 4182 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4183 | type = "check"; |
| 4184 | else |
| 4185 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4186 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4187 | type = "recover"; |
| 4188 | } |
| 4189 | return sprintf(page, "%s\n", type); |
| 4190 | } |
| 4191 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4192 | static void reap_sync_thread(struct mddev *mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4193 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4194 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4195 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4196 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4197 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4198 | return -EINVAL; |
| 4199 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4200 | if (cmd_match(page, "frozen")) |
| 4201 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4202 | else |
| 4203 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4204 | |
| 4205 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4206 | if (mddev->sync_thread) { |
| 4207 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4208 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4209 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4210 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4211 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4212 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4213 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4214 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4215 | else if (cmd_match(page, "recover")) { |
| 4216 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4217 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4218 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4219 | int err; |
| 4220 | if (mddev->pers->start_reshape == NULL) |
| 4221 | return -EINVAL; |
| 4222 | err = mddev->pers->start_reshape(mddev); |
| 4223 | if (err) |
| 4224 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4225 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4226 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4227 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4228 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4229 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4230 | return -EINVAL; |
| 4231 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4232 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4233 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4234 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4235 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4236 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4237 | return len; |
| 4238 | } |
| 4239 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4240 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4241 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4242 | { |
| 4243 | return sprintf(page, "%llu\n", |
| 4244 | (unsigned long long) mddev->resync_mismatches); |
| 4245 | } |
| 4246 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4247 | static struct md_sysfs_entry md_scan_mode = |
| 4248 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4249 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4250 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4251 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4252 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4253 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4254 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4255 | { |
| 4256 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4257 | mddev->sync_speed_min ? "local": "system"); |
| 4258 | } |
| 4259 | |
| 4260 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4261 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4262 | { |
| 4263 | int min; |
| 4264 | char *e; |
| 4265 | if (strncmp(buf, "system", 6)==0) { |
| 4266 | mddev->sync_speed_min = 0; |
| 4267 | return len; |
| 4268 | } |
| 4269 | min = simple_strtoul(buf, &e, 10); |
| 4270 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4271 | return -EINVAL; |
| 4272 | mddev->sync_speed_min = min; |
| 4273 | return len; |
| 4274 | } |
| 4275 | |
| 4276 | static struct md_sysfs_entry md_sync_min = |
| 4277 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4278 | |
| 4279 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4280 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4281 | { |
| 4282 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4283 | mddev->sync_speed_max ? "local": "system"); |
| 4284 | } |
| 4285 | |
| 4286 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4287 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4288 | { |
| 4289 | int max; |
| 4290 | char *e; |
| 4291 | if (strncmp(buf, "system", 6)==0) { |
| 4292 | mddev->sync_speed_max = 0; |
| 4293 | return len; |
| 4294 | } |
| 4295 | max = simple_strtoul(buf, &e, 10); |
| 4296 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4297 | return -EINVAL; |
| 4298 | mddev->sync_speed_max = max; |
| 4299 | return len; |
| 4300 | } |
| 4301 | |
| 4302 | static struct md_sysfs_entry md_sync_max = |
| 4303 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4304 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4305 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4306 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4307 | { |
| 4308 | return sprintf(page, "%d\n", mddev->degraded); |
| 4309 | } |
| 4310 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4311 | |
| 4312 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4313 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4314 | { |
| 4315 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4316 | } |
| 4317 | |
| 4318 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4319 | 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] | 4320 | { |
| 4321 | long n; |
| 4322 | |
| 4323 | if (strict_strtol(buf, 10, &n)) |
| 4324 | return -EINVAL; |
| 4325 | |
| 4326 | if (n != 0 && n != 1) |
| 4327 | return -EINVAL; |
| 4328 | |
| 4329 | mddev->parallel_resync = n; |
| 4330 | |
| 4331 | if (mddev->sync_thread) |
| 4332 | wake_up(&resync_wait); |
| 4333 | |
| 4334 | return len; |
| 4335 | } |
| 4336 | |
| 4337 | /* force parallel resync, even with shared block devices */ |
| 4338 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4339 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4340 | sync_force_parallel_show, sync_force_parallel_store); |
| 4341 | |
| 4342 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4343 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4344 | { |
| 4345 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4346 | if (mddev->curr_resync == 0) |
| 4347 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4348 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4349 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4350 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4351 | db = resync - mddev->resync_mark_cnt; |
| 4352 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4353 | } |
| 4354 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4355 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4356 | |
| 4357 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4358 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4359 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4360 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4361 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4362 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4363 | return sprintf(page, "none\n"); |
| 4364 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4365 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4366 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4367 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4368 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4369 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4370 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4371 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4372 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4373 | } |
| 4374 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4375 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4376 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4377 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4378 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4379 | { |
| 4380 | return sprintf(page, "%llu\n", |
| 4381 | (unsigned long long)mddev->resync_min); |
| 4382 | } |
| 4383 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4384 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4385 | { |
| 4386 | unsigned long long min; |
| 4387 | if (strict_strtoull(buf, 10, &min)) |
| 4388 | return -EINVAL; |
| 4389 | if (min > mddev->resync_max) |
| 4390 | return -EINVAL; |
| 4391 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4392 | return -EBUSY; |
| 4393 | |
| 4394 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4395 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4396 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4397 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4398 | return -EINVAL; |
| 4399 | } |
| 4400 | mddev->resync_min = min; |
| 4401 | |
| 4402 | return len; |
| 4403 | } |
| 4404 | |
| 4405 | static struct md_sysfs_entry md_min_sync = |
| 4406 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4407 | |
| 4408 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4409 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4410 | { |
| 4411 | if (mddev->resync_max == MaxSector) |
| 4412 | return sprintf(page, "max\n"); |
| 4413 | else |
| 4414 | return sprintf(page, "%llu\n", |
| 4415 | (unsigned long long)mddev->resync_max); |
| 4416 | } |
| 4417 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4418 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4419 | { |
| 4420 | if (strncmp(buf, "max", 3) == 0) |
| 4421 | mddev->resync_max = MaxSector; |
| 4422 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4423 | unsigned long long max; |
| 4424 | if (strict_strtoull(buf, 10, &max)) |
| 4425 | return -EINVAL; |
| 4426 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4427 | return -EINVAL; |
| 4428 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4429 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4430 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4431 | return -EBUSY; |
| 4432 | |
| 4433 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4434 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4435 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4436 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4437 | return -EINVAL; |
| 4438 | } |
| 4439 | mddev->resync_max = max; |
| 4440 | } |
| 4441 | wake_up(&mddev->recovery_wait); |
| 4442 | return len; |
| 4443 | } |
| 4444 | |
| 4445 | static struct md_sysfs_entry md_max_sync = |
| 4446 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4447 | |
| 4448 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4449 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4450 | { |
| 4451 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4452 | } |
| 4453 | |
| 4454 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4455 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4456 | { |
| 4457 | char *e; |
| 4458 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4459 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4460 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4461 | if (mddev->pers == NULL || |
| 4462 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4463 | return -EINVAL; |
| 4464 | if (buf == e || (*e && *e != '\n')) |
| 4465 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4466 | |
| 4467 | mddev->suspend_lo = new; |
| 4468 | if (new >= old) |
| 4469 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4470 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4471 | else { |
| 4472 | /* Expanding suspended region - need to wait */ |
| 4473 | mddev->pers->quiesce(mddev, 1); |
| 4474 | mddev->pers->quiesce(mddev, 0); |
| 4475 | } |
| 4476 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4477 | } |
| 4478 | static struct md_sysfs_entry md_suspend_lo = |
| 4479 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4480 | |
| 4481 | |
| 4482 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4483 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4484 | { |
| 4485 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4486 | } |
| 4487 | |
| 4488 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4489 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4490 | { |
| 4491 | char *e; |
| 4492 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4493 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4494 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4495 | if (mddev->pers == NULL || |
| 4496 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4497 | return -EINVAL; |
| 4498 | if (buf == e || (*e && *e != '\n')) |
| 4499 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4500 | |
| 4501 | mddev->suspend_hi = new; |
| 4502 | if (new <= old) |
| 4503 | /* Shrinking suspended region */ |
| 4504 | mddev->pers->quiesce(mddev, 2); |
| 4505 | else { |
| 4506 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4507 | mddev->pers->quiesce(mddev, 1); |
| 4508 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4509 | } |
| 4510 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4511 | } |
| 4512 | static struct md_sysfs_entry md_suspend_hi = |
| 4513 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4514 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4515 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4516 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4517 | { |
| 4518 | if (mddev->reshape_position != MaxSector) |
| 4519 | return sprintf(page, "%llu\n", |
| 4520 | (unsigned long long)mddev->reshape_position); |
| 4521 | strcpy(page, "none\n"); |
| 4522 | return 5; |
| 4523 | } |
| 4524 | |
| 4525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4526 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4527 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4528 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4529 | char *e; |
| 4530 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4531 | if (mddev->pers) |
| 4532 | return -EBUSY; |
| 4533 | if (buf == e || (*e && *e != '\n')) |
| 4534 | return -EINVAL; |
| 4535 | mddev->reshape_position = new; |
| 4536 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4537 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4538 | mddev->new_level = mddev->level; |
| 4539 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4540 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4541 | rdev_for_each(rdev, mddev) |
| 4542 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4543 | return len; |
| 4544 | } |
| 4545 | |
| 4546 | static struct md_sysfs_entry md_reshape_position = |
| 4547 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4548 | reshape_position_store); |
| 4549 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4550 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4551 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4552 | { |
| 4553 | return sprintf(page, "%s\n", |
| 4554 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4555 | } |
| 4556 | |
| 4557 | static ssize_t |
| 4558 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4559 | { |
| 4560 | int backwards = 0; |
| 4561 | if (cmd_match(buf, "forwards")) |
| 4562 | backwards = 0; |
| 4563 | else if (cmd_match(buf, "backwards")) |
| 4564 | backwards = 1; |
| 4565 | else |
| 4566 | return -EINVAL; |
| 4567 | if (mddev->reshape_backwards == backwards) |
| 4568 | return len; |
| 4569 | |
| 4570 | /* check if we are allowed to change */ |
| 4571 | if (mddev->delta_disks) |
| 4572 | return -EBUSY; |
| 4573 | |
| 4574 | if (mddev->persistent && |
| 4575 | mddev->major_version == 0) |
| 4576 | return -EINVAL; |
| 4577 | |
| 4578 | mddev->reshape_backwards = backwards; |
| 4579 | return len; |
| 4580 | } |
| 4581 | |
| 4582 | static struct md_sysfs_entry md_reshape_direction = |
| 4583 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4584 | reshape_direction_store); |
| 4585 | |
| 4586 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4587 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4588 | { |
| 4589 | if (mddev->external_size) |
| 4590 | return sprintf(page, "%llu\n", |
| 4591 | (unsigned long long)mddev->array_sectors/2); |
| 4592 | else |
| 4593 | return sprintf(page, "default\n"); |
| 4594 | } |
| 4595 | |
| 4596 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4597 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4598 | { |
| 4599 | sector_t sectors; |
| 4600 | |
| 4601 | if (strncmp(buf, "default", 7) == 0) { |
| 4602 | if (mddev->pers) |
| 4603 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4604 | else |
| 4605 | sectors = mddev->array_sectors; |
| 4606 | |
| 4607 | mddev->external_size = 0; |
| 4608 | } else { |
| 4609 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4610 | return -EINVAL; |
| 4611 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4612 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4613 | |
| 4614 | mddev->external_size = 1; |
| 4615 | } |
| 4616 | |
| 4617 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4618 | if (mddev->pers) { |
| 4619 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4620 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4621 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4622 | return len; |
| 4623 | } |
| 4624 | |
| 4625 | static struct md_sysfs_entry md_array_size = |
| 4626 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4627 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4628 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4629 | static struct attribute *md_default_attrs[] = { |
| 4630 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4631 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4632 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4633 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4634 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4635 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4636 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4637 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4638 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4639 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4640 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4641 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4642 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4643 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4644 | NULL, |
| 4645 | }; |
| 4646 | |
| 4647 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4648 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4649 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4650 | &md_sync_min.attr, |
| 4651 | &md_sync_max.attr, |
| 4652 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4653 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4654 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4655 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4656 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4657 | &md_suspend_lo.attr, |
| 4658 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4659 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4660 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4661 | NULL, |
| 4662 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4663 | static struct attribute_group md_redundancy_group = { |
| 4664 | .name = NULL, |
| 4665 | .attrs = md_redundancy_attrs, |
| 4666 | }; |
| 4667 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4668 | |
| 4669 | static ssize_t |
| 4670 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4671 | { |
| 4672 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4673 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4674 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4675 | |
| 4676 | if (!entry->show) |
| 4677 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4678 | spin_lock(&all_mddevs_lock); |
| 4679 | if (list_empty(&mddev->all_mddevs)) { |
| 4680 | spin_unlock(&all_mddevs_lock); |
| 4681 | return -EBUSY; |
| 4682 | } |
| 4683 | mddev_get(mddev); |
| 4684 | spin_unlock(&all_mddevs_lock); |
| 4685 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4686 | rv = mddev_lock(mddev); |
| 4687 | if (!rv) { |
| 4688 | rv = entry->show(mddev, page); |
| 4689 | mddev_unlock(mddev); |
| 4690 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4691 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4692 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | static ssize_t |
| 4696 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4697 | const char *page, size_t length) |
| 4698 | { |
| 4699 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4701 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4702 | |
| 4703 | if (!entry->store) |
| 4704 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4705 | if (!capable(CAP_SYS_ADMIN)) |
| 4706 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4707 | spin_lock(&all_mddevs_lock); |
| 4708 | if (list_empty(&mddev->all_mddevs)) { |
| 4709 | spin_unlock(&all_mddevs_lock); |
| 4710 | return -EBUSY; |
| 4711 | } |
| 4712 | mddev_get(mddev); |
| 4713 | spin_unlock(&all_mddevs_lock); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4714 | rv = mddev_lock(mddev); |
| 4715 | if (!rv) { |
| 4716 | rv = entry->store(mddev, page, length); |
| 4717 | mddev_unlock(mddev); |
| 4718 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4719 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4720 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | static void md_free(struct kobject *ko) |
| 4724 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4725 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4726 | |
| 4727 | if (mddev->sysfs_state) |
| 4728 | sysfs_put(mddev->sysfs_state); |
| 4729 | |
| 4730 | if (mddev->gendisk) { |
| 4731 | del_gendisk(mddev->gendisk); |
| 4732 | put_disk(mddev->gendisk); |
| 4733 | } |
| 4734 | if (mddev->queue) |
| 4735 | blk_cleanup_queue(mddev->queue); |
| 4736 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4737 | kfree(mddev); |
| 4738 | } |
| 4739 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4740 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4741 | .show = md_attr_show, |
| 4742 | .store = md_attr_store, |
| 4743 | }; |
| 4744 | static struct kobj_type md_ktype = { |
| 4745 | .release = md_free, |
| 4746 | .sysfs_ops = &md_sysfs_ops, |
| 4747 | .default_attrs = md_default_attrs, |
| 4748 | }; |
| 4749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | int mdp_major = 0; |
| 4751 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4752 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4753 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4754 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4755 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4756 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4757 | kobject_del(&mddev->kobj); |
| 4758 | kobject_put(&mddev->kobj); |
| 4759 | } |
| 4760 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4761 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4763 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4764 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4766 | int partitioned; |
| 4767 | int shift; |
| 4768 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4769 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
| 4771 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4772 | return -ENODEV; |
| 4773 | |
| 4774 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4775 | shift = partitioned ? MdpMinorShift : 0; |
| 4776 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4778 | /* wait for any previous instance of this device to be |
| 4779 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4780 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4781 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4782 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4783 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4784 | error = -EEXIST; |
| 4785 | if (mddev->gendisk) |
| 4786 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4787 | |
| 4788 | if (name) { |
| 4789 | /* Need to ensure that 'name' is not a duplicate. |
| 4790 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4791 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4792 | spin_lock(&all_mddevs_lock); |
| 4793 | |
| 4794 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4795 | if (mddev2->gendisk && |
| 4796 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4797 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4798 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4799 | } |
| 4800 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4802 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4803 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4804 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4805 | if (!mddev->queue) |
| 4806 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4807 | mddev->queue->queuedata = mddev; |
| 4808 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4809 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4810 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | disk = alloc_disk(1 << shift); |
| 4813 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4814 | blk_cleanup_queue(mddev->queue); |
| 4815 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4816 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4818 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4820 | if (name) |
| 4821 | strcpy(disk->disk_name, name); |
| 4822 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4824 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | disk->fops = &md_fops; |
| 4827 | disk->private_data = mddev; |
| 4828 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4829 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4830 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4831 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4832 | * remove it now. |
| 4833 | */ |
| 4834 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4836 | /* As soon as we call add_disk(), another thread could get |
| 4837 | * through to md_open, so make sure it doesn't get too far |
| 4838 | */ |
| 4839 | mutex_lock(&mddev->open_mutex); |
| 4840 | add_disk(disk); |
| 4841 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4842 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4843 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4844 | if (error) { |
| 4845 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4846 | * __must_check, we must do something with the result |
| 4847 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4848 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4849 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4850 | error = 0; |
| 4851 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4852 | if (mddev->kobj.sd && |
| 4853 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4854 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4855 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4856 | abort: |
| 4857 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4858 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4859 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4860 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4861 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4862 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4863 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4867 | { |
| 4868 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | return NULL; |
| 4870 | } |
| 4871 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4872 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4873 | { |
| 4874 | /* val must be "md_*" where * is not all digits. |
| 4875 | * We allocate an array with a large free minor number, and |
| 4876 | * set the name to val. val must not already be an active name. |
| 4877 | */ |
| 4878 | int len = strlen(val); |
| 4879 | char buf[DISK_NAME_LEN]; |
| 4880 | |
| 4881 | while (len && val[len-1] == '\n') |
| 4882 | len--; |
| 4883 | if (len >= DISK_NAME_LEN) |
| 4884 | return -E2BIG; |
| 4885 | strlcpy(buf, val, len+1); |
| 4886 | if (strncmp(buf, "md_", 3) != 0) |
| 4887 | return -EINVAL; |
| 4888 | return md_alloc(0, buf); |
| 4889 | } |
| 4890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | static void md_safemode_timeout(unsigned long data) |
| 4892 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4893 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4895 | if (!atomic_read(&mddev->writes_pending)) { |
| 4896 | mddev->safemode = 1; |
| 4897 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4898 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | md_wakeup_thread(mddev->thread); |
| 4901 | } |
| 4902 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4903 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4905 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4907 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4908 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4909 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4911 | if (list_empty(&mddev->disks)) |
| 4912 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | |
| 4915 | if (mddev->pers) |
| 4916 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4917 | /* Cannot run until previous stop completes properly */ |
| 4918 | if (mddev->sysfs_active) |
| 4919 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | /* |
| 4922 | * Analyze all RAID superblock(s) |
| 4923 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4924 | if (!mddev->raid_disks) { |
| 4925 | if (!mddev->persistent) |
| 4926 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4927 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4928 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4930 | if (mddev->level != LEVEL_NONE) |
| 4931 | request_module("md-level-%d", mddev->level); |
| 4932 | else if (mddev->clevel[0]) |
| 4933 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | |
| 4935 | /* |
| 4936 | * Drop all container device buffers, from now on |
| 4937 | * the only valid external interface is through the md |
| 4938 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4940 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4941 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | continue; |
| 4943 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4944 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4945 | |
| 4946 | /* perform some consistency tests on the device. |
| 4947 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4948 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4949 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4950 | if (rdev->meta_bdev) { |
| 4951 | /* Nothing to check */; |
| 4952 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4953 | if (mddev->dev_sectors && |
| 4954 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4955 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4956 | printk("md: %s: data overlaps metadata\n", |
| 4957 | mdname(mddev)); |
| 4958 | return -EINVAL; |
| 4959 | } |
| 4960 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4961 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4962 | > rdev->data_offset) { |
| 4963 | printk("md: %s: metadata overlaps data\n", |
| 4964 | mdname(mddev)); |
| 4965 | return -EINVAL; |
| 4966 | } |
| 4967 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4968 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | } |
| 4970 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4971 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 4972 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4975 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4976 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4978 | if (mddev->level != LEVEL_NONE) |
| 4979 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4980 | mddev->level); |
| 4981 | else |
| 4982 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4983 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | return -EINVAL; |
| 4985 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4986 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4988 | if (mddev->level != pers->level) { |
| 4989 | mddev->level = pers->level; |
| 4990 | mddev->new_level = pers->level; |
| 4991 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4992 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4994 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4995 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4996 | /* This personality cannot handle reshaping... */ |
| 4997 | mddev->pers = NULL; |
| 4998 | module_put(pers->owner); |
| 4999 | return -EINVAL; |
| 5000 | } |
| 5001 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5002 | if (pers->sync_request) { |
| 5003 | /* Warn if this is a potentially silly |
| 5004 | * configuration. |
| 5005 | */ |
| 5006 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5007 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5008 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5009 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5010 | rdev_for_each(rdev, mddev) |
| 5011 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5012 | if (rdev < rdev2 && |
| 5013 | rdev->bdev->bd_contains == |
| 5014 | rdev2->bdev->bd_contains) { |
| 5015 | printk(KERN_WARNING |
| 5016 | "%s: WARNING: %s appears to be" |
| 5017 | " on the same physical disk as" |
| 5018 | " %s.\n", |
| 5019 | mdname(mddev), |
| 5020 | bdevname(rdev->bdev,b), |
| 5021 | bdevname(rdev2->bdev,b2)); |
| 5022 | warned = 1; |
| 5023 | } |
| 5024 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5025 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5026 | if (warned) |
| 5027 | printk(KERN_WARNING |
| 5028 | "True protection against single-disk" |
| 5029 | " failure might be compromised.\n"); |
| 5030 | } |
| 5031 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5032 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5033 | /* may be over-ridden by personality */ |
| 5034 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5035 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5036 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5038 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5039 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5040 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5041 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5042 | if (err) |
| 5043 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5044 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5045 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5046 | " but 'external_size' not in effect?\n", __func__); |
| 5047 | printk(KERN_ERR |
| 5048 | "md: invalid array_size %llu > default size %llu\n", |
| 5049 | (unsigned long long)mddev->array_sectors / 2, |
| 5050 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5051 | err = -EINVAL; |
| 5052 | mddev->pers->stop(mddev); |
| 5053 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5054 | if (err == 0 && mddev->pers->sync_request && |
| 5055 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5056 | err = bitmap_create(mddev); |
| 5057 | if (err) { |
| 5058 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5059 | mdname(mddev), err); |
| 5060 | mddev->pers->stop(mddev); |
| 5061 | } |
| 5062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | module_put(mddev->pers->owner); |
| 5065 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5066 | bitmap_destroy(mddev); |
| 5067 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5069 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5070 | if (mddev->kobj.sd && |
| 5071 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5072 | printk(KERN_WARNING |
| 5073 | "md: cannot register extra attributes for %s\n", |
| 5074 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5075 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5076 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5077 | mddev->ro = 0; |
| 5078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5080 | atomic_set(&mddev->max_corr_read_errors, |
| 5081 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | mddev->safemode = 0; |
| 5083 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5084 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5085 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5087 | smp_wmb(); |
| 5088 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5089 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5090 | if (rdev->raid_disk >= 0) |
| 5091 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5092 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | |
| 5094 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5095 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5096 | if (mddev->flags) |
| 5097 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5099 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5100 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5101 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5102 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | return 0; |
| 5104 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5105 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5107 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5108 | { |
| 5109 | int err; |
| 5110 | |
| 5111 | err = md_run(mddev); |
| 5112 | if (err) |
| 5113 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5114 | err = bitmap_load(mddev); |
| 5115 | if (err) { |
| 5116 | bitmap_destroy(mddev); |
| 5117 | goto out; |
| 5118 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5119 | |
| 5120 | md_wakeup_thread(mddev->thread); |
| 5121 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5122 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5123 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5124 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5125 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5126 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5127 | out: |
| 5128 | return err; |
| 5129 | } |
| 5130 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5131 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | { |
| 5133 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5134 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5135 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5137 | return -ENXIO; |
| 5138 | if (!mddev->pers) |
| 5139 | return -EINVAL; |
| 5140 | if (!mddev->ro) |
| 5141 | return -EBUSY; |
| 5142 | mddev->safemode = 0; |
| 5143 | mddev->ro = 0; |
| 5144 | set_disk_ro(disk, 0); |
| 5145 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5146 | mdname(mddev)); |
| 5147 | /* Kick recovery or resync if necessary */ |
| 5148 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5149 | md_wakeup_thread(mddev->thread); |
| 5150 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5151 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5152 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5155 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5156 | * to the file ourselves */ |
| 5157 | static int deny_bitmap_write_access(struct file * file) |
| 5158 | { |
| 5159 | struct inode *inode = file->f_mapping->host; |
| 5160 | |
| 5161 | spin_lock(&inode->i_lock); |
| 5162 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5163 | spin_unlock(&inode->i_lock); |
| 5164 | return -ETXTBSY; |
| 5165 | } |
| 5166 | atomic_set(&inode->i_writecount, -1); |
| 5167 | spin_unlock(&inode->i_lock); |
| 5168 | |
| 5169 | return 0; |
| 5170 | } |
| 5171 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5172 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5173 | { |
| 5174 | struct inode *inode = file->f_mapping->host; |
| 5175 | |
| 5176 | spin_lock(&inode->i_lock); |
| 5177 | atomic_set(&inode->i_writecount, 1); |
| 5178 | spin_unlock(&inode->i_lock); |
| 5179 | } |
| 5180 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5181 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5182 | { |
| 5183 | mddev->array_sectors = 0; |
| 5184 | mddev->external_size = 0; |
| 5185 | mddev->dev_sectors = 0; |
| 5186 | mddev->raid_disks = 0; |
| 5187 | mddev->recovery_cp = 0; |
| 5188 | mddev->resync_min = 0; |
| 5189 | mddev->resync_max = MaxSector; |
| 5190 | mddev->reshape_position = MaxSector; |
| 5191 | mddev->external = 0; |
| 5192 | mddev->persistent = 0; |
| 5193 | mddev->level = LEVEL_NONE; |
| 5194 | mddev->clevel[0] = 0; |
| 5195 | mddev->flags = 0; |
| 5196 | mddev->ro = 0; |
| 5197 | mddev->metadata_type[0] = 0; |
| 5198 | mddev->chunk_sectors = 0; |
| 5199 | mddev->ctime = mddev->utime = 0; |
| 5200 | mddev->layout = 0; |
| 5201 | mddev->max_disks = 0; |
| 5202 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5203 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5204 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5205 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5206 | mddev->new_level = LEVEL_NONE; |
| 5207 | mddev->new_layout = 0; |
| 5208 | mddev->new_chunk_sectors = 0; |
| 5209 | mddev->curr_resync = 0; |
| 5210 | mddev->resync_mismatches = 0; |
| 5211 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5212 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5213 | mddev->recovery = 0; |
| 5214 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5215 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5216 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5217 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5218 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5219 | mddev->bitmap_info.offset = 0; |
| 5220 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5221 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5222 | mddev->bitmap_info.chunksize = 0; |
| 5223 | mddev->bitmap_info.daemon_sleep = 0; |
| 5224 | mddev->bitmap_info.max_write_behind = 0; |
| 5225 | } |
| 5226 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5227 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5228 | { |
| 5229 | if (mddev->sync_thread) { |
| 5230 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5231 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5232 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | del_timer_sync(&mddev->safemode_timer); |
| 5236 | |
| 5237 | bitmap_flush(mddev); |
| 5238 | md_super_wait(mddev); |
| 5239 | |
| 5240 | if (!mddev->in_sync || mddev->flags) { |
| 5241 | /* mark array as shutdown cleanly */ |
| 5242 | mddev->in_sync = 1; |
| 5243 | md_update_sb(mddev, 1); |
| 5244 | } |
| 5245 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5246 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5247 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5248 | { |
| 5249 | mddev_lock(mddev); |
| 5250 | __md_stop_writes(mddev); |
| 5251 | mddev_unlock(mddev); |
| 5252 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5253 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5254 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5255 | void md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5256 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5257 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5258 | mddev->pers->stop(mddev); |
| 5259 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5260 | mddev->to_remove = &md_redundancy_group; |
| 5261 | module_put(mddev->pers->owner); |
| 5262 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5263 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5264 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5265 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5266 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5267 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5268 | { |
| 5269 | int err = 0; |
| 5270 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5271 | if (atomic_read(&mddev->openers) > !!bdev) { |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5272 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5273 | err = -EBUSY; |
| 5274 | goto out; |
| 5275 | } |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5276 | if (bdev) |
| 5277 | sync_blockdev(bdev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5278 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5279 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5280 | |
| 5281 | err = -ENXIO; |
| 5282 | if (mddev->ro==1) |
| 5283 | goto out; |
| 5284 | mddev->ro = 1; |
| 5285 | set_disk_ro(mddev->gendisk, 1); |
| 5286 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5287 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5288 | err = 0; |
| 5289 | } |
| 5290 | out: |
| 5291 | mutex_unlock(&mddev->open_mutex); |
| 5292 | return err; |
| 5293 | } |
| 5294 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5295 | /* mode: |
| 5296 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5297 | * 2 - stop but do not disassemble array |
| 5298 | */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5299 | static int do_md_stop(struct mddev * mddev, int mode, |
| 5300 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5303 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5305 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5306 | if (atomic_read(&mddev->openers) > !!bdev || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5307 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5308 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5309 | mutex_unlock(&mddev->open_mutex); |
| 5310 | return -EBUSY; |
| 5311 | } |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5312 | if (bdev) |
| 5313 | /* It is possible IO was issued on some other |
| 5314 | * open file which was closed before we took ->open_mutex. |
| 5315 | * As that was not the last close __blkdev_put will not |
| 5316 | * have called sync_blockdev, so we must. |
| 5317 | */ |
| 5318 | sync_blockdev(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5320 | if (mddev->pers) { |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5321 | if (mddev->ro) |
| 5322 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5323 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5324 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5325 | md_stop(mddev); |
| 5326 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5327 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5328 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5329 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5330 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5331 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5332 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5333 | if (rdev->raid_disk >= 0) |
| 5334 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5335 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5336 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5337 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5338 | mddev->changed = 1; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5339 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5340 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5341 | if (mddev->ro) |
| 5342 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5343 | } else |
| 5344 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | /* |
| 5346 | * Free resources if final stop |
| 5347 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5348 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5350 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5351 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5352 | if (mddev->bitmap_info.file) { |
| 5353 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5354 | fput(mddev->bitmap_info.file); |
| 5355 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5356 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5357 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | export_array(mddev); |
| 5360 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5361 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5362 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5363 | if (mddev->hold_active == UNTIL_STOP) |
| 5364 | mddev->hold_active = 0; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5365 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5366 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5367 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5368 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5369 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | } |
| 5371 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5372 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5373 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5375 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | int err; |
| 5377 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5378 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | |
| 5381 | printk(KERN_INFO "md: running: "); |
| 5382 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5383 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | char b[BDEVNAME_SIZE]; |
| 5385 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5386 | } |
| 5387 | printk("\n"); |
| 5388 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5389 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | if (err) { |
| 5391 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5392 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | /* |
| 5397 | * lets try to run arrays based on all disks that have arrived |
| 5398 | * until now. (those are in pending_raid_disks) |
| 5399 | * |
| 5400 | * the method: pick the first pending disk, collect all disks with |
| 5401 | * the same UUID, remove all from the pending list and put them into |
| 5402 | * the 'same_array' list. Then order this list based on superblock |
| 5403 | * update time (freshest comes first), kick out 'old' disks and |
| 5404 | * compare superblocks. If everything's fine then run it. |
| 5405 | * |
| 5406 | * If "unit" is allocated, then bump its reference count |
| 5407 | */ |
| 5408 | static void autorun_devices(int part) |
| 5409 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5410 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5411 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | char b[BDEVNAME_SIZE]; |
| 5413 | |
| 5414 | printk(KERN_INFO "md: autorun ...\n"); |
| 5415 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5416 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5418 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5420 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | |
| 5422 | printk(KERN_INFO "md: considering %s ...\n", |
| 5423 | bdevname(rdev0->bdev,b)); |
| 5424 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5425 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5427 | printk(KERN_INFO "md: adding %s ...\n", |
| 5428 | bdevname(rdev->bdev,b)); |
| 5429 | list_move(&rdev->same_set, &candidates); |
| 5430 | } |
| 5431 | /* |
| 5432 | * now we have a set of devices, with all of them having |
| 5433 | * mostly sane superblocks. It's time to allocate the |
| 5434 | * mddev. |
| 5435 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5436 | if (part) { |
| 5437 | dev = MKDEV(mdp_major, |
| 5438 | rdev0->preferred_minor << MdpMinorShift); |
| 5439 | unit = MINOR(dev) >> MdpMinorShift; |
| 5440 | } else { |
| 5441 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5442 | unit = MINOR(dev); |
| 5443 | } |
| 5444 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5446 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5447 | break; |
| 5448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | |
| 5450 | md_probe(dev, NULL, NULL); |
| 5451 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5452 | if (!mddev || !mddev->gendisk) { |
| 5453 | if (mddev) |
| 5454 | mddev_put(mddev); |
| 5455 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | "md: cannot allocate memory for md drive.\n"); |
| 5457 | break; |
| 5458 | } |
| 5459 | if (mddev_lock(mddev)) |
| 5460 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5461 | mdname(mddev)); |
| 5462 | else if (mddev->raid_disks || mddev->major_version |
| 5463 | || !list_empty(&mddev->disks)) { |
| 5464 | printk(KERN_WARNING |
| 5465 | "md: %s already running, cannot run %s\n", |
| 5466 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5467 | mddev_unlock(mddev); |
| 5468 | } else { |
| 5469 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5470 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5471 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | list_del_init(&rdev->same_set); |
| 5473 | if (bind_rdev_to_array(rdev, mddev)) |
| 5474 | export_rdev(rdev); |
| 5475 | } |
| 5476 | autorun_array(mddev); |
| 5477 | mddev_unlock(mddev); |
| 5478 | } |
| 5479 | /* on success, candidates will be empty, on error |
| 5480 | * it won't... |
| 5481 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5482 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5483 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | mddev_put(mddev); |
| 5487 | } |
| 5488 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5489 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5490 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | static int get_version(void __user * arg) |
| 5493 | { |
| 5494 | mdu_version_t ver; |
| 5495 | |
| 5496 | ver.major = MD_MAJOR_VERSION; |
| 5497 | ver.minor = MD_MINOR_VERSION; |
| 5498 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5499 | |
| 5500 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5501 | return -EFAULT; |
| 5502 | |
| 5503 | return 0; |
| 5504 | } |
| 5505 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5506 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | { |
| 5508 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5509 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5510 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5512 | nr=working=insync=failed=spare=0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5513 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5515 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | failed++; |
| 5517 | else { |
| 5518 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5519 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5520 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | else |
| 5522 | spare++; |
| 5523 | } |
| 5524 | } |
| 5525 | |
| 5526 | info.major_version = mddev->major_version; |
| 5527 | info.minor_version = mddev->minor_version; |
| 5528 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5529 | info.ctime = mddev->ctime; |
| 5530 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5531 | info.size = mddev->dev_sectors / 2; |
| 5532 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5533 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | info.nr_disks = nr; |
| 5535 | info.raid_disks = mddev->raid_disks; |
| 5536 | info.md_minor = mddev->md_minor; |
| 5537 | info.not_persistent= !mddev->persistent; |
| 5538 | |
| 5539 | info.utime = mddev->utime; |
| 5540 | info.state = 0; |
| 5541 | if (mddev->in_sync) |
| 5542 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5543 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5544 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5545 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | info.working_disks = working; |
| 5547 | info.failed_disks = failed; |
| 5548 | info.spare_disks = spare; |
| 5549 | |
| 5550 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5551 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | |
| 5553 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5554 | return -EFAULT; |
| 5555 | |
| 5556 | return 0; |
| 5557 | } |
| 5558 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5559 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5560 | { |
| 5561 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5562 | char *ptr, *buf = NULL; |
| 5563 | int err = -ENOMEM; |
| 5564 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5565 | if (md_allow_write(mddev)) |
| 5566 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5567 | else |
| 5568 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5569 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5570 | if (!file) |
| 5571 | goto out; |
| 5572 | |
| 5573 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5574 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5575 | file->pathname[0] = '\0'; |
| 5576 | goto copy_out; |
| 5577 | } |
| 5578 | |
| 5579 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5580 | if (!buf) |
| 5581 | goto out; |
| 5582 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5583 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5584 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5585 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5586 | goto out; |
| 5587 | |
| 5588 | strcpy(file->pathname, ptr); |
| 5589 | |
| 5590 | copy_out: |
| 5591 | err = 0; |
| 5592 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5593 | err = -EFAULT; |
| 5594 | out: |
| 5595 | kfree(buf); |
| 5596 | kfree(file); |
| 5597 | return err; |
| 5598 | } |
| 5599 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5600 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | { |
| 5602 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5603 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | |
| 5605 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5606 | return -EFAULT; |
| 5607 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5608 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | if (rdev) { |
| 5610 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5611 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5612 | info.raid_disk = rdev->raid_disk; |
| 5613 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5614 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5616 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5618 | info.state |= (1<<MD_DISK_SYNC); |
| 5619 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5620 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5621 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | } else { |
| 5623 | info.major = info.minor = 0; |
| 5624 | info.raid_disk = -1; |
| 5625 | info.state = (1<<MD_DISK_REMOVED); |
| 5626 | } |
| 5627 | |
| 5628 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5629 | return -EFAULT; |
| 5630 | |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5634 | 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] | 5635 | { |
| 5636 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5637 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | dev_t dev = MKDEV(info->major,info->minor); |
| 5639 | |
| 5640 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5641 | return -EOVERFLOW; |
| 5642 | |
| 5643 | if (!mddev->raid_disks) { |
| 5644 | int err; |
| 5645 | /* expecting a device which has a superblock */ |
| 5646 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5647 | if (IS_ERR(rdev)) { |
| 5648 | printk(KERN_WARNING |
| 5649 | "md: md_import_device returned %ld\n", |
| 5650 | PTR_ERR(rdev)); |
| 5651 | return PTR_ERR(rdev); |
| 5652 | } |
| 5653 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5654 | struct md_rdev *rdev0 |
| 5655 | = list_entry(mddev->disks.next, |
| 5656 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5657 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5659 | if (err < 0) { |
| 5660 | printk(KERN_WARNING |
| 5661 | "md: %s has different UUID to %s\n", |
| 5662 | bdevname(rdev->bdev,b), |
| 5663 | bdevname(rdev0->bdev,b2)); |
| 5664 | export_rdev(rdev); |
| 5665 | return -EINVAL; |
| 5666 | } |
| 5667 | } |
| 5668 | err = bind_rdev_to_array(rdev, mddev); |
| 5669 | if (err) |
| 5670 | export_rdev(rdev); |
| 5671 | return err; |
| 5672 | } |
| 5673 | |
| 5674 | /* |
| 5675 | * add_new_disk can be used once the array is assembled |
| 5676 | * to add "hot spares". They must already have a superblock |
| 5677 | * written |
| 5678 | */ |
| 5679 | if (mddev->pers) { |
| 5680 | int err; |
| 5681 | if (!mddev->pers->hot_add_disk) { |
| 5682 | printk(KERN_WARNING |
| 5683 | "%s: personality does not support diskops!\n", |
| 5684 | mdname(mddev)); |
| 5685 | return -EINVAL; |
| 5686 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5687 | if (mddev->persistent) |
| 5688 | rdev = md_import_device(dev, mddev->major_version, |
| 5689 | mddev->minor_version); |
| 5690 | else |
| 5691 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | if (IS_ERR(rdev)) { |
| 5693 | printk(KERN_WARNING |
| 5694 | "md: md_import_device returned %ld\n", |
| 5695 | PTR_ERR(rdev)); |
| 5696 | return PTR_ERR(rdev); |
| 5697 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5698 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5699 | if (!mddev->persistent) { |
| 5700 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5701 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5702 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5703 | set_bit(In_sync, &rdev->flags); |
| 5704 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5705 | rdev->raid_disk = -1; |
| 5706 | } else |
| 5707 | super_types[mddev->major_version]. |
| 5708 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5709 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5710 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5711 | /* This was a hot-add request, but events doesn't |
| 5712 | * match, so reject it. |
| 5713 | */ |
| 5714 | export_rdev(rdev); |
| 5715 | return -EINVAL; |
| 5716 | } |
| 5717 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5718 | if (test_bit(In_sync, &rdev->flags)) |
| 5719 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5720 | else |
| 5721 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5722 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5723 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5724 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5725 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5726 | else |
| 5727 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | rdev->raid_disk = -1; |
| 5730 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5731 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5732 | /* If there is hot_add_disk but no hot_remove_disk |
| 5733 | * then added disks for geometry changes, |
| 5734 | * and should be added immediately. |
| 5735 | */ |
| 5736 | super_types[mddev->major_version]. |
| 5737 | validate_super(mddev, rdev); |
| 5738 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5739 | if (err) |
| 5740 | unbind_rdev_from_array(rdev); |
| 5741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | if (err) |
| 5743 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5744 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5745 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5746 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5747 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5748 | if (mddev->degraded) |
| 5749 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5750 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5751 | if (!err) |
| 5752 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5753 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | return err; |
| 5755 | } |
| 5756 | |
| 5757 | /* otherwise, add_new_disk is only allowed |
| 5758 | * for major_version==0 superblocks |
| 5759 | */ |
| 5760 | if (mddev->major_version != 0) { |
| 5761 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5762 | mdname(mddev)); |
| 5763 | return -EINVAL; |
| 5764 | } |
| 5765 | |
| 5766 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5767 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5768 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | if (IS_ERR(rdev)) { |
| 5770 | printk(KERN_WARNING |
| 5771 | "md: error, md_import_device() returned %ld\n", |
| 5772 | PTR_ERR(rdev)); |
| 5773 | return PTR_ERR(rdev); |
| 5774 | } |
| 5775 | rdev->desc_nr = info->number; |
| 5776 | if (info->raid_disk < mddev->raid_disks) |
| 5777 | rdev->raid_disk = info->raid_disk; |
| 5778 | else |
| 5779 | rdev->raid_disk = -1; |
| 5780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5782 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5783 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5785 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5786 | set_bit(WriteMostly, &rdev->flags); |
| 5787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | if (!mddev->persistent) { |
| 5789 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5790 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5791 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5792 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5793 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5795 | err = bind_rdev_to_array(rdev, mddev); |
| 5796 | if (err) { |
| 5797 | export_rdev(rdev); |
| 5798 | return err; |
| 5799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | return 0; |
| 5803 | } |
| 5804 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5805 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | { |
| 5807 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5808 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | rdev = find_rdev(mddev, dev); |
| 5811 | if (!rdev) |
| 5812 | return -ENXIO; |
| 5813 | |
| 5814 | if (rdev->raid_disk >= 0) |
| 5815 | goto busy; |
| 5816 | |
| 5817 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5818 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5819 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | |
| 5821 | return 0; |
| 5822 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5823 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5825 | return -EBUSY; |
| 5826 | } |
| 5827 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5828 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | { |
| 5830 | char b[BDEVNAME_SIZE]; |
| 5831 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5832 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | |
| 5834 | if (!mddev->pers) |
| 5835 | return -ENODEV; |
| 5836 | |
| 5837 | if (mddev->major_version != 0) { |
| 5838 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5839 | " version-0 superblocks.\n", |
| 5840 | mdname(mddev)); |
| 5841 | return -EINVAL; |
| 5842 | } |
| 5843 | if (!mddev->pers->hot_add_disk) { |
| 5844 | printk(KERN_WARNING |
| 5845 | "%s: personality does not support diskops!\n", |
| 5846 | mdname(mddev)); |
| 5847 | return -EINVAL; |
| 5848 | } |
| 5849 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5850 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | if (IS_ERR(rdev)) { |
| 5852 | printk(KERN_WARNING |
| 5853 | "md: error, md_import_device() returned %ld\n", |
| 5854 | PTR_ERR(rdev)); |
| 5855 | return -EINVAL; |
| 5856 | } |
| 5857 | |
| 5858 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5859 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5861 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5863 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5865 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | printk(KERN_WARNING |
| 5867 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5868 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5869 | err = -EINVAL; |
| 5870 | goto abort_export; |
| 5871 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5872 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5874 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5875 | err = bind_rdev_to_array(rdev, mddev); |
| 5876 | if (err) |
| 5877 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | |
| 5879 | /* |
| 5880 | * The rest should better be atomic, we can have disk failures |
| 5881 | * noticed in interrupt contexts ... |
| 5882 | */ |
| 5883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | rdev->raid_disk = -1; |
| 5885 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5886 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | |
| 5888 | /* |
| 5889 | * Kick recovery, maybe this spare has to be added to the |
| 5890 | * array immediately. |
| 5891 | */ |
| 5892 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5893 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5894 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | return 0; |
| 5896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | abort_export: |
| 5898 | export_rdev(rdev); |
| 5899 | return err; |
| 5900 | } |
| 5901 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5902 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5903 | { |
| 5904 | int err; |
| 5905 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5906 | if (mddev->pers) { |
| 5907 | if (!mddev->pers->quiesce) |
| 5908 | return -EBUSY; |
| 5909 | if (mddev->recovery || mddev->sync_thread) |
| 5910 | return -EBUSY; |
| 5911 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5914 | |
| 5915 | if (fd >= 0) { |
| 5916 | if (mddev->bitmap) |
| 5917 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5918 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5919 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5920 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5921 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5922 | mdname(mddev)); |
| 5923 | return -EBADF; |
| 5924 | } |
| 5925 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5926 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5927 | if (err) { |
| 5928 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5929 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5930 | fput(mddev->bitmap_info.file); |
| 5931 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5932 | return err; |
| 5933 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5934 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5935 | } else if (mddev->bitmap == NULL) |
| 5936 | return -ENOENT; /* cannot remove what isn't there */ |
| 5937 | err = 0; |
| 5938 | if (mddev->pers) { |
| 5939 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5940 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5941 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5942 | if (!err) |
| 5943 | err = bitmap_load(mddev); |
| 5944 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5945 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5946 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5947 | fd = -1; /* make sure to put the file */ |
| 5948 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5949 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5950 | } |
| 5951 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5952 | if (mddev->bitmap_info.file) { |
| 5953 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5954 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5955 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5956 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5957 | } |
| 5958 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5959 | return err; |
| 5960 | } |
| 5961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | /* |
| 5963 | * set_array_info is used two different ways |
| 5964 | * The original usage is when creating a new array. |
| 5965 | * In this usage, raid_disks is > 0 and it together with |
| 5966 | * level, size, not_persistent,layout,chunksize determine the |
| 5967 | * shape of the array. |
| 5968 | * This will always create an array with a type-0.90.0 superblock. |
| 5969 | * The newer usage is when assembling an array. |
| 5970 | * In this case raid_disks will be 0, and the major_version field is |
| 5971 | * use to determine which style super-blocks are to be found on the devices. |
| 5972 | * The minor and patch _version numbers are also kept incase the |
| 5973 | * super_block handler wishes to interpret them. |
| 5974 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5975 | 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] | 5976 | { |
| 5977 | |
| 5978 | if (info->raid_disks == 0) { |
| 5979 | /* just setting version number for superblock loading */ |
| 5980 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5981 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | super_types[info->major_version].name == NULL) { |
| 5983 | /* maybe try to auto-load a module? */ |
| 5984 | printk(KERN_INFO |
| 5985 | "md: superblock version %d not known\n", |
| 5986 | info->major_version); |
| 5987 | return -EINVAL; |
| 5988 | } |
| 5989 | mddev->major_version = info->major_version; |
| 5990 | mddev->minor_version = info->minor_version; |
| 5991 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5992 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5993 | /* ensure mddev_put doesn't delete this now that there |
| 5994 | * is some minimal configuration. |
| 5995 | */ |
| 5996 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | return 0; |
| 5998 | } |
| 5999 | mddev->major_version = MD_MAJOR_VERSION; |
| 6000 | mddev->minor_version = MD_MINOR_VERSION; |
| 6001 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6002 | mddev->ctime = get_seconds(); |
| 6003 | |
| 6004 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6005 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6006 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | mddev->raid_disks = info->raid_disks; |
| 6008 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6009 | * openned |
| 6010 | */ |
| 6011 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6012 | mddev->recovery_cp = MaxSector; |
| 6013 | else |
| 6014 | mddev->recovery_cp = 0; |
| 6015 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6016 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | |
| 6018 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6019 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
| 6021 | mddev->max_disks = MD_SB_DISKS; |
| 6022 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6023 | if (mddev->persistent) |
| 6024 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6025 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6027 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6028 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6029 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6030 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6031 | mddev->reshape_position = MaxSector; |
| 6032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | /* |
| 6034 | * Generate a 128 bit UUID |
| 6035 | */ |
| 6036 | get_random_bytes(mddev->uuid, 16); |
| 6037 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6038 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6039 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6040 | mddev->new_layout = mddev->layout; |
| 6041 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6042 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | return 0; |
| 6045 | } |
| 6046 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6047 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6048 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6049 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6050 | |
| 6051 | if (mddev->external_size) |
| 6052 | return; |
| 6053 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6054 | mddev->array_sectors = array_sectors; |
| 6055 | } |
| 6056 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6057 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6058 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6059 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6060 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6061 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6062 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6063 | |
| 6064 | if (mddev->pers->resize == NULL) |
| 6065 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6066 | /* The "num_sectors" is the number of sectors of each device that |
| 6067 | * is used. This can only make sense for arrays with redundancy. |
| 6068 | * linear and raid0 always use whatever space is available. We can only |
| 6069 | * consider changing this number if no resync or reconstruction is |
| 6070 | * 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] | 6071 | * 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] | 6072 | * of each device. If num_sectors is zero, we find the largest size |
| 6073 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6074 | */ |
| 6075 | if (mddev->sync_thread) |
| 6076 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6077 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6078 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6079 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6080 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6081 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6082 | num_sectors = avail; |
| 6083 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6084 | return -ENOSPC; |
| 6085 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6086 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6087 | if (!rv) |
| 6088 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6089 | return rv; |
| 6090 | } |
| 6091 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6092 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6093 | { |
| 6094 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6095 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6096 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6097 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6098 | return -EINVAL; |
| 6099 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6100 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6101 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6102 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6103 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6104 | |
| 6105 | rdev_for_each(rdev, mddev) { |
| 6106 | if (mddev->raid_disks < raid_disks && |
| 6107 | rdev->data_offset < rdev->new_data_offset) |
| 6108 | return -EINVAL; |
| 6109 | if (mddev->raid_disks > raid_disks && |
| 6110 | rdev->data_offset > rdev->new_data_offset) |
| 6111 | return -EINVAL; |
| 6112 | } |
| 6113 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6114 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6115 | if (mddev->delta_disks < 0) |
| 6116 | mddev->reshape_backwards = 1; |
| 6117 | else if (mddev->delta_disks > 0) |
| 6118 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6119 | |
| 6120 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6121 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6122 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6123 | mddev->reshape_backwards = 0; |
| 6124 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6125 | return rv; |
| 6126 | } |
| 6127 | |
| 6128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | /* |
| 6130 | * update_array_info is used to change the configuration of an |
| 6131 | * on-line array. |
| 6132 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6133 | * fields in the info are checked against the array. |
| 6134 | * Any differences that cannot be handled will cause an error. |
| 6135 | * Normally, only one change can be managed at a time. |
| 6136 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6137 | 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] | 6138 | { |
| 6139 | int rv = 0; |
| 6140 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6141 | int state = 0; |
| 6142 | |
| 6143 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6144 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6145 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | |
| 6147 | if (mddev->major_version != info->major_version || |
| 6148 | mddev->minor_version != info->minor_version || |
| 6149 | /* mddev->patch_version != info->patch_version || */ |
| 6150 | mddev->ctime != info->ctime || |
| 6151 | mddev->level != info->level || |
| 6152 | /* mddev->layout != info->layout || */ |
| 6153 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6154 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6155 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6156 | ((state^info->state) & 0xfffffe00) |
| 6157 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | return -EINVAL; |
| 6159 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6160 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6161 | cnt++; |
| 6162 | if (mddev->raid_disks != info->raid_disks) |
| 6163 | cnt++; |
| 6164 | if (mddev->layout != info->layout) |
| 6165 | cnt++; |
| 6166 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6167 | cnt++; |
| 6168 | if (cnt == 0) |
| 6169 | return 0; |
| 6170 | if (cnt > 1) |
| 6171 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | |
| 6173 | if (mddev->layout != info->layout) { |
| 6174 | /* Change layout |
| 6175 | * we don't need to do anything at the md level, the |
| 6176 | * personality will take care of it all. |
| 6177 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6178 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6180 | else { |
| 6181 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6182 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6183 | if (rv) |
| 6184 | mddev->new_layout = mddev->layout; |
| 6185 | return rv; |
| 6186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6188 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6189 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6191 | if (mddev->raid_disks != info->raid_disks) |
| 6192 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6194 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6195 | if (mddev->pers->quiesce == NULL) |
| 6196 | return -EINVAL; |
| 6197 | if (mddev->recovery || mddev->sync_thread) |
| 6198 | return -EBUSY; |
| 6199 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6200 | /* add the bitmap */ |
| 6201 | if (mddev->bitmap) |
| 6202 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6203 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6204 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6205 | mddev->bitmap_info.offset = |
| 6206 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6207 | mddev->bitmap_info.space = |
| 6208 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6209 | mddev->pers->quiesce(mddev, 1); |
| 6210 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6211 | if (!rv) |
| 6212 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6213 | if (rv) |
| 6214 | bitmap_destroy(mddev); |
| 6215 | mddev->pers->quiesce(mddev, 0); |
| 6216 | } else { |
| 6217 | /* remove the bitmap */ |
| 6218 | if (!mddev->bitmap) |
| 6219 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6220 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6221 | return -EINVAL; |
| 6222 | mddev->pers->quiesce(mddev, 1); |
| 6223 | bitmap_destroy(mddev); |
| 6224 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6225 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6226 | } |
| 6227 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6228 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | return rv; |
| 6230 | } |
| 6231 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6232 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6234 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | |
| 6236 | if (mddev->pers == NULL) |
| 6237 | return -ENODEV; |
| 6238 | |
| 6239 | rdev = find_rdev(mddev, dev); |
| 6240 | if (!rdev) |
| 6241 | return -ENODEV; |
| 6242 | |
| 6243 | md_error(mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 6244 | if (!test_bit(Faulty, &rdev->flags)) |
| 6245 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | return 0; |
| 6247 | } |
| 6248 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6249 | /* |
| 6250 | * We have a problem here : there is no easy way to give a CHS |
| 6251 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6252 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6253 | * dosfs just mad... ;-) |
| 6254 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6255 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6256 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6257 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6258 | |
| 6259 | geo->heads = 2; |
| 6260 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6261 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6262 | return 0; |
| 6263 | } |
| 6264 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6265 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | unsigned int cmd, unsigned long arg) |
| 6267 | { |
| 6268 | int err = 0; |
| 6269 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6270 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6271 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6273 | switch (cmd) { |
| 6274 | case RAID_VERSION: |
| 6275 | case GET_ARRAY_INFO: |
| 6276 | case GET_DISK_INFO: |
| 6277 | break; |
| 6278 | default: |
| 6279 | if (!capable(CAP_SYS_ADMIN)) |
| 6280 | return -EACCES; |
| 6281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | |
| 6283 | /* |
| 6284 | * Commands dealing with the RAID driver but not any |
| 6285 | * particular array: |
| 6286 | */ |
| 6287 | switch (cmd) |
| 6288 | { |
| 6289 | case RAID_VERSION: |
| 6290 | err = get_version(argp); |
| 6291 | goto done; |
| 6292 | |
| 6293 | case PRINT_RAID_DEBUG: |
| 6294 | err = 0; |
| 6295 | md_print_devices(); |
| 6296 | goto done; |
| 6297 | |
| 6298 | #ifndef MODULE |
| 6299 | case RAID_AUTORUN: |
| 6300 | err = 0; |
| 6301 | autostart_arrays(arg); |
| 6302 | goto done; |
| 6303 | #endif |
| 6304 | default:; |
| 6305 | } |
| 6306 | |
| 6307 | /* |
| 6308 | * Commands creating/starting a new array: |
| 6309 | */ |
| 6310 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6311 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | |
| 6313 | if (!mddev) { |
| 6314 | BUG(); |
| 6315 | goto abort; |
| 6316 | } |
| 6317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | err = mddev_lock(mddev); |
| 6319 | if (err) { |
| 6320 | printk(KERN_INFO |
| 6321 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6322 | err, cmd); |
| 6323 | goto abort; |
| 6324 | } |
| 6325 | |
| 6326 | switch (cmd) |
| 6327 | { |
| 6328 | case SET_ARRAY_INFO: |
| 6329 | { |
| 6330 | mdu_array_info_t info; |
| 6331 | if (!arg) |
| 6332 | memset(&info, 0, sizeof(info)); |
| 6333 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6334 | err = -EFAULT; |
| 6335 | goto abort_unlock; |
| 6336 | } |
| 6337 | if (mddev->pers) { |
| 6338 | err = update_array_info(mddev, &info); |
| 6339 | if (err) { |
| 6340 | printk(KERN_WARNING "md: couldn't update" |
| 6341 | " array info. %d\n", err); |
| 6342 | goto abort_unlock; |
| 6343 | } |
| 6344 | goto done_unlock; |
| 6345 | } |
| 6346 | if (!list_empty(&mddev->disks)) { |
| 6347 | printk(KERN_WARNING |
| 6348 | "md: array %s already has disks!\n", |
| 6349 | mdname(mddev)); |
| 6350 | err = -EBUSY; |
| 6351 | goto abort_unlock; |
| 6352 | } |
| 6353 | if (mddev->raid_disks) { |
| 6354 | printk(KERN_WARNING |
| 6355 | "md: array %s already initialised!\n", |
| 6356 | mdname(mddev)); |
| 6357 | err = -EBUSY; |
| 6358 | goto abort_unlock; |
| 6359 | } |
| 6360 | err = set_array_info(mddev, &info); |
| 6361 | if (err) { |
| 6362 | printk(KERN_WARNING "md: couldn't set" |
| 6363 | " array info. %d\n", err); |
| 6364 | goto abort_unlock; |
| 6365 | } |
| 6366 | } |
| 6367 | goto done_unlock; |
| 6368 | |
| 6369 | default:; |
| 6370 | } |
| 6371 | |
| 6372 | /* |
| 6373 | * Commands querying/configuring an existing array: |
| 6374 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6375 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6376 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6377 | if ((!mddev->raid_disks && !mddev->external) |
| 6378 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6379 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6380 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | err = -ENODEV; |
| 6382 | goto abort_unlock; |
| 6383 | } |
| 6384 | |
| 6385 | /* |
| 6386 | * Commands even a read-only array can execute: |
| 6387 | */ |
| 6388 | switch (cmd) |
| 6389 | { |
| 6390 | case GET_ARRAY_INFO: |
| 6391 | err = get_array_info(mddev, argp); |
| 6392 | goto done_unlock; |
| 6393 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6394 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6395 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6396 | goto done_unlock; |
| 6397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | case GET_DISK_INFO: |
| 6399 | err = get_disk_info(mddev, argp); |
| 6400 | goto done_unlock; |
| 6401 | |
| 6402 | case RESTART_ARRAY_RW: |
| 6403 | err = restart_array(mddev); |
| 6404 | goto done_unlock; |
| 6405 | |
| 6406 | case STOP_ARRAY: |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6407 | err = do_md_stop(mddev, 0, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | goto done_unlock; |
| 6409 | |
| 6410 | case STOP_ARRAY_RO: |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6411 | err = md_set_readonly(mddev, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | goto done_unlock; |
| 6413 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6414 | case BLKROSET: |
| 6415 | if (get_user(ro, (int __user *)(arg))) { |
| 6416 | err = -EFAULT; |
| 6417 | goto done_unlock; |
| 6418 | } |
| 6419 | err = -EINVAL; |
| 6420 | |
| 6421 | /* if the bdev is going readonly the value of mddev->ro |
| 6422 | * does not matter, no writes are coming |
| 6423 | */ |
| 6424 | if (ro) |
| 6425 | goto done_unlock; |
| 6426 | |
| 6427 | /* are we are already prepared for writes? */ |
| 6428 | if (mddev->ro != 1) |
| 6429 | goto done_unlock; |
| 6430 | |
| 6431 | /* transitioning to readauto need only happen for |
| 6432 | * arrays that call md_write_start |
| 6433 | */ |
| 6434 | if (mddev->pers) { |
| 6435 | err = restart_array(mddev); |
| 6436 | if (err == 0) { |
| 6437 | mddev->ro = 2; |
| 6438 | set_disk_ro(mddev->gendisk, 0); |
| 6439 | } |
| 6440 | } |
| 6441 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | /* |
| 6445 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6446 | * superblock, so we do not allow them on read-only arrays. |
| 6447 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6448 | * here and hit the 'default' below, so only disallow |
| 6449 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6451 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6452 | if (mddev->ro == 2) { |
| 6453 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6454 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6455 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6456 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6457 | } else { |
| 6458 | err = -EROFS; |
| 6459 | goto abort_unlock; |
| 6460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | switch (cmd) |
| 6464 | { |
| 6465 | case ADD_NEW_DISK: |
| 6466 | { |
| 6467 | mdu_disk_info_t info; |
| 6468 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6469 | err = -EFAULT; |
| 6470 | else |
| 6471 | err = add_new_disk(mddev, &info); |
| 6472 | goto done_unlock; |
| 6473 | } |
| 6474 | |
| 6475 | case HOT_REMOVE_DISK: |
| 6476 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6477 | goto done_unlock; |
| 6478 | |
| 6479 | case HOT_ADD_DISK: |
| 6480 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6481 | goto done_unlock; |
| 6482 | |
| 6483 | case SET_DISK_FAULTY: |
| 6484 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6485 | goto done_unlock; |
| 6486 | |
| 6487 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6488 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | goto done_unlock; |
| 6490 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6491 | case SET_BITMAP_FILE: |
| 6492 | err = set_bitmap_file(mddev, (int)arg); |
| 6493 | goto done_unlock; |
| 6494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | err = -EINVAL; |
| 6497 | goto abort_unlock; |
| 6498 | } |
| 6499 | |
| 6500 | done_unlock: |
| 6501 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6502 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6503 | err != -EINVAL) |
| 6504 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | mddev_unlock(mddev); |
| 6506 | |
| 6507 | return err; |
| 6508 | done: |
| 6509 | if (err) |
| 6510 | MD_BUG(); |
| 6511 | abort: |
| 6512 | return err; |
| 6513 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6514 | #ifdef CONFIG_COMPAT |
| 6515 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6516 | unsigned int cmd, unsigned long arg) |
| 6517 | { |
| 6518 | switch (cmd) { |
| 6519 | case HOT_REMOVE_DISK: |
| 6520 | case HOT_ADD_DISK: |
| 6521 | case SET_DISK_FAULTY: |
| 6522 | case SET_BITMAP_FILE: |
| 6523 | /* These take in integer arg, do not convert */ |
| 6524 | break; |
| 6525 | default: |
| 6526 | arg = (unsigned long)compat_ptr(arg); |
| 6527 | break; |
| 6528 | } |
| 6529 | |
| 6530 | return md_ioctl(bdev, mode, cmd, arg); |
| 6531 | } |
| 6532 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6534 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | { |
| 6536 | /* |
| 6537 | * Succeed if we can lock the mddev, which confirms that |
| 6538 | * it isn't being stopped right now. |
| 6539 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6540 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | int err; |
| 6542 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6543 | if (!mddev) |
| 6544 | return -ENODEV; |
| 6545 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6546 | if (mddev->gendisk != bdev->bd_disk) { |
| 6547 | /* we are racing with mddev_put which is discarding this |
| 6548 | * bd_disk. |
| 6549 | */ |
| 6550 | mddev_put(mddev); |
| 6551 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6552 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6553 | /* Then retry the open from the top */ |
| 6554 | return -ERESTARTSYS; |
| 6555 | } |
| 6556 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6557 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6558 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | goto out; |
| 6560 | |
| 6561 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6562 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6563 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6565 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | out: |
| 6567 | return err; |
| 6568 | } |
| 6569 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6570 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6571 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6572 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6574 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6575 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | mddev_put(mddev); |
| 6577 | |
| 6578 | return 0; |
| 6579 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6580 | |
| 6581 | static int md_media_changed(struct gendisk *disk) |
| 6582 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6583 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6584 | |
| 6585 | return mddev->changed; |
| 6586 | } |
| 6587 | |
| 6588 | static int md_revalidate(struct gendisk *disk) |
| 6589 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6590 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6591 | |
| 6592 | mddev->changed = 0; |
| 6593 | return 0; |
| 6594 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6595 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | { |
| 6597 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6598 | .open = md_open, |
| 6599 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6600 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6601 | #ifdef CONFIG_COMPAT |
| 6602 | .compat_ioctl = md_compat_ioctl, |
| 6603 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6604 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6605 | .media_changed = md_media_changed, |
| 6606 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | }; |
| 6608 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6609 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6611 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | /* |
| 6614 | * md_thread is a 'system-thread', it's priority should be very |
| 6615 | * high. We avoid resource deadlocks individually in each |
| 6616 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6617 | * the very same RT priority as kswapd, thus we will never get |
| 6618 | * into a priority inversion deadlock. |
| 6619 | * |
| 6620 | * we definitely have to have equal or higher priority than |
| 6621 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6622 | * many dirty RAID5 blocks. |
| 6623 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6625 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6626 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6628 | /* We need to wait INTERRUPTIBLE so that |
| 6629 | * we don't add to the load-average. |
| 6630 | * That means we need to be sure no signals are |
| 6631 | * pending |
| 6632 | */ |
| 6633 | if (signal_pending(current)) |
| 6634 | flush_signals(current); |
| 6635 | |
| 6636 | wait_event_interruptible_timeout |
| 6637 | (thread->wqueue, |
| 6638 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6639 | || kthread_should_stop(), |
| 6640 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6642 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6643 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6644 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | return 0; |
| 6648 | } |
| 6649 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6650 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | { |
| 6652 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6653 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6655 | wake_up(&thread->wqueue); |
| 6656 | } |
| 6657 | } |
| 6658 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6659 | struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | const char *name) |
| 6661 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6662 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6664 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | if (!thread) |
| 6666 | return NULL; |
| 6667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | init_waitqueue_head(&thread->wqueue); |
| 6669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | thread->run = run; |
| 6671 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6672 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6673 | thread->tsk = kthread_run(md_thread, thread, |
| 6674 | "%s_%s", |
| 6675 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6676 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6677 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | kfree(thread); |
| 6679 | return NULL; |
| 6680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | return thread; |
| 6682 | } |
| 6683 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6684 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6686 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6687 | if (!thread) |
| 6688 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6689 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6690 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6691 | * non-existent thread |
| 6692 | */ |
| 6693 | spin_lock(&pers_lock); |
| 6694 | *threadp = NULL; |
| 6695 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6696 | |
| 6697 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | kfree(thread); |
| 6699 | } |
| 6700 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6701 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | { |
| 6703 | if (!mddev) { |
| 6704 | MD_BUG(); |
| 6705 | return; |
| 6706 | } |
| 6707 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6708 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6710 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6711 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | return; |
| 6713 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6714 | if (mddev->degraded) |
| 6715 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6716 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6718 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6719 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6720 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6721 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6722 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | } |
| 6724 | |
| 6725 | /* seq_file implementation /proc/mdstat */ |
| 6726 | |
| 6727 | static void status_unused(struct seq_file *seq) |
| 6728 | { |
| 6729 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6730 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | |
| 6732 | seq_printf(seq, "unused devices: "); |
| 6733 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6734 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | char b[BDEVNAME_SIZE]; |
| 6736 | i++; |
| 6737 | seq_printf(seq, "%s ", |
| 6738 | bdevname(rdev->bdev,b)); |
| 6739 | } |
| 6740 | if (!i) |
| 6741 | seq_printf(seq, "<none>"); |
| 6742 | |
| 6743 | seq_printf(seq, "\n"); |
| 6744 | } |
| 6745 | |
| 6746 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6747 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6749 | sector_t max_sectors, resync, res; |
| 6750 | unsigned long dt, db; |
| 6751 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6752 | int scale; |
| 6753 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6755 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6757 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6758 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6759 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6761 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | |
| 6763 | /* |
| 6764 | * Should not happen. |
| 6765 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6766 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | MD_BUG(); |
| 6768 | return; |
| 6769 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6770 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6771 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6772 | * u32, as those are the requirements for sector_div. |
| 6773 | * Thus 'scale' must be at least 10 |
| 6774 | */ |
| 6775 | scale = 10; |
| 6776 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6777 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6778 | scale++; |
| 6779 | } |
| 6780 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6781 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6782 | |
| 6783 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6785 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | seq_printf(seq, "["); |
| 6787 | for (i = 0; i < x; i++) |
| 6788 | seq_printf(seq, "="); |
| 6789 | seq_printf(seq, ">"); |
| 6790 | for (i = 0; i < y; i++) |
| 6791 | seq_printf(seq, "."); |
| 6792 | seq_printf(seq, "] "); |
| 6793 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6794 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6795 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6796 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6797 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6798 | "check" : |
| 6799 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6800 | "resync" : "recovery"))), |
| 6801 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6802 | (unsigned long long) resync/2, |
| 6803 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | |
| 6805 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | * dt: time from mark until now |
| 6807 | * db: blocks written from mark until now |
| 6808 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6809 | * |
| 6810 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6811 | * So we divide before multiply in case it is 32bit and close |
| 6812 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6813 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6814 | * near the end of resync when the number of remaining sectors |
| 6815 | * is close to 'db'. |
| 6816 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6817 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | */ |
| 6819 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6820 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6821 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6822 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6824 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6825 | sector_div(rt, db/32+1); |
| 6826 | rt *= dt; |
| 6827 | rt >>= 5; |
| 6828 | |
| 6829 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6830 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6832 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6836 | { |
| 6837 | struct list_head *tmp; |
| 6838 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6839 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | |
| 6841 | if (l >= 0x10000) |
| 6842 | return NULL; |
| 6843 | if (!l--) |
| 6844 | /* header */ |
| 6845 | return (void*)1; |
| 6846 | |
| 6847 | spin_lock(&all_mddevs_lock); |
| 6848 | list_for_each(tmp,&all_mddevs) |
| 6849 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6850 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | mddev_get(mddev); |
| 6852 | spin_unlock(&all_mddevs_lock); |
| 6853 | return mddev; |
| 6854 | } |
| 6855 | spin_unlock(&all_mddevs_lock); |
| 6856 | if (!l--) |
| 6857 | return (void*)2;/* tail */ |
| 6858 | return NULL; |
| 6859 | } |
| 6860 | |
| 6861 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6862 | { |
| 6863 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6864 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
| 6866 | ++*pos; |
| 6867 | if (v == (void*)2) |
| 6868 | return NULL; |
| 6869 | |
| 6870 | spin_lock(&all_mddevs_lock); |
| 6871 | if (v == (void*)1) |
| 6872 | tmp = all_mddevs.next; |
| 6873 | else |
| 6874 | tmp = mddev->all_mddevs.next; |
| 6875 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6876 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | else { |
| 6878 | next_mddev = (void*)2; |
| 6879 | *pos = 0x10000; |
| 6880 | } |
| 6881 | spin_unlock(&all_mddevs_lock); |
| 6882 | |
| 6883 | if (v != (void*)1) |
| 6884 | mddev_put(mddev); |
| 6885 | return next_mddev; |
| 6886 | |
| 6887 | } |
| 6888 | |
| 6889 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6890 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6891 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | |
| 6893 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6894 | mddev_put(mddev); |
| 6895 | } |
| 6896 | |
| 6897 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6898 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6899 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6900 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6901 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | |
| 6903 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6904 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | seq_printf(seq, "Personalities : "); |
| 6906 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6907 | list_for_each_entry(pers, &pers_list, list) |
| 6908 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | |
| 6910 | spin_unlock(&pers_lock); |
| 6911 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6912 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | return 0; |
| 6914 | } |
| 6915 | if (v == (void*)2) { |
| 6916 | status_unused(seq); |
| 6917 | return 0; |
| 6918 | } |
| 6919 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6920 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6924 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6925 | mddev->pers ? "" : "in"); |
| 6926 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6927 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6929 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6930 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | seq_printf(seq, " %s", mddev->pers->name); |
| 6932 | } |
| 6933 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6934 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6935 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6936 | char b[BDEVNAME_SIZE]; |
| 6937 | seq_printf(seq, " %s[%d]", |
| 6938 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6939 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6940 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6941 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | seq_printf(seq, "(F)"); |
| 6943 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6944 | } |
| 6945 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6946 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6947 | if (test_bit(Replacement, &rdev->flags)) |
| 6948 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6949 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | } |
| 6951 | |
| 6952 | if (!list_empty(&mddev->disks)) { |
| 6953 | if (mddev->pers) |
| 6954 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6955 | (unsigned long long) |
| 6956 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | else |
| 6958 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6959 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6961 | if (mddev->persistent) { |
| 6962 | if (mddev->major_version != 0 || |
| 6963 | mddev->minor_version != 90) { |
| 6964 | seq_printf(seq," super %d.%d", |
| 6965 | mddev->major_version, |
| 6966 | mddev->minor_version); |
| 6967 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6968 | } else if (mddev->external) |
| 6969 | seq_printf(seq, " super external:%s", |
| 6970 | mddev->metadata_type); |
| 6971 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6972 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | |
| 6974 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6975 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6977 | if (mddev->pers->sync_request) { |
| 6978 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6979 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6980 | seq_printf(seq, "\n "); |
| 6981 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6982 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6983 | else if (mddev->recovery_cp < MaxSector) |
| 6984 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6985 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6986 | } else |
| 6987 | seq_printf(seq, "\n "); |
| 6988 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 6989 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | |
| 6991 | seq_printf(seq, "\n"); |
| 6992 | } |
| 6993 | mddev_unlock(mddev); |
| 6994 | |
| 6995 | return 0; |
| 6996 | } |
| 6997 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6998 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6999 | .start = md_seq_start, |
| 7000 | .next = md_seq_next, |
| 7001 | .stop = md_seq_stop, |
| 7002 | .show = md_seq_show, |
| 7003 | }; |
| 7004 | |
| 7005 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7006 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7007 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7008 | int error; |
| 7009 | |
| 7010 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7011 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7012 | return error; |
| 7013 | |
| 7014 | seq = file->private_data; |
| 7015 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | return error; |
| 7017 | } |
| 7018 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7019 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7020 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7021 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7022 | int mask; |
| 7023 | |
| 7024 | poll_wait(filp, &md_event_waiters, wait); |
| 7025 | |
| 7026 | /* always allow read */ |
| 7027 | mask = POLLIN | POLLRDNORM; |
| 7028 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7029 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7030 | mask |= POLLERR | POLLPRI; |
| 7031 | return mask; |
| 7032 | } |
| 7033 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7034 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7035 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | .open = md_seq_open, |
| 7037 | .read = seq_read, |
| 7038 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7039 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7040 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | }; |
| 7042 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7043 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7046 | list_add_tail(&p->list, &pers_list); |
| 7047 | 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] | 7048 | spin_unlock(&pers_lock); |
| 7049 | return 0; |
| 7050 | } |
| 7051 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7052 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7054 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7056 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7057 | spin_unlock(&pers_lock); |
| 7058 | return 0; |
| 7059 | } |
| 7060 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7061 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7063 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7065 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | |
| 7067 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7068 | rcu_read_lock(); |
| 7069 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7071 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7072 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7073 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7074 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7075 | * as sync_io is counted when a request starts, and |
| 7076 | * disk_stats is counted when it completes. |
| 7077 | * So resync activity will cause curr_events to be smaller than |
| 7078 | * when there was no such activity. |
| 7079 | * non-sync IO will cause disk_stat to increase without |
| 7080 | * increasing sync_io so curr_events will (eventually) |
| 7081 | * be larger than it was before. Once it becomes |
| 7082 | * substantially larger, the test below will cause |
| 7083 | * the array to appear non-idle, and resync will slow |
| 7084 | * down. |
| 7085 | * If there is a lot of outstanding resync activity when |
| 7086 | * we set last_event to curr_events, then all that activity |
| 7087 | * completing might cause the array to appear non-idle |
| 7088 | * and resync will be slowed down even though there might |
| 7089 | * not have been non-resync activity. This will only |
| 7090 | * happen once though. 'last_events' will soon reflect |
| 7091 | * the state where there is little or no outstanding |
| 7092 | * resync requests, and further resync activity will |
| 7093 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7094 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7096 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | rdev->last_events = curr_events; |
| 7098 | idle = 0; |
| 7099 | } |
| 7100 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7101 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | return idle; |
| 7103 | } |
| 7104 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7105 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7106 | { |
| 7107 | /* another "blocks" (512byte) blocks have been synced */ |
| 7108 | atomic_sub(blocks, &mddev->recovery_active); |
| 7109 | wake_up(&mddev->recovery_wait); |
| 7110 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7111 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | md_wakeup_thread(mddev->thread); |
| 7113 | // stop recovery, signal do_sync .... |
| 7114 | } |
| 7115 | } |
| 7116 | |
| 7117 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7118 | /* md_write_start(mddev, bi) |
| 7119 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7120 | * in superblock) before writing, schedule a superblock update |
| 7121 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7122 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7123 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7125 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7126 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7127 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7128 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7129 | BUG_ON(mddev->ro == 1); |
| 7130 | if (mddev->ro == 2) { |
| 7131 | /* need to switch to read/write */ |
| 7132 | mddev->ro = 0; |
| 7133 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7134 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7135 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7136 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7137 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7138 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7139 | if (mddev->safemode == 1) |
| 7140 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7141 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7142 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7143 | if (mddev->in_sync) { |
| 7144 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7145 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7146 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7147 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7148 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7149 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7150 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7151 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7152 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7153 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7154 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7155 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | } |
| 7157 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7158 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | { |
| 7160 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7161 | if (mddev->safemode == 2) |
| 7162 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7163 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7165 | } |
| 7166 | } |
| 7167 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7168 | /* md_allow_write(mddev) |
| 7169 | * Calling this ensures that the array is marked 'active' so that writes |
| 7170 | * may proceed without blocking. It is important to call this before |
| 7171 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7172 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7173 | * |
| 7174 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7175 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7176 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7177 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7178 | { |
| 7179 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7180 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7181 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7182 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7183 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7184 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7185 | |
| 7186 | spin_lock_irq(&mddev->write_lock); |
| 7187 | if (mddev->in_sync) { |
| 7188 | mddev->in_sync = 0; |
| 7189 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7190 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7191 | if (mddev->safemode_delay && |
| 7192 | mddev->safemode == 0) |
| 7193 | mddev->safemode = 1; |
| 7194 | spin_unlock_irq(&mddev->write_lock); |
| 7195 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7196 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7197 | } else |
| 7198 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7199 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7200 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7201 | return -EAGAIN; |
| 7202 | else |
| 7203 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7204 | } |
| 7205 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | #define SYNC_MARKS 10 |
| 7208 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7209 | void md_do_sync(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7211 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | unsigned int currspeed = 0, |
| 7213 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7214 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | unsigned long mark[SYNC_MARKS]; |
| 7216 | sector_t mark_cnt[SYNC_MARKS]; |
| 7217 | int last_mark,m; |
| 7218 | struct list_head *tmp; |
| 7219 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7220 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7221 | struct md_rdev *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7222 | char *desc; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7223 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | |
| 7225 | /* just incase thread restarts... */ |
| 7226 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7227 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7228 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7229 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7231 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7232 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 7233 | desc = "data-check"; |
| 7234 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7235 | desc = "requested-resync"; |
| 7236 | else |
| 7237 | desc = "resync"; |
| 7238 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7239 | desc = "reshape"; |
| 7240 | else |
| 7241 | desc = "recovery"; |
| 7242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | /* we overload curr_resync somewhat here. |
| 7244 | * 0 == not engaged in resync at all |
| 7245 | * 2 == checking that there is no conflict with another sync |
| 7246 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7247 | * commense |
| 7248 | * other == active in resync - this many blocks |
| 7249 | * |
| 7250 | * Before starting a resync we must have set curr_resync to |
| 7251 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7252 | * less than ours. When we find one that is the same or higher |
| 7253 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7254 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7255 | * This will mean we have to start checking from the beginning again. |
| 7256 | * |
| 7257 | */ |
| 7258 | |
| 7259 | do { |
| 7260 | mddev->curr_resync = 2; |
| 7261 | |
| 7262 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7263 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7264 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7265 | |
| 7266 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7268 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | if (mddev2 == mddev) |
| 7270 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7271 | if (!mddev->parallel_resync |
| 7272 | && mddev2->curr_resync |
| 7273 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | DEFINE_WAIT(wq); |
| 7275 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7276 | /* arbitrarily yield */ |
| 7277 | mddev->curr_resync = 1; |
| 7278 | wake_up(&resync_wait); |
| 7279 | } |
| 7280 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7281 | /* no need to wait here, we can wait the next |
| 7282 | * time 'round when curr_resync == 2 |
| 7283 | */ |
| 7284 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7285 | /* We need to wait 'interruptible' so as not to |
| 7286 | * contribute to the load average, and not to |
| 7287 | * be caught by 'softlockup' |
| 7288 | */ |
| 7289 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7290 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7291 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7292 | printk(KERN_INFO "md: delaying %s of %s" |
| 7293 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7295 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7297 | if (signal_pending(current)) |
| 7298 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | schedule(); |
| 7300 | finish_wait(&resync_wait, &wq); |
| 7301 | goto try_again; |
| 7302 | } |
| 7303 | finish_wait(&resync_wait, &wq); |
| 7304 | } |
| 7305 | } |
| 7306 | } while (mddev->curr_resync < 2); |
| 7307 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7308 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7309 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7311 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | */ |
| 7313 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7314 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7315 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7316 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7317 | j = mddev->resync_min; |
| 7318 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7319 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7320 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7321 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7322 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7323 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7324 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7325 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7326 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7327 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7328 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7329 | if (rdev->raid_disk >= 0 && |
| 7330 | !test_bit(Faulty, &rdev->flags) && |
| 7331 | !test_bit(In_sync, &rdev->flags) && |
| 7332 | rdev->recovery_offset < j) |
| 7333 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7334 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7337 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7338 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7339 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7340 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7341 | "(but not more than %d KB/sec) for %s.\n", |
| 7342 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7344 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7345 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7346 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7347 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7348 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7349 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7350 | } |
| 7351 | last_mark = 0; |
| 7352 | mddev->resync_mark = mark[last_mark]; |
| 7353 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7354 | |
| 7355 | /* |
| 7356 | * Tune reconstruction: |
| 7357 | */ |
| 7358 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7359 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7360 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | |
| 7362 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | last_check = 0; |
| 7364 | |
| 7365 | if (j>2) { |
| 7366 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7367 | "md: resuming %s of %s from checkpoint.\n", |
| 7368 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7369 | mddev->curr_resync = j; |
| 7370 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7371 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7373 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7375 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7377 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7378 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7379 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7380 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7381 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7382 | > (max_sectors >> 4)) || |
| 7383 | (j - mddev->curr_resync_completed)*2 |
| 7384 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7385 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7386 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7387 | wait_event(mddev->recovery_wait, |
| 7388 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7389 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7390 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7391 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7392 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7393 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7394 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7395 | /* As this condition is controlled by user-space, |
| 7396 | * we can block indefinitely, so use '_interruptible' |
| 7397 | * to avoid triggering warnings. |
| 7398 | */ |
| 7399 | flush_signals(current); /* just in case */ |
| 7400 | wait_event_interruptible(mddev->recovery_wait, |
| 7401 | mddev->resync_max > j |
| 7402 | || kthread_should_stop()); |
| 7403 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7404 | |
| 7405 | if (kthread_should_stop()) |
| 7406 | goto interrupted; |
| 7407 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7408 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7409 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7410 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7411 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | goto out; |
| 7413 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7414 | |
| 7415 | if (!skipped) { /* actual IO requested */ |
| 7416 | io_sectors += sectors; |
| 7417 | atomic_add(sectors, &mddev->recovery_active); |
| 7418 | } |
| 7419 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7420 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7421 | break; |
| 7422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | j += sectors; |
| 7424 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7425 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7426 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7427 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7428 | * visible in /proc/mdstat |
| 7429 | */ |
| 7430 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7431 | |
| 7432 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | continue; |
| 7434 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7435 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | repeat: |
| 7437 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7438 | /* step marks */ |
| 7439 | int next = (last_mark+1) % SYNC_MARKS; |
| 7440 | |
| 7441 | mddev->resync_mark = mark[next]; |
| 7442 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7443 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7444 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | last_mark = next; |
| 7446 | } |
| 7447 | |
| 7448 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7449 | if (kthread_should_stop()) |
| 7450 | goto interrupted; |
| 7451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7452 | |
| 7453 | /* |
| 7454 | * this loop exits only if either when we are slower than |
| 7455 | * the 'hard' speed limit, or the system was IO-idle for |
| 7456 | * a jiffy. |
| 7457 | * the system might be non-idle CPU-wise, but we only care |
| 7458 | * about not overloading the IO subsystem. (things like an |
| 7459 | * e2fsck being done on the RAID array should execute fast) |
| 7460 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7461 | cond_resched(); |
| 7462 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7463 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7464 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7466 | if (currspeed > speed_min(mddev)) { |
| 7467 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7468 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7469 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | goto repeat; |
| 7471 | } |
| 7472 | } |
| 7473 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7474 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | /* |
| 7476 | * this also signals 'finished resyncing' to md_stop |
| 7477 | */ |
| 7478 | out: |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7479 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7481 | |
| 7482 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7483 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7485 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7486 | mddev->curr_resync > 2) { |
| 7487 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7488 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7489 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7490 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7491 | "md: checkpointing %s of %s.\n", |
| 7492 | desc, mdname(mddev)); |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7493 | mddev->recovery_cp = |
| 7494 | mddev->curr_resync_completed; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7495 | } |
| 7496 | } else |
| 7497 | mddev->recovery_cp = MaxSector; |
| 7498 | } else { |
| 7499 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7500 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7501 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7502 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7503 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7504 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7505 | !test_bit(Faulty, &rdev->flags) && |
| 7506 | !test_bit(In_sync, &rdev->flags) && |
| 7507 | rdev->recovery_offset < mddev->curr_resync) |
| 7508 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7509 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7511 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7512 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7513 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7515 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7516 | /* We completed so min/max setting can be forgotten if used. */ |
| 7517 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7518 | mddev->resync_min = 0; |
| 7519 | mddev->resync_max = MaxSector; |
| 7520 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7521 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | mddev->curr_resync = 0; |
| 7523 | wake_up(&resync_wait); |
| 7524 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7525 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7526 | return; |
| 7527 | |
| 7528 | interrupted: |
| 7529 | /* |
| 7530 | * got a signal, exit. |
| 7531 | */ |
| 7532 | printk(KERN_INFO |
| 7533 | "md: md_do_sync() got signal ... exiting\n"); |
| 7534 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7535 | goto out; |
| 7536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7537 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7538 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7540 | static int remove_and_add_spares(struct mddev *mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7541 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7542 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7543 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7544 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7545 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7546 | mddev->curr_resync_completed = 0; |
| 7547 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7548 | rdev_for_each(rdev, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7549 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7550 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7551 | (test_bit(Faulty, &rdev->flags) || |
| 7552 | ! test_bit(In_sync, &rdev->flags)) && |
| 7553 | atomic_read(&rdev->nr_pending)==0) { |
| 7554 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7555 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7556 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7557 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7558 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7559 | } |
| 7560 | } |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7561 | if (removed) |
| 7562 | sysfs_notify(&mddev->kobj, NULL, |
| 7563 | "degraded"); |
| 7564 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7565 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7566 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7567 | if (rdev->raid_disk >= 0 && |
| 7568 | !test_bit(In_sync, &rdev->flags) && |
| 7569 | !test_bit(Faulty, &rdev->flags)) |
| 7570 | spares++; |
| 7571 | if (rdev->raid_disk < 0 |
| 7572 | && !test_bit(Faulty, &rdev->flags)) { |
| 7573 | rdev->recovery_offset = 0; |
| 7574 | if (mddev->pers-> |
| 7575 | hot_add_disk(mddev, rdev) == 0) { |
| 7576 | if (sysfs_link_rdev(mddev, rdev)) |
| 7577 | /* failure here is OK */; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7578 | spares++; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7579 | md_new_event(mddev); |
| 7580 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7581 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7582 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7583 | } |
| 7584 | return spares; |
| 7585 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7586 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7587 | static void reap_sync_thread(struct mddev *mddev) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7588 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7589 | struct md_rdev *rdev; |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7590 | |
| 7591 | /* resync has finished, collect result */ |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7592 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7593 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7594 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7595 | /* success...*/ |
| 7596 | /* activate any spares */ |
| 7597 | if (mddev->pers->spare_active(mddev)) |
| 7598 | sysfs_notify(&mddev->kobj, NULL, |
| 7599 | "degraded"); |
| 7600 | } |
| 7601 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7602 | mddev->pers->finish_reshape) |
| 7603 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7604 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7605 | /* If array is no-longer degraded, then any saved_raid_disk |
| 7606 | * information must be scrapped. Also if any device is now |
| 7607 | * In_sync we must scrape the saved_raid_disk for that device |
| 7608 | * do the superblock for an incrementally recovered device |
| 7609 | * written out. |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7610 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7611 | rdev_for_each(rdev, mddev) |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7612 | if (!mddev->degraded || |
| 7613 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7614 | rdev->saved_raid_disk = -1; |
| 7615 | |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 7616 | md_update_sb(mddev, 1); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7617 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7618 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7619 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7620 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7621 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7622 | /* flag recovery needed just to double check */ |
| 7623 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7624 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7625 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7626 | if (mddev->event_work.func) |
| 7627 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7628 | } |
| 7629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7630 | /* |
| 7631 | * This routine is regularly called by all per-raid-array threads to |
| 7632 | * deal with generic issues like resync and super-block update. |
| 7633 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7634 | * need this as they never do any recovery or update the superblock. |
| 7635 | * |
| 7636 | * It does not do any resync itself, but rather "forks" off other threads |
| 7637 | * to do that as needed. |
| 7638 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7639 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7640 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7641 | * and wakeups up this thread which will reap the thread and finish up. |
| 7642 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7643 | * |
| 7644 | * The overall approach is: |
| 7645 | * 1/ if the superblock needs updating, update it. |
| 7646 | * 2/ If a recovery thread is running, don't do anything else. |
| 7647 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7648 | * 4/ If there are any faulty devices, remove them. |
| 7649 | * 5/ If array is degraded, try to add spares devices |
| 7650 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7651 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7652 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7653 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7654 | if (mddev->suspended) |
| 7655 | return; |
| 7656 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7657 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7658 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7659 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7660 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7661 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7662 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7663 | mdname(mddev)); |
| 7664 | mddev->safemode = 2; |
| 7665 | } |
| 7666 | flush_signals(current); |
| 7667 | } |
| 7668 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7669 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7670 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7671 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7672 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7674 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7675 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7676 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7677 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7678 | )) |
| 7679 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7680 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7681 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7682 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7683 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7684 | if (mddev->ro) { |
| 7685 | /* Only thing we do on a ro array is remove |
| 7686 | * failed devices. |
| 7687 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7688 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7689 | rdev_for_each(rdev, mddev) |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7690 | if (rdev->raid_disk >= 0 && |
| 7691 | !test_bit(Blocked, &rdev->flags) && |
| 7692 | test_bit(Faulty, &rdev->flags) && |
| 7693 | atomic_read(&rdev->nr_pending)==0) { |
| 7694 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7695 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7696 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7697 | rdev->raid_disk = -1; |
| 7698 | } |
| 7699 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7700 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7701 | goto unlock; |
| 7702 | } |
| 7703 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7704 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7705 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7706 | spin_lock_irq(&mddev->write_lock); |
| 7707 | if (mddev->safemode && |
| 7708 | !atomic_read(&mddev->writes_pending) && |
| 7709 | !mddev->in_sync && |
| 7710 | mddev->recovery_cp == MaxSector) { |
| 7711 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7712 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7713 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7714 | } |
| 7715 | if (mddev->safemode == 1) |
| 7716 | mddev->safemode = 0; |
| 7717 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7718 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7719 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7720 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7721 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7722 | if (mddev->flags) |
| 7723 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7725 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7726 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7727 | /* resync/recovery still happening */ |
| 7728 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7729 | goto unlock; |
| 7730 | } |
| 7731 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7732 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7733 | goto unlock; |
| 7734 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7735 | /* Set RUNNING before clearing NEEDED to avoid |
| 7736 | * any transients in the value of "sync_action". |
| 7737 | */ |
| 7738 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7739 | /* Clear some bits that don't mean anything, but |
| 7740 | * might be left set |
| 7741 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7742 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7743 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7744 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7745 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7746 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7747 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | /* no recovery is running. |
| 7749 | * remove any failed drives, then |
| 7750 | * add spares if possible. |
| 7751 | * Spare are also removed and re-added, to allow |
| 7752 | * the personality to fail the re-add. |
| 7753 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7754 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7755 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7756 | if (mddev->pers->check_reshape == NULL || |
| 7757 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7758 | /* Cannot proceed */ |
| 7759 | goto unlock; |
| 7760 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7761 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7762 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7763 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7764 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7765 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7766 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7767 | } else if (mddev->recovery_cp < MaxSector) { |
| 7768 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7769 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7770 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7771 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7774 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7775 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7776 | /* We are adding a device or devices to an array |
| 7777 | * which has the bitmap stored on all devices. |
| 7778 | * So make sure all bitmap pages get written |
| 7779 | */ |
| 7780 | bitmap_write_all(mddev->bitmap); |
| 7781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7782 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7783 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7784 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7785 | if (!mddev->sync_thread) { |
| 7786 | printk(KERN_ERR "%s: could not start resync" |
| 7787 | " thread...\n", |
| 7788 | mdname(mddev)); |
| 7789 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7790 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7791 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7792 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7793 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7794 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7795 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7796 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7797 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7798 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7799 | } |
| 7800 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7801 | if (!mddev->sync_thread) { |
| 7802 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7803 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7804 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7805 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7806 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | mddev_unlock(mddev); |
| 7809 | } |
| 7810 | } |
| 7811 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7812 | 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] | 7813 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7814 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7815 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7816 | !test_bit(Blocked, &rdev->flags) && |
| 7817 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7818 | msecs_to_jiffies(5000)); |
| 7819 | rdev_dec_pending(rdev, mddev); |
| 7820 | } |
| 7821 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7822 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7823 | void md_finish_reshape(struct mddev *mddev) |
| 7824 | { |
| 7825 | /* called be personality module when reshape completes. */ |
| 7826 | struct md_rdev *rdev; |
| 7827 | |
| 7828 | rdev_for_each(rdev, mddev) { |
| 7829 | if (rdev->data_offset > rdev->new_data_offset) |
| 7830 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7831 | else |
| 7832 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7833 | rdev->data_offset = rdev->new_data_offset; |
| 7834 | } |
| 7835 | } |
| 7836 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7837 | |
| 7838 | /* Bad block management. |
| 7839 | * We can record which blocks on each device are 'bad' and so just |
| 7840 | * fail those blocks, or that stripe, rather than the whole device. |
| 7841 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7842 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7843 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7844 | * A 'shift' can be set so that larger blocks are tracked and |
| 7845 | * consequently larger devices can be covered. |
| 7846 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7847 | * |
| 7848 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7849 | * might need to retry if it is very unlucky. |
| 7850 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7851 | * so we use the write_seqlock_irq variant. |
| 7852 | * |
| 7853 | * When looking for a bad block we specify a range and want to |
| 7854 | * know if any block in the range is bad. So we binary-search |
| 7855 | * to the last range that starts at-or-before the given endpoint, |
| 7856 | * (or "before the sector after the target range") |
| 7857 | * then see if it ends after the given start. |
| 7858 | * We return |
| 7859 | * 0 if there are no known bad blocks in the range |
| 7860 | * 1 if there are known bad block which are all acknowledged |
| 7861 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7862 | * plus the start/length of the first bad section we overlap. |
| 7863 | */ |
| 7864 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7865 | sector_t *first_bad, int *bad_sectors) |
| 7866 | { |
| 7867 | int hi; |
| 7868 | int lo = 0; |
| 7869 | u64 *p = bb->page; |
| 7870 | int rv = 0; |
| 7871 | sector_t target = s + sectors; |
| 7872 | unsigned seq; |
| 7873 | |
| 7874 | if (bb->shift > 0) { |
| 7875 | /* round the start down, and the end up */ |
| 7876 | s >>= bb->shift; |
| 7877 | target += (1<<bb->shift) - 1; |
| 7878 | target >>= bb->shift; |
| 7879 | sectors = target - s; |
| 7880 | } |
| 7881 | /* 'target' is now the first block after the bad range */ |
| 7882 | |
| 7883 | retry: |
| 7884 | seq = read_seqbegin(&bb->lock); |
| 7885 | |
| 7886 | hi = bb->count; |
| 7887 | |
| 7888 | /* Binary search between lo and hi for 'target' |
| 7889 | * i.e. for the last range that starts before 'target' |
| 7890 | */ |
| 7891 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7892 | * are known not to be the last range before target. |
| 7893 | * VARIANT: hi-lo is the number of possible |
| 7894 | * ranges, and decreases until it reaches 1 |
| 7895 | */ |
| 7896 | while (hi - lo > 1) { |
| 7897 | int mid = (lo + hi) / 2; |
| 7898 | sector_t a = BB_OFFSET(p[mid]); |
| 7899 | if (a < target) |
| 7900 | /* This could still be the one, earlier ranges |
| 7901 | * could not. */ |
| 7902 | lo = mid; |
| 7903 | else |
| 7904 | /* This and later ranges are definitely out. */ |
| 7905 | hi = mid; |
| 7906 | } |
| 7907 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7908 | if (hi > lo) { |
| 7909 | /* need to check all range that end after 's' to see if |
| 7910 | * any are unacknowledged. |
| 7911 | */ |
| 7912 | while (lo >= 0 && |
| 7913 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7914 | if (BB_OFFSET(p[lo]) < target) { |
| 7915 | /* starts before the end, and finishes after |
| 7916 | * the start, so they must overlap |
| 7917 | */ |
| 7918 | if (rv != -1 && BB_ACK(p[lo])) |
| 7919 | rv = 1; |
| 7920 | else |
| 7921 | rv = -1; |
| 7922 | *first_bad = BB_OFFSET(p[lo]); |
| 7923 | *bad_sectors = BB_LEN(p[lo]); |
| 7924 | } |
| 7925 | lo--; |
| 7926 | } |
| 7927 | } |
| 7928 | |
| 7929 | if (read_seqretry(&bb->lock, seq)) |
| 7930 | goto retry; |
| 7931 | |
| 7932 | return rv; |
| 7933 | } |
| 7934 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7935 | |
| 7936 | /* |
| 7937 | * Add a range of bad blocks to the table. |
| 7938 | * This might extend the table, or might contract it |
| 7939 | * if two adjacent ranges can be merged. |
| 7940 | * We binary-search to find the 'insertion' point, then |
| 7941 | * decide how best to handle it. |
| 7942 | */ |
| 7943 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7944 | int acknowledged) |
| 7945 | { |
| 7946 | u64 *p; |
| 7947 | int lo, hi; |
| 7948 | int rv = 1; |
| 7949 | |
| 7950 | if (bb->shift < 0) |
| 7951 | /* badblocks are disabled */ |
| 7952 | return 0; |
| 7953 | |
| 7954 | if (bb->shift) { |
| 7955 | /* round the start down, and the end up */ |
| 7956 | sector_t next = s + sectors; |
| 7957 | s >>= bb->shift; |
| 7958 | next += (1<<bb->shift) - 1; |
| 7959 | next >>= bb->shift; |
| 7960 | sectors = next - s; |
| 7961 | } |
| 7962 | |
| 7963 | write_seqlock_irq(&bb->lock); |
| 7964 | |
| 7965 | p = bb->page; |
| 7966 | lo = 0; |
| 7967 | hi = bb->count; |
| 7968 | /* Find the last range that starts at-or-before 's' */ |
| 7969 | while (hi - lo > 1) { |
| 7970 | int mid = (lo + hi) / 2; |
| 7971 | sector_t a = BB_OFFSET(p[mid]); |
| 7972 | if (a <= s) |
| 7973 | lo = mid; |
| 7974 | else |
| 7975 | hi = mid; |
| 7976 | } |
| 7977 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7978 | hi = lo; |
| 7979 | |
| 7980 | if (hi > lo) { |
| 7981 | /* we found a range that might merge with the start |
| 7982 | * of our new range |
| 7983 | */ |
| 7984 | sector_t a = BB_OFFSET(p[lo]); |
| 7985 | sector_t e = a + BB_LEN(p[lo]); |
| 7986 | int ack = BB_ACK(p[lo]); |
| 7987 | if (e >= s) { |
| 7988 | /* Yes, we can merge with a previous range */ |
| 7989 | if (s == a && s + sectors >= e) |
| 7990 | /* new range covers old */ |
| 7991 | ack = acknowledged; |
| 7992 | else |
| 7993 | ack = ack && acknowledged; |
| 7994 | |
| 7995 | if (e < s + sectors) |
| 7996 | e = s + sectors; |
| 7997 | if (e - a <= BB_MAX_LEN) { |
| 7998 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7999 | s = e; |
| 8000 | } else { |
| 8001 | /* does not all fit in one range, |
| 8002 | * make p[lo] maximal |
| 8003 | */ |
| 8004 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8005 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8006 | s = a + BB_MAX_LEN; |
| 8007 | } |
| 8008 | sectors = e - s; |
| 8009 | } |
| 8010 | } |
| 8011 | if (sectors && hi < bb->count) { |
| 8012 | /* 'hi' points to the first range that starts after 's'. |
| 8013 | * Maybe we can merge with the start of that range */ |
| 8014 | sector_t a = BB_OFFSET(p[hi]); |
| 8015 | sector_t e = a + BB_LEN(p[hi]); |
| 8016 | int ack = BB_ACK(p[hi]); |
| 8017 | if (a <= s + sectors) { |
| 8018 | /* merging is possible */ |
| 8019 | if (e <= s + sectors) { |
| 8020 | /* full overlap */ |
| 8021 | e = s + sectors; |
| 8022 | ack = acknowledged; |
| 8023 | } else |
| 8024 | ack = ack && acknowledged; |
| 8025 | |
| 8026 | a = s; |
| 8027 | if (e - a <= BB_MAX_LEN) { |
| 8028 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8029 | s = e; |
| 8030 | } else { |
| 8031 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8032 | s = a + BB_MAX_LEN; |
| 8033 | } |
| 8034 | sectors = e - s; |
| 8035 | lo = hi; |
| 8036 | hi++; |
| 8037 | } |
| 8038 | } |
| 8039 | if (sectors == 0 && hi < bb->count) { |
| 8040 | /* we might be able to combine lo and hi */ |
| 8041 | /* Note: 's' is at the end of 'lo' */ |
| 8042 | sector_t a = BB_OFFSET(p[hi]); |
| 8043 | int lolen = BB_LEN(p[lo]); |
| 8044 | int hilen = BB_LEN(p[hi]); |
| 8045 | int newlen = lolen + hilen - (s - a); |
| 8046 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8047 | /* yes, we can combine them */ |
| 8048 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8049 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8050 | memmove(p + hi, p + hi + 1, |
| 8051 | (bb->count - hi - 1) * 8); |
| 8052 | bb->count--; |
| 8053 | } |
| 8054 | } |
| 8055 | while (sectors) { |
| 8056 | /* didn't merge (it all). |
| 8057 | * Need to add a range just before 'hi' */ |
| 8058 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8059 | /* No room for more */ |
| 8060 | rv = 0; |
| 8061 | break; |
| 8062 | } else { |
| 8063 | int this_sectors = sectors; |
| 8064 | memmove(p + hi + 1, p + hi, |
| 8065 | (bb->count - hi) * 8); |
| 8066 | bb->count++; |
| 8067 | |
| 8068 | if (this_sectors > BB_MAX_LEN) |
| 8069 | this_sectors = BB_MAX_LEN; |
| 8070 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8071 | sectors -= this_sectors; |
| 8072 | s += this_sectors; |
| 8073 | } |
| 8074 | } |
| 8075 | |
| 8076 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8077 | if (!acknowledged) |
| 8078 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8079 | write_sequnlock_irq(&bb->lock); |
| 8080 | |
| 8081 | return rv; |
| 8082 | } |
| 8083 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8084 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8085 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8086 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8087 | int rv; |
| 8088 | if (is_new) |
| 8089 | s += rdev->new_data_offset; |
| 8090 | else |
| 8091 | s += rdev->data_offset; |
| 8092 | rv = md_set_badblocks(&rdev->badblocks, |
| 8093 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8094 | if (rv) { |
| 8095 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8096 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8097 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8098 | md_wakeup_thread(rdev->mddev->thread); |
| 8099 | } |
| 8100 | return rv; |
| 8101 | } |
| 8102 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8103 | |
| 8104 | /* |
| 8105 | * Remove a range of bad blocks from the table. |
| 8106 | * This may involve extending the table if we spilt a region, |
| 8107 | * but it must not fail. So if the table becomes full, we just |
| 8108 | * drop the remove request. |
| 8109 | */ |
| 8110 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8111 | { |
| 8112 | u64 *p; |
| 8113 | int lo, hi; |
| 8114 | sector_t target = s + sectors; |
| 8115 | int rv = 0; |
| 8116 | |
| 8117 | if (bb->shift > 0) { |
| 8118 | /* When clearing we round the start up and the end down. |
| 8119 | * This should not matter as the shift should align with |
| 8120 | * the block size and no rounding should ever be needed. |
| 8121 | * However it is better the think a block is bad when it |
| 8122 | * isn't than to think a block is not bad when it is. |
| 8123 | */ |
| 8124 | s += (1<<bb->shift) - 1; |
| 8125 | s >>= bb->shift; |
| 8126 | target >>= bb->shift; |
| 8127 | sectors = target - s; |
| 8128 | } |
| 8129 | |
| 8130 | write_seqlock_irq(&bb->lock); |
| 8131 | |
| 8132 | p = bb->page; |
| 8133 | lo = 0; |
| 8134 | hi = bb->count; |
| 8135 | /* Find the last range that starts before 'target' */ |
| 8136 | while (hi - lo > 1) { |
| 8137 | int mid = (lo + hi) / 2; |
| 8138 | sector_t a = BB_OFFSET(p[mid]); |
| 8139 | if (a < target) |
| 8140 | lo = mid; |
| 8141 | else |
| 8142 | hi = mid; |
| 8143 | } |
| 8144 | if (hi > lo) { |
| 8145 | /* p[lo] is the last range that could overlap the |
| 8146 | * current range. Earlier ranges could also overlap, |
| 8147 | * but only this one can overlap the end of the range. |
| 8148 | */ |
| 8149 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8150 | /* Partial overlap, leave the tail of this range */ |
| 8151 | int ack = BB_ACK(p[lo]); |
| 8152 | sector_t a = BB_OFFSET(p[lo]); |
| 8153 | sector_t end = a + BB_LEN(p[lo]); |
| 8154 | |
| 8155 | if (a < s) { |
| 8156 | /* we need to split this range */ |
| 8157 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8158 | rv = 0; |
| 8159 | goto out; |
| 8160 | } |
| 8161 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8162 | bb->count++; |
| 8163 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8164 | lo++; |
| 8165 | } |
| 8166 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8167 | /* there is no longer an overlap */ |
| 8168 | hi = lo; |
| 8169 | lo--; |
| 8170 | } |
| 8171 | while (lo >= 0 && |
| 8172 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8173 | /* This range does overlap */ |
| 8174 | if (BB_OFFSET(p[lo]) < s) { |
| 8175 | /* Keep the early parts of this range. */ |
| 8176 | int ack = BB_ACK(p[lo]); |
| 8177 | sector_t start = BB_OFFSET(p[lo]); |
| 8178 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8179 | /* now low doesn't overlap, so.. */ |
| 8180 | break; |
| 8181 | } |
| 8182 | lo--; |
| 8183 | } |
| 8184 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8185 | * anything between needs to be discarded |
| 8186 | */ |
| 8187 | if (hi - lo > 1) { |
| 8188 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8189 | bb->count -= (hi - lo - 1); |
| 8190 | } |
| 8191 | } |
| 8192 | |
| 8193 | bb->changed = 1; |
| 8194 | out: |
| 8195 | write_sequnlock_irq(&bb->lock); |
| 8196 | return rv; |
| 8197 | } |
| 8198 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8199 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8200 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8201 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8202 | if (is_new) |
| 8203 | s += rdev->new_data_offset; |
| 8204 | else |
| 8205 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8206 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8207 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8208 | } |
| 8209 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8210 | |
| 8211 | /* |
| 8212 | * Acknowledge all bad blocks in a list. |
| 8213 | * This only succeeds if ->changed is clear. It is used by |
| 8214 | * in-kernel metadata updates |
| 8215 | */ |
| 8216 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8217 | { |
| 8218 | if (bb->page == NULL || bb->changed) |
| 8219 | /* no point even trying */ |
| 8220 | return; |
| 8221 | write_seqlock_irq(&bb->lock); |
| 8222 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8223 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8224 | u64 *p = bb->page; |
| 8225 | int i; |
| 8226 | for (i = 0; i < bb->count ; i++) { |
| 8227 | if (!BB_ACK(p[i])) { |
| 8228 | sector_t start = BB_OFFSET(p[i]); |
| 8229 | int len = BB_LEN(p[i]); |
| 8230 | p[i] = BB_MAKE(start, len, 1); |
| 8231 | } |
| 8232 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8233 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8234 | } |
| 8235 | write_sequnlock_irq(&bb->lock); |
| 8236 | } |
| 8237 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8238 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8239 | /* sysfs access to bad-blocks list. |
| 8240 | * We present two files. |
| 8241 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8242 | * are recorded as bad. The list is truncated to fit within |
| 8243 | * the one-page limit of sysfs. |
| 8244 | * Writing "sector length" to this file adds an acknowledged |
| 8245 | * bad block list. |
| 8246 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8247 | * been acknowledged. Writing to this file adds bad blocks |
| 8248 | * without acknowledging them. This is largely for testing. |
| 8249 | */ |
| 8250 | |
| 8251 | static ssize_t |
| 8252 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8253 | { |
| 8254 | size_t len; |
| 8255 | int i; |
| 8256 | u64 *p = bb->page; |
| 8257 | unsigned seq; |
| 8258 | |
| 8259 | if (bb->shift < 0) |
| 8260 | return 0; |
| 8261 | |
| 8262 | retry: |
| 8263 | seq = read_seqbegin(&bb->lock); |
| 8264 | |
| 8265 | len = 0; |
| 8266 | i = 0; |
| 8267 | |
| 8268 | while (len < PAGE_SIZE && i < bb->count) { |
| 8269 | sector_t s = BB_OFFSET(p[i]); |
| 8270 | unsigned int length = BB_LEN(p[i]); |
| 8271 | int ack = BB_ACK(p[i]); |
| 8272 | i++; |
| 8273 | |
| 8274 | if (unack && ack) |
| 8275 | continue; |
| 8276 | |
| 8277 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8278 | (unsigned long long)s << bb->shift, |
| 8279 | length << bb->shift); |
| 8280 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8281 | if (unack && len == 0) |
| 8282 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8283 | |
| 8284 | if (read_seqretry(&bb->lock, seq)) |
| 8285 | goto retry; |
| 8286 | |
| 8287 | return len; |
| 8288 | } |
| 8289 | |
| 8290 | #define DO_DEBUG 1 |
| 8291 | |
| 8292 | static ssize_t |
| 8293 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8294 | { |
| 8295 | unsigned long long sector; |
| 8296 | int length; |
| 8297 | char newline; |
| 8298 | #ifdef DO_DEBUG |
| 8299 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8300 | * Normally only a successful write may clear a badblock |
| 8301 | */ |
| 8302 | int clear = 0; |
| 8303 | if (page[0] == '-') { |
| 8304 | clear = 1; |
| 8305 | page++; |
| 8306 | } |
| 8307 | #endif /* DO_DEBUG */ |
| 8308 | |
| 8309 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8310 | case 3: |
| 8311 | if (newline != '\n') |
| 8312 | return -EINVAL; |
| 8313 | case 2: |
| 8314 | if (length <= 0) |
| 8315 | return -EINVAL; |
| 8316 | break; |
| 8317 | default: |
| 8318 | return -EINVAL; |
| 8319 | } |
| 8320 | |
| 8321 | #ifdef DO_DEBUG |
| 8322 | if (clear) { |
| 8323 | md_clear_badblocks(bb, sector, length); |
| 8324 | return len; |
| 8325 | } |
| 8326 | #endif /* DO_DEBUG */ |
| 8327 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8328 | return len; |
| 8329 | else |
| 8330 | return -ENOSPC; |
| 8331 | } |
| 8332 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8333 | static int md_notify_reboot(struct notifier_block *this, |
| 8334 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8335 | { |
| 8336 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8337 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8338 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8339 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8340 | for_each_mddev(mddev, tmp) { |
| 8341 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8342 | if (mddev->pers) |
| 8343 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8344 | mddev->safemode = 2; |
| 8345 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8346 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8347 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8348 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8349 | /* |
| 8350 | * certain more exotic SCSI devices are known to be |
| 8351 | * volatile wrt too early system reboots. While the |
| 8352 | * right place to handle this issue is the given |
| 8353 | * driver, we do want to have a safe RAID driver ... |
| 8354 | */ |
| 8355 | if (need_delay) |
| 8356 | mdelay(1000*1); |
| 8357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8358 | return NOTIFY_DONE; |
| 8359 | } |
| 8360 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8361 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8362 | .notifier_call = md_notify_reboot, |
| 8363 | .next = NULL, |
| 8364 | .priority = INT_MAX, /* before any real devices */ |
| 8365 | }; |
| 8366 | |
| 8367 | static void md_geninit(void) |
| 8368 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8369 | 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] | 8370 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8371 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8372 | } |
| 8373 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8374 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8375 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8376 | int ret = -ENOMEM; |
| 8377 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8378 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8379 | if (!md_wq) |
| 8380 | goto err_wq; |
| 8381 | |
| 8382 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8383 | if (!md_misc_wq) |
| 8384 | goto err_misc_wq; |
| 8385 | |
| 8386 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8387 | goto err_md; |
| 8388 | |
| 8389 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8390 | goto err_mdp; |
| 8391 | mdp_major = ret; |
| 8392 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8393 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8394 | md_probe, NULL, NULL); |
| 8395 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8396 | md_probe, NULL, NULL); |
| 8397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8398 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8399 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8400 | |
| 8401 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8402 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8403 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8404 | err_mdp: |
| 8405 | unregister_blkdev(MD_MAJOR, "md"); |
| 8406 | err_md: |
| 8407 | destroy_workqueue(md_misc_wq); |
| 8408 | err_misc_wq: |
| 8409 | destroy_workqueue(md_wq); |
| 8410 | err_wq: |
| 8411 | return ret; |
| 8412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8413 | |
| 8414 | #ifndef MODULE |
| 8415 | |
| 8416 | /* |
| 8417 | * Searches all registered partitions for autorun RAID arrays |
| 8418 | * at boot time. |
| 8419 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8420 | |
| 8421 | static LIST_HEAD(all_detected_devices); |
| 8422 | struct detected_devices_node { |
| 8423 | struct list_head list; |
| 8424 | dev_t dev; |
| 8425 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8426 | |
| 8427 | void md_autodetect_dev(dev_t dev) |
| 8428 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8429 | struct detected_devices_node *node_detected_dev; |
| 8430 | |
| 8431 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8432 | if (node_detected_dev) { |
| 8433 | node_detected_dev->dev = dev; |
| 8434 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8435 | } else { |
| 8436 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8437 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8439 | } |
| 8440 | |
| 8441 | |
| 8442 | static void autostart_arrays(int part) |
| 8443 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8444 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8445 | struct detected_devices_node *node_detected_dev; |
| 8446 | dev_t dev; |
| 8447 | int i_scanned, i_passed; |
| 8448 | |
| 8449 | i_scanned = 0; |
| 8450 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8451 | |
| 8452 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8453 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8454 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8455 | i_scanned++; |
| 8456 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8457 | struct detected_devices_node, list); |
| 8458 | list_del(&node_detected_dev->list); |
| 8459 | dev = node_detected_dev->dev; |
| 8460 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8461 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8462 | if (IS_ERR(rdev)) |
| 8463 | continue; |
| 8464 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8465 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8466 | MD_BUG(); |
| 8467 | continue; |
| 8468 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8469 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8470 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8471 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8472 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8473 | |
| 8474 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8475 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8476 | |
| 8477 | autorun_devices(part); |
| 8478 | } |
| 8479 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8480 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8481 | |
| 8482 | static __exit void md_exit(void) |
| 8483 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8484 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8485 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8486 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8487 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8488 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8489 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8490 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8491 | unregister_blkdev(mdp_major, "mdp"); |
| 8492 | unregister_reboot_notifier(&md_notifier); |
| 8493 | unregister_sysctl_table(raid_table_header); |
| 8494 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8495 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8496 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8497 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8498 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8499 | destroy_workqueue(md_misc_wq); |
| 8500 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8501 | } |
| 8502 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8503 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 | module_exit(md_exit) |
| 8505 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8506 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8507 | { |
| 8508 | return sprintf(buffer, "%d", start_readonly); |
| 8509 | } |
| 8510 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8511 | { |
| 8512 | char *e; |
| 8513 | int num = simple_strtoul(val, &e, 10); |
| 8514 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8515 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8516 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8517 | } |
| 8518 | return -EINVAL; |
| 8519 | } |
| 8520 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8521 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8522 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8523 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8524 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8526 | EXPORT_SYMBOL(register_md_personality); |
| 8527 | EXPORT_SYMBOL(unregister_md_personality); |
| 8528 | EXPORT_SYMBOL(md_error); |
| 8529 | EXPORT_SYMBOL(md_done_sync); |
| 8530 | EXPORT_SYMBOL(md_write_start); |
| 8531 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8532 | EXPORT_SYMBOL(md_register_thread); |
| 8533 | EXPORT_SYMBOL(md_unregister_thread); |
| 8534 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8535 | EXPORT_SYMBOL(md_check_recovery); |
| 8536 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8537 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8538 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8539 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |