Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
| 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define DEBUG 0 |
| 58 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 64 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pers_lock); |
| 66 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 67 | static void md_print_devices(void); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 70 | static struct workqueue_struct *md_wq; |
| 71 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 73 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 76 | * Default number of read corrections we'll attempt on an rdev |
| 77 | * before ejecting it from the array. We divide the read error |
| 78 | * count by 2 for every hour elapsed between read errors. |
| 79 | */ |
| 80 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * 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] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .procname = "speed_limit_min", |
| 113 | .data = &sysctl_speed_limit_min, |
| 114 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 115 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 116 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "speed_limit_max", |
| 120 | .data = &sysctl_speed_limit_max, |
| 121 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 122 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 123 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 125 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static ctl_table raid_dir_table[] = { |
| 129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .procname = "raid", |
| 131 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 132 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .child = raid_table, |
| 134 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 135 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static ctl_table raid_root_table[] = { |
| 139 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .procname = "dev", |
| 141 | .maxlen = 0, |
| 142 | .mode = 0555, |
| 143 | .child = raid_dir_table, |
| 144 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 145 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 148 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 150 | static int start_readonly; |
| 151 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 152 | /* bio_clone_mddev |
| 153 | * like bio_clone, but with a local bio set |
| 154 | */ |
| 155 | |
| 156 | static void mddev_bio_destructor(struct bio *bio) |
| 157 | { |
| 158 | mddev_t *mddev, **mddevp; |
| 159 | |
| 160 | mddevp = (void*)bio; |
| 161 | mddev = mddevp[-1]; |
| 162 | |
| 163 | bio_free(bio, mddev->bio_set); |
| 164 | } |
| 165 | |
| 166 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
| 167 | mddev_t *mddev) |
| 168 | { |
| 169 | struct bio *b; |
| 170 | mddev_t **mddevp; |
| 171 | |
| 172 | if (!mddev || !mddev->bio_set) |
| 173 | return bio_alloc(gfp_mask, nr_iovecs); |
| 174 | |
| 175 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 176 | mddev->bio_set); |
| 177 | if (!b) |
| 178 | return NULL; |
| 179 | mddevp = (void*)b; |
| 180 | mddevp[-1] = mddev; |
| 181 | b->bi_destructor = mddev_bio_destructor; |
| 182 | return b; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 185 | |
| 186 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
| 187 | mddev_t *mddev) |
| 188 | { |
| 189 | struct bio *b; |
| 190 | mddev_t **mddevp; |
| 191 | |
| 192 | if (!mddev || !mddev->bio_set) |
| 193 | return bio_clone(bio, gfp_mask); |
| 194 | |
| 195 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 196 | mddev->bio_set); |
| 197 | if (!b) |
| 198 | return NULL; |
| 199 | mddevp = (void*)b; |
| 200 | mddevp[-1] = mddev; |
| 201 | b->bi_destructor = mddev_bio_destructor; |
| 202 | __bio_clone(b, bio); |
| 203 | if (bio_integrity(bio)) { |
| 204 | int ret; |
| 205 | |
| 206 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 207 | |
| 208 | if (ret < 0) { |
| 209 | bio_put(b); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return b; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 217 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 218 | void md_trim_bio(struct bio *bio, int offset, int size) |
| 219 | { |
| 220 | /* 'bio' is a cloned bio which we need to trim to match |
| 221 | * the given offset and size. |
| 222 | * This requires adjusting bi_sector, bi_size, and bi_io_vec |
| 223 | */ |
| 224 | int i; |
| 225 | struct bio_vec *bvec; |
| 226 | int sofar = 0; |
| 227 | |
| 228 | size <<= 9; |
| 229 | if (offset == 0 && size == bio->bi_size) |
| 230 | return; |
| 231 | |
| 232 | bio->bi_sector += offset; |
| 233 | bio->bi_size = size; |
| 234 | offset <<= 9; |
| 235 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 236 | |
| 237 | while (bio->bi_idx < bio->bi_vcnt && |
| 238 | bio->bi_io_vec[bio->bi_idx].bv_len <= offset) { |
| 239 | /* remove this whole bio_vec */ |
| 240 | offset -= bio->bi_io_vec[bio->bi_idx].bv_len; |
| 241 | bio->bi_idx++; |
| 242 | } |
| 243 | if (bio->bi_idx < bio->bi_vcnt) { |
| 244 | bio->bi_io_vec[bio->bi_idx].bv_offset += offset; |
| 245 | bio->bi_io_vec[bio->bi_idx].bv_len -= offset; |
| 246 | } |
| 247 | /* avoid any complications with bi_idx being non-zero*/ |
| 248 | if (bio->bi_idx) { |
| 249 | memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx, |
| 250 | (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec)); |
| 251 | bio->bi_vcnt -= bio->bi_idx; |
| 252 | bio->bi_idx = 0; |
| 253 | } |
| 254 | /* Make sure vcnt and last bv are not too big */ |
| 255 | bio_for_each_segment(bvec, bio, i) { |
| 256 | if (sofar + bvec->bv_len > size) |
| 257 | bvec->bv_len = size - sofar; |
| 258 | if (bvec->bv_len == 0) { |
| 259 | bio->bi_vcnt = i; |
| 260 | break; |
| 261 | } |
| 262 | sofar += bvec->bv_len; |
| 263 | } |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(md_trim_bio); |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 268 | * We have a system wide 'event count' that is incremented |
| 269 | * on any 'interesting' event, and readers of /proc/mdstat |
| 270 | * can use 'poll' or 'select' to find out when the event |
| 271 | * count increases. |
| 272 | * |
| 273 | * Events are: |
| 274 | * start array, stop array, error, add device, remove device, |
| 275 | * start build, activate spare |
| 276 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 277 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 278 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 279 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 280 | { |
| 281 | atomic_inc(&md_event_count); |
| 282 | wake_up(&md_event_waiters); |
| 283 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 285 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 286 | /* Alternate version that can be called from interrupts |
| 287 | * when calling sysfs_notify isn't needed. |
| 288 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 289 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 290 | { |
| 291 | atomic_inc(&md_event_count); |
| 292 | wake_up(&md_event_waiters); |
| 293 | } |
| 294 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 295 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * Enables to iterate over all existing md arrays |
| 297 | * all_mddevs_lock protects this list. |
| 298 | */ |
| 299 | static LIST_HEAD(all_mddevs); |
| 300 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 301 | |
| 302 | |
| 303 | /* |
| 304 | * iterates through all used mddevs in the system. |
| 305 | * We take care to grab the all_mddevs_lock whenever navigating |
| 306 | * the list, and to always hold a refcount when unlocked. |
| 307 | * Any code which breaks out of this loop while own |
| 308 | * a reference to the current mddev and must mddev_put it. |
| 309 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 310 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | \ |
| 312 | for (({ spin_lock(&all_mddevs_lock); \ |
| 313 | tmp = all_mddevs.next; \ |
| 314 | mddev = NULL;}); \ |
| 315 | ({ if (tmp != &all_mddevs) \ |
| 316 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 317 | spin_unlock(&all_mddevs_lock); \ |
| 318 | if (mddev) mddev_put(mddev); \ |
| 319 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 320 | tmp != &all_mddevs;}); \ |
| 321 | ({ spin_lock(&all_mddevs_lock); \ |
| 322 | tmp = tmp->next;}) \ |
| 323 | ) |
| 324 | |
| 325 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 326 | /* Rather than calling directly into the personality make_request function, |
| 327 | * IO requests come here first so that we can check if the device is |
| 328 | * being suspended pending a reconfiguration. |
| 329 | * We hold a refcount over the call to ->make_request. By the time that |
| 330 | * call has finished, the bio has been linked into some internal structure |
| 331 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 332 | */ |
| 333 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 335 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 336 | mddev_t *mddev = q->queuedata; |
| 337 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 338 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 339 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 340 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 341 | if (mddev == NULL || mddev->pers == NULL |
| 342 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 343 | bio_io_error(bio); |
| 344 | return 0; |
| 345 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 346 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 347 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 348 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 349 | DEFINE_WAIT(__wait); |
| 350 | for (;;) { |
| 351 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 352 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 353 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 354 | break; |
| 355 | rcu_read_unlock(); |
| 356 | schedule(); |
| 357 | rcu_read_lock(); |
| 358 | } |
| 359 | finish_wait(&mddev->sb_wait, &__wait); |
| 360 | } |
| 361 | atomic_inc(&mddev->active_io); |
| 362 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 363 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 364 | /* |
| 365 | * save the sectors now since our bio can |
| 366 | * go away inside make_request |
| 367 | */ |
| 368 | sectors = bio_sectors(bio); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 369 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 370 | |
| 371 | cpu = part_stat_lock(); |
| 372 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 373 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 374 | part_stat_unlock(); |
| 375 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 376 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 377 | wake_up(&mddev->sb_wait); |
| 378 | |
| 379 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 382 | /* mddev_suspend makes sure no new requests are submitted |
| 383 | * to the device, and that any requests that have been submitted |
| 384 | * are completely handled. |
| 385 | * Once ->stop is called and completes, the module will be completely |
| 386 | * unused. |
| 387 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 388 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 389 | { |
| 390 | BUG_ON(mddev->suspended); |
| 391 | mddev->suspended = 1; |
| 392 | synchronize_rcu(); |
| 393 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 394 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 395 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 397 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 398 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 399 | { |
| 400 | mddev->suspended = 0; |
| 401 | wake_up(&mddev->sb_wait); |
| 402 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 403 | |
| 404 | md_wakeup_thread(mddev->thread); |
| 405 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 406 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 407 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 408 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 409 | int mddev_congested(mddev_t *mddev, int bits) |
| 410 | { |
| 411 | return mddev->suspended; |
| 412 | } |
| 413 | EXPORT_SYMBOL(mddev_congested); |
| 414 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 415 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | */ |
| 418 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 419 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | { |
| 421 | mdk_rdev_t *rdev = bio->bi_private; |
| 422 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | |
| 424 | rdev_dec_pending(rdev, mddev); |
| 425 | |
| 426 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 427 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 428 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 429 | } |
| 430 | bio_put(bio); |
| 431 | } |
| 432 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 433 | static void md_submit_flush_data(struct work_struct *ws); |
| 434 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 435 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 436 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 437 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | mdk_rdev_t *rdev; |
| 439 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 440 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 441 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 442 | rcu_read_lock(); |
| 443 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 444 | if (rdev->raid_disk >= 0 && |
| 445 | !test_bit(Faulty, &rdev->flags)) { |
| 446 | /* Take two references, one is dropped |
| 447 | * when request finishes, one after |
| 448 | * we reclaim rcu_read_lock |
| 449 | */ |
| 450 | struct bio *bi; |
| 451 | atomic_inc(&rdev->nr_pending); |
| 452 | atomic_inc(&rdev->nr_pending); |
| 453 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 454 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 455 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 456 | bi->bi_private = rdev; |
| 457 | bi->bi_bdev = rdev->bdev; |
| 458 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 459 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 460 | rcu_read_lock(); |
| 461 | rdev_dec_pending(rdev, mddev); |
| 462 | } |
| 463 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 464 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 465 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 466 | } |
| 467 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 468 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 469 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 470 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 471 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 472 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 473 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 474 | /* an empty barrier - all done */ |
| 475 | bio_endio(bio, 0); |
| 476 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 477 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 478 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 479 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 480 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 481 | |
| 482 | mddev->flush_bio = NULL; |
| 483 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 484 | } |
| 485 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 486 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 487 | { |
| 488 | spin_lock_irq(&mddev->write_lock); |
| 489 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 490 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 491 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 492 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 493 | spin_unlock_irq(&mddev->write_lock); |
| 494 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 495 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 496 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 497 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 498 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 499 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 500 | /* Support for plugging. |
| 501 | * This mirrors the plugging support in request_queue, but does not |
| 502 | * require having a whole queue or request structures. |
| 503 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 504 | * plugged on. This links tot the private plug_handle structure in the |
| 505 | * personality data where we keep a count of the number of outstanding |
| 506 | * plugs so other code can see if a plug is active. |
| 507 | */ |
| 508 | struct md_plug_cb { |
| 509 | struct blk_plug_cb cb; |
| 510 | mddev_t *mddev; |
| 511 | }; |
| 512 | |
| 513 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 514 | { |
| 515 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 516 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 517 | md_wakeup_thread(mdcb->mddev->thread); |
| 518 | kfree(mdcb); |
| 519 | } |
| 520 | |
| 521 | /* Check that an unplug wakeup will come shortly. |
| 522 | * If not, wakeup the md thread immediately |
| 523 | */ |
| 524 | int mddev_check_plugged(mddev_t *mddev) |
| 525 | { |
| 526 | struct blk_plug *plug = current->plug; |
| 527 | struct md_plug_cb *mdcb; |
| 528 | |
| 529 | if (!plug) |
| 530 | return 0; |
| 531 | |
| 532 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 533 | if (mdcb->cb.callback == plugger_unplug && |
| 534 | mdcb->mddev == mddev) { |
| 535 | /* Already on the list, move to top */ |
| 536 | if (mdcb != list_first_entry(&plug->cb_list, |
| 537 | struct md_plug_cb, |
| 538 | cb.list)) |
| 539 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 540 | return 1; |
| 541 | } |
| 542 | } |
| 543 | /* Not currently on the callback list */ |
| 544 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 545 | if (!mdcb) |
| 546 | return 0; |
| 547 | |
| 548 | mdcb->mddev = mddev; |
| 549 | mdcb->cb.callback = plugger_unplug; |
| 550 | atomic_inc(&mddev->plug_cnt); |
| 551 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 552 | return 1; |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 557 | { |
| 558 | atomic_inc(&mddev->active); |
| 559 | return mddev; |
| 560 | } |
| 561 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 562 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | static void mddev_put(mddev_t *mddev) |
| 565 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 566 | struct bio_set *bs = NULL; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 569 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 570 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 571 | mddev->ctime == 0 && !mddev->hold_active) { |
| 572 | /* Array is not configured at all, and not held active, |
| 573 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 575 | bs = mddev->bio_set; |
| 576 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 577 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 578 | /* We did a probe so need to clean up. Call |
| 579 | * queue_work inside the spinlock so that |
| 580 | * flush_workqueue() after mddev_find will |
| 581 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 582 | */ |
| 583 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 584 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 585 | } else |
| 586 | kfree(mddev); |
| 587 | } |
| 588 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 589 | if (bs) |
| 590 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 593 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 594 | { |
| 595 | mutex_init(&mddev->open_mutex); |
| 596 | mutex_init(&mddev->reconfig_mutex); |
| 597 | mutex_init(&mddev->bitmap_info.mutex); |
| 598 | INIT_LIST_HEAD(&mddev->disks); |
| 599 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 600 | init_timer(&mddev->safemode_timer); |
| 601 | atomic_set(&mddev->active, 1); |
| 602 | atomic_set(&mddev->openers, 0); |
| 603 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 604 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 605 | spin_lock_init(&mddev->write_lock); |
| 606 | atomic_set(&mddev->flush_pending, 0); |
| 607 | init_waitqueue_head(&mddev->sb_wait); |
| 608 | init_waitqueue_head(&mddev->recovery_wait); |
| 609 | mddev->reshape_position = MaxSector; |
| 610 | mddev->resync_min = 0; |
| 611 | mddev->resync_max = MaxSector; |
| 612 | mddev->level = LEVEL_NONE; |
| 613 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 614 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | static mddev_t * mddev_find(dev_t unit) |
| 617 | { |
| 618 | mddev_t *mddev, *new = NULL; |
| 619 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 620 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 621 | unit &= ~((1<<MdpMinorShift)-1); |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | retry: |
| 624 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 626 | if (unit) { |
| 627 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 628 | if (mddev->unit == unit) { |
| 629 | mddev_get(mddev); |
| 630 | spin_unlock(&all_mddevs_lock); |
| 631 | kfree(new); |
| 632 | return mddev; |
| 633 | } |
| 634 | |
| 635 | if (new) { |
| 636 | list_add(&new->all_mddevs, &all_mddevs); |
| 637 | spin_unlock(&all_mddevs_lock); |
| 638 | new->hold_active = UNTIL_IOCTL; |
| 639 | return new; |
| 640 | } |
| 641 | } else if (new) { |
| 642 | /* find an unused unit number */ |
| 643 | static int next_minor = 512; |
| 644 | int start = next_minor; |
| 645 | int is_free = 0; |
| 646 | int dev = 0; |
| 647 | while (!is_free) { |
| 648 | dev = MKDEV(MD_MAJOR, next_minor); |
| 649 | next_minor++; |
| 650 | if (next_minor > MINORMASK) |
| 651 | next_minor = 0; |
| 652 | if (next_minor == start) { |
| 653 | /* Oh dear, all in use. */ |
| 654 | spin_unlock(&all_mddevs_lock); |
| 655 | kfree(new); |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | is_free = 1; |
| 660 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 661 | if (mddev->unit == dev) { |
| 662 | is_free = 0; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | new->unit = dev; |
| 667 | new->md_minor = MINOR(dev); |
| 668 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | list_add(&new->all_mddevs, &all_mddevs); |
| 670 | spin_unlock(&all_mddevs_lock); |
| 671 | return new; |
| 672 | } |
| 673 | spin_unlock(&all_mddevs_lock); |
| 674 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 675 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (!new) |
| 677 | return NULL; |
| 678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | new->unit = unit; |
| 680 | if (MAJOR(unit) == MD_MAJOR) |
| 681 | new->md_minor = MINOR(unit); |
| 682 | else |
| 683 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 684 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 685 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | goto retry; |
| 688 | } |
| 689 | |
| 690 | static inline int mddev_lock(mddev_t * mddev) |
| 691 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 692 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 695 | static inline int mddev_is_locked(mddev_t *mddev) |
| 696 | { |
| 697 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | static inline int mddev_trylock(mddev_t * mddev) |
| 701 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 702 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 705 | static struct attribute_group md_redundancy_group; |
| 706 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 707 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 709 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 710 | /* These cannot be removed under reconfig_mutex as |
| 711 | * an access to the files will try to take reconfig_mutex |
| 712 | * while holding the file unremovable, which leads to |
| 713 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 714 | * So hold set sysfs_active while the remove in happeing, |
| 715 | * and anything else which might set ->to_remove or my |
| 716 | * otherwise change the sysfs namespace will fail with |
| 717 | * -EBUSY if sysfs_active is still set. |
| 718 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 719 | * test it under the same mutex to ensure its correct value |
| 720 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 721 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 722 | struct attribute_group *to_remove = mddev->to_remove; |
| 723 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 724 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 725 | mutex_unlock(&mddev->reconfig_mutex); |
| 726 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 727 | if (mddev->kobj.sd) { |
| 728 | if (to_remove != &md_redundancy_group) |
| 729 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 730 | if (mddev->pers == NULL || |
| 731 | mddev->pers->sync_request == NULL) { |
| 732 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 733 | if (mddev->sysfs_action) |
| 734 | sysfs_put(mddev->sysfs_action); |
| 735 | mddev->sysfs_action = NULL; |
| 736 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 737 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 738 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 739 | } else |
| 740 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 742 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | } |
| 744 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 745 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 747 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 749 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | if (rdev->desc_nr == nr) |
| 751 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 757 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | mdk_rdev_t *rdev; |
| 759 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 760 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | if (rdev->bdev->bd_dev == dev) |
| 762 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | return NULL; |
| 765 | } |
| 766 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 767 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 768 | { |
| 769 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 770 | list_for_each_entry(pers, &pers_list, list) { |
| 771 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 772 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 773 | if (strcmp(pers->name, clevel)==0) |
| 774 | return pers; |
| 775 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 776 | return NULL; |
| 777 | } |
| 778 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 779 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 780 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 782 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 783 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 787 | { |
| 788 | if (rdev->sb_page) |
| 789 | MD_BUG(); |
| 790 | |
| 791 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 792 | if (!rdev->sb_page) { |
| 793 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 794 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 801 | { |
| 802 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 803 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | rdev->sb_loaded = 0; |
| 805 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 806 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 807 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 809 | if (rdev->bb_page) { |
| 810 | put_page(rdev->bb_page); |
| 811 | rdev->bb_page = NULL; |
| 812 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 816 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 817 | { |
| 818 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 819 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 820 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 821 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 822 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 823 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 824 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 825 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 826 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 827 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 828 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 829 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 830 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 834 | sector_t sector, int size, struct page *page) |
| 835 | { |
| 836 | /* write first size bytes of page to sector of rdev |
| 837 | * Increment mddev->pending_writes before returning |
| 838 | * and decrement it on completion, waking up sb_wait |
| 839 | * if zero is reached. |
| 840 | * If an error occurred, call md_error |
| 841 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 842 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 843 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 844 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 845 | bio->bi_sector = sector; |
| 846 | bio_add_page(bio, page, size, 0); |
| 847 | bio->bi_private = rdev; |
| 848 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 849 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 850 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 851 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void md_super_wait(mddev_t *mddev) |
| 855 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 856 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 857 | DEFINE_WAIT(wq); |
| 858 | for(;;) { |
| 859 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 860 | if (atomic_read(&mddev->pending_writes)==0) |
| 861 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 862 | schedule(); |
| 863 | } |
| 864 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 865 | } |
| 866 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 867 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 872 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 873 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 875 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct completion event; |
| 877 | int ret; |
| 878 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 879 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 881 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 882 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 883 | if (metadata_op) |
| 884 | bio->bi_sector = sector + rdev->sb_start; |
| 885 | else |
| 886 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | bio_add_page(bio, page, size, 0); |
| 888 | init_completion(&event); |
| 889 | bio->bi_private = &event; |
| 890 | bio->bi_end_io = bi_complete; |
| 891 | submit_bio(rw, bio); |
| 892 | wait_for_completion(&event); |
| 893 | |
| 894 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 895 | bio_put(bio); |
| 896 | return ret; |
| 897 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 898 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 900 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
| 902 | char b[BDEVNAME_SIZE]; |
| 903 | if (!rdev->sb_page) { |
| 904 | MD_BUG(); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | if (rdev->sb_loaded) |
| 908 | return 0; |
| 909 | |
| 910 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 911 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | goto fail; |
| 913 | rdev->sb_loaded = 1; |
| 914 | return 0; |
| 915 | |
| 916 | fail: |
| 917 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 918 | bdevname(rdev->bdev,b)); |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 923 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 924 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 925 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 926 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 927 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 931 | { |
| 932 | int ret; |
| 933 | mdp_super_t *tmp1, *tmp2; |
| 934 | |
| 935 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 936 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 937 | |
| 938 | if (!tmp1 || !tmp2) { |
| 939 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 940 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | goto abort; |
| 942 | } |
| 943 | |
| 944 | *tmp1 = *sb1; |
| 945 | *tmp2 = *sb2; |
| 946 | |
| 947 | /* |
| 948 | * nr_disks is not constant |
| 949 | */ |
| 950 | tmp1->nr_disks = 0; |
| 951 | tmp2->nr_disks = 0; |
| 952 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 953 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 955 | kfree(tmp1); |
| 956 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return ret; |
| 958 | } |
| 959 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 960 | |
| 961 | static u32 md_csum_fold(u32 csum) |
| 962 | { |
| 963 | csum = (csum & 0xffff) + (csum >> 16); |
| 964 | return (csum & 0xffff) + (csum >> 16); |
| 965 | } |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 968 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 969 | u64 newcsum = 0; |
| 970 | u32 *sb32 = (u32*)sb; |
| 971 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | unsigned int disk_csum, csum; |
| 973 | |
| 974 | disk_csum = sb->sb_csum; |
| 975 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 976 | |
| 977 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 978 | newcsum += sb32[i]; |
| 979 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 980 | |
| 981 | |
| 982 | #ifdef CONFIG_ALPHA |
| 983 | /* This used to use csum_partial, which was wrong for several |
| 984 | * reasons including that different results are returned on |
| 985 | * different architectures. It isn't critical that we get exactly |
| 986 | * the same return value as before (we always csum_fold before |
| 987 | * testing, and that removes any differences). However as we |
| 988 | * know that csum_partial always returned a 16bit value on |
| 989 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 990 | */ |
| 991 | sb->sb_csum = md_csum_fold(disk_csum); |
| 992 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 994 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | return csum; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | /* |
| 1000 | * Handle superblock details. |
| 1001 | * We want to be able to handle multiple superblock formats |
| 1002 | * so we have a common interface to them all, and an array of |
| 1003 | * different handlers. |
| 1004 | * We rely on user-space to write the initial superblock, and support |
| 1005 | * reading and updating of superblocks. |
| 1006 | * Interface methods are: |
| 1007 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 1008 | * loads and validates a superblock on dev. |
| 1009 | * if refdev != NULL, compare superblocks on both devices |
| 1010 | * Return: |
| 1011 | * 0 - dev has a superblock that is compatible with refdev |
| 1012 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1013 | * so dev should be used as the refdev in future |
| 1014 | * -EINVAL superblock incompatible or invalid |
| 1015 | * -othererror e.g. -EIO |
| 1016 | * |
| 1017 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 1018 | * Verify that dev is acceptable into mddev. |
| 1019 | * The first time, mddev->raid_disks will be 0, and data from |
| 1020 | * dev should be merged in. Subsequent calls check that dev |
| 1021 | * is new enough. Return 0 or -EINVAL |
| 1022 | * |
| 1023 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 1024 | * Update the superblock for rdev with data in mddev |
| 1025 | * This does not write to disc. |
| 1026 | * |
| 1027 | */ |
| 1028 | |
| 1029 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1030 | char *name; |
| 1031 | struct module *owner; |
| 1032 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 1033 | int minor_version); |
| 1034 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 1035 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 1036 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1037 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | }; |
| 1039 | |
| 1040 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1041 | * Check that the given mddev has no bitmap. |
| 1042 | * |
| 1043 | * This function is called from the run method of all personalities that do not |
| 1044 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1045 | * has a bitmap. Otherwise, it returns 0. |
| 1046 | * |
| 1047 | */ |
| 1048 | int md_check_no_bitmap(mddev_t *mddev) |
| 1049 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1050 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1051 | return 0; |
| 1052 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1053 | mdname(mddev), mddev->pers->name); |
| 1054 | return 1; |
| 1055 | } |
| 1056 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1057 | |
| 1058 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | * load_super for 0.90.0 |
| 1060 | */ |
| 1061 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1062 | { |
| 1063 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1064 | mdp_super_t *sb; |
| 1065 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1068 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | * it's at the end of the disk. |
| 1070 | * |
| 1071 | * It also happens to be a multiple of 4Kb. |
| 1072 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1073 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1075 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | if (ret) return ret; |
| 1077 | |
| 1078 | ret = -EINVAL; |
| 1079 | |
| 1080 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1081 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1084 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1085 | b); |
| 1086 | goto abort; |
| 1087 | } |
| 1088 | |
| 1089 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1090 | sb->minor_version < 90 || |
| 1091 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1093 | sb->major_version, sb->minor_version, |
| 1094 | b); |
| 1095 | goto abort; |
| 1096 | } |
| 1097 | |
| 1098 | if (sb->raid_disks <= 0) |
| 1099 | goto abort; |
| 1100 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1101 | 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] | 1102 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1103 | b); |
| 1104 | goto abort; |
| 1105 | } |
| 1106 | |
| 1107 | rdev->preferred_minor = sb->md_minor; |
| 1108 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1109 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1110 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | if (sb->level == LEVEL_MULTIPATH) |
| 1113 | rdev->desc_nr = -1; |
| 1114 | else |
| 1115 | rdev->desc_nr = sb->this_disk.number; |
| 1116 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1117 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1119 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1121 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | if (!uuid_equal(refsb, sb)) { |
| 1123 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1124 | b, bdevname(refdev->bdev,b2)); |
| 1125 | goto abort; |
| 1126 | } |
| 1127 | if (!sb_equal(refsb, sb)) { |
| 1128 | printk(KERN_WARNING "md: %s has same UUID" |
| 1129 | " but different superblock to %s\n", |
| 1130 | b, bdevname(refdev->bdev, b2)); |
| 1131 | goto abort; |
| 1132 | } |
| 1133 | ev1 = md_event(sb); |
| 1134 | ev2 = md_event(refsb); |
| 1135 | if (ev1 > ev2) |
| 1136 | ret = 1; |
| 1137 | else |
| 1138 | ret = 0; |
| 1139 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1140 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1142 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1143 | /* "this cannot possibly happen" ... */ |
| 1144 | ret = -EINVAL; |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | abort: |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * validate_super for 0.90.0 |
| 1152 | */ |
| 1153 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1154 | { |
| 1155 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1156 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1157 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1159 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1160 | clear_bit(Faulty, &rdev->flags); |
| 1161 | clear_bit(In_sync, &rdev->flags); |
| 1162 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (mddev->raid_disks == 0) { |
| 1165 | mddev->major_version = 0; |
| 1166 | mddev->minor_version = sb->minor_version; |
| 1167 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1168 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1169 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | mddev->ctime = sb->ctime; |
| 1171 | mddev->utime = sb->utime; |
| 1172 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1173 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | mddev->layout = sb->layout; |
| 1175 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1176 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1177 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1178 | mddev->bitmap_info.offset = 0; |
| 1179 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1181 | if (mddev->minor_version >= 91) { |
| 1182 | mddev->reshape_position = sb->reshape_position; |
| 1183 | mddev->delta_disks = sb->delta_disks; |
| 1184 | mddev->new_level = sb->new_level; |
| 1185 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1186 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1187 | } else { |
| 1188 | mddev->reshape_position = MaxSector; |
| 1189 | mddev->delta_disks = 0; |
| 1190 | mddev->new_level = mddev->level; |
| 1191 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1192 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1196 | mddev->recovery_cp = MaxSector; |
| 1197 | else { |
| 1198 | if (sb->events_hi == sb->cp_events_hi && |
| 1199 | sb->events_lo == sb->cp_events_lo) { |
| 1200 | mddev->recovery_cp = sb->recovery_cp; |
| 1201 | } else |
| 1202 | mddev->recovery_cp = 0; |
| 1203 | } |
| 1204 | |
| 1205 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1206 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1207 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1208 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1209 | |
| 1210 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1213 | mddev->bitmap_info.file == NULL) |
| 1214 | mddev->bitmap_info.offset = |
| 1215 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1216 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1217 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1218 | /* Insist on good event counter while assembling, except |
| 1219 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1221 | if (sb->disks[rdev->desc_nr].state & ( |
| 1222 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1223 | if (ev1 < mddev->events) |
| 1224 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1225 | } else if (mddev->bitmap) { |
| 1226 | /* if adding to array with a bitmap, then we can accept an |
| 1227 | * older device ... but not too old. |
| 1228 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1229 | if (ev1 < mddev->bitmap->events_cleared) |
| 1230 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1231 | } else { |
| 1232 | if (ev1 < mddev->events) |
| 1233 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1234 | return 0; |
| 1235 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | desc = sb->disks + rdev->desc_nr; |
| 1239 | |
| 1240 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1241 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1242 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1243 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1244 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1246 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1247 | /* active but not in sync implies recovery up to |
| 1248 | * reshape position. We don't know exactly where |
| 1249 | * that is, so set to zero for now */ |
| 1250 | if (mddev->minor_version >= 91) { |
| 1251 | rdev->recovery_offset = 0; |
| 1252 | rdev->raid_disk = desc->raid_disk; |
| 1253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1255 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1256 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1257 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1258 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * sync_super for 0.90.0 |
| 1264 | */ |
| 1265 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1266 | { |
| 1267 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | mdk_rdev_t *rdev2; |
| 1269 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* make rdev->sb match mddev data.. |
| 1273 | * |
| 1274 | * 1/ zero out disks |
| 1275 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1276 | * 3/ any empty disks < next_spare become removed |
| 1277 | * |
| 1278 | * disks[0] gets initialised to REMOVED because |
| 1279 | * we cannot be sure from other fields if it has |
| 1280 | * been initialised or not. |
| 1281 | */ |
| 1282 | int i; |
| 1283 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1284 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1285 | rdev->sb_size = MD_SB_BYTES; |
| 1286 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1287 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
| 1289 | memset(sb, 0, sizeof(*sb)); |
| 1290 | |
| 1291 | sb->md_magic = MD_SB_MAGIC; |
| 1292 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | sb->patch_version = mddev->patch_version; |
| 1294 | sb->gvalid_words = 0; /* ignored */ |
| 1295 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1296 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1297 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1298 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1299 | |
| 1300 | sb->ctime = mddev->ctime; |
| 1301 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1302 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | sb->raid_disks = mddev->raid_disks; |
| 1304 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1305 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | sb->utime = mddev->utime; |
| 1307 | sb->state = 0; |
| 1308 | sb->events_hi = (mddev->events>>32); |
| 1309 | sb->events_lo = (u32)mddev->events; |
| 1310 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1311 | if (mddev->reshape_position == MaxSector) |
| 1312 | sb->minor_version = 90; |
| 1313 | else { |
| 1314 | sb->minor_version = 91; |
| 1315 | sb->reshape_position = mddev->reshape_position; |
| 1316 | sb->new_level = mddev->new_level; |
| 1317 | sb->delta_disks = mddev->delta_disks; |
| 1318 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1319 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1320 | } |
| 1321 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | if (mddev->in_sync) |
| 1323 | { |
| 1324 | sb->recovery_cp = mddev->recovery_cp; |
| 1325 | sb->cp_events_hi = (mddev->events>>32); |
| 1326 | sb->cp_events_lo = (u32)mddev->events; |
| 1327 | if (mddev->recovery_cp == MaxSector) |
| 1328 | sb->state = (1<< MD_SB_CLEAN); |
| 1329 | } else |
| 1330 | sb->recovery_cp = 0; |
| 1331 | |
| 1332 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1333 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1335 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1336 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1339 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1341 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1342 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1343 | |
| 1344 | if (rdev2->raid_disk >= 0 && |
| 1345 | sb->minor_version >= 91) |
| 1346 | /* we have nowhere to store the recovery_offset, |
| 1347 | * but if it is not below the reshape_position, |
| 1348 | * we can piggy-back on that. |
| 1349 | */ |
| 1350 | is_active = 1; |
| 1351 | if (rdev2->raid_disk < 0 || |
| 1352 | test_bit(Faulty, &rdev2->flags)) |
| 1353 | is_active = 0; |
| 1354 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1355 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1357 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1358 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | d = &sb->disks[rdev2->desc_nr]; |
| 1360 | nr_disks++; |
| 1361 | d->number = rdev2->desc_nr; |
| 1362 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1363 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1364 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | d->raid_disk = rdev2->raid_disk; |
| 1366 | else |
| 1367 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1368 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1370 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1372 | if (test_bit(In_sync, &rdev2->flags)) |
| 1373 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | active++; |
| 1375 | working++; |
| 1376 | } else { |
| 1377 | d->state = 0; |
| 1378 | spare++; |
| 1379 | working++; |
| 1380 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1381 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1382 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1385 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1386 | mdp_disk_t *d = &sb->disks[i]; |
| 1387 | if (d->state == 0 && d->number == 0) { |
| 1388 | d->number = i; |
| 1389 | d->raid_disk = i; |
| 1390 | d->state = (1<<MD_DISK_REMOVED); |
| 1391 | d->state |= (1<<MD_DISK_FAULTY); |
| 1392 | failed++; |
| 1393 | } |
| 1394 | } |
| 1395 | sb->nr_disks = nr_disks; |
| 1396 | sb->active_disks = active; |
| 1397 | sb->working_disks = working; |
| 1398 | sb->failed_disks = failed; |
| 1399 | sb->spare_disks = spare; |
| 1400 | |
| 1401 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1402 | sb->sb_csum = calc_sb_csum(sb); |
| 1403 | } |
| 1404 | |
| 1405 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1406 | * rdev_size_change for 0.90.0 |
| 1407 | */ |
| 1408 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1409 | super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1410 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1411 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1412 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1413 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1414 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1415 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1416 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1417 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1418 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1419 | rdev->sb_page); |
| 1420 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1421 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | * version 1 superblock |
| 1427 | */ |
| 1428 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1429 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1431 | __le32 disk_csum; |
| 1432 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | unsigned long long newcsum; |
| 1434 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1435 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | int i; |
| 1437 | |
| 1438 | disk_csum = sb->sb_csum; |
| 1439 | sb->sb_csum = 0; |
| 1440 | newcsum = 0; |
| 1441 | for (i=0; size>=4; size -= 4 ) |
| 1442 | newcsum += le32_to_cpu(*isuper++); |
| 1443 | |
| 1444 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1445 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1448 | sb->sb_csum = disk_csum; |
| 1449 | return cpu_to_le32(csum); |
| 1450 | } |
| 1451 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1452 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1453 | int acknowledged); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1455 | { |
| 1456 | struct mdp_superblock_1 *sb; |
| 1457 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1458 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1460 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
| 1462 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1463 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | * It is always aligned to a 4K boundary and |
| 1465 | * depeding on minor_version, it can be: |
| 1466 | * 0: At least 8K, but less than 12K, from end of device |
| 1467 | * 1: At start of device |
| 1468 | * 2: 4K from start of device. |
| 1469 | */ |
| 1470 | switch(minor_version) { |
| 1471 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1472 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1473 | sb_start -= 8*2; |
| 1474 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | break; |
| 1476 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1477 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1480 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | default: |
| 1483 | return -EINVAL; |
| 1484 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1485 | rdev->sb_start = sb_start; |
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 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1488 | * and it is safe to read 4k, so we do that |
| 1489 | */ |
| 1490 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | if (ret) return ret; |
| 1492 | |
| 1493 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1494 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1497 | sb->major_version != cpu_to_le32(1) || |
| 1498 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1499 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1500 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | return -EINVAL; |
| 1502 | |
| 1503 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1504 | printk("md: invalid superblock checksum on %s\n", |
| 1505 | bdevname(rdev->bdev,b)); |
| 1506 | return -EINVAL; |
| 1507 | } |
| 1508 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1509 | printk("md: data_size too small on %s\n", |
| 1510 | bdevname(rdev->bdev,b)); |
| 1511 | return -EINVAL; |
| 1512 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | rdev->preferred_minor = 0xffff; |
| 1515 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1516 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1518 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1519 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1520 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1521 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1522 | |
| 1523 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1524 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1525 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1526 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1527 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1528 | rdev->desc_nr = -1; |
| 1529 | else |
| 1530 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1531 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1532 | if (!rdev->bb_page) { |
| 1533 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1534 | if (!rdev->bb_page) |
| 1535 | return -ENOMEM; |
| 1536 | } |
| 1537 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1538 | rdev->badblocks.count == 0) { |
| 1539 | /* need to load the bad block list. |
| 1540 | * Currently we limit it to one page. |
| 1541 | */ |
| 1542 | s32 offset; |
| 1543 | sector_t bb_sector; |
| 1544 | u64 *bbp; |
| 1545 | int i; |
| 1546 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1547 | if (sectors > (PAGE_SIZE / 512)) |
| 1548 | return -EINVAL; |
| 1549 | offset = le32_to_cpu(sb->bblog_offset); |
| 1550 | if (offset == 0) |
| 1551 | return -EINVAL; |
| 1552 | bb_sector = (long long)offset; |
| 1553 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1554 | rdev->bb_page, READ, true)) |
| 1555 | return -EIO; |
| 1556 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1557 | rdev->badblocks.shift = sb->bblog_shift; |
| 1558 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1559 | u64 bb = le64_to_cpu(*bbp); |
| 1560 | int count = bb & (0x3ff); |
| 1561 | u64 sector = bb >> 10; |
| 1562 | sector <<= sb->bblog_shift; |
| 1563 | count <<= sb->bblog_shift; |
| 1564 | if (bb + 1 == 0) |
| 1565 | break; |
| 1566 | if (md_set_badblocks(&rdev->badblocks, |
| 1567 | sector, count, 1) == 0) |
| 1568 | return -EINVAL; |
| 1569 | } |
| 1570 | } else if (sb->bblog_offset == 0) |
| 1571 | rdev->badblocks.shift = -1; |
| 1572 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1573 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1574 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1575 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1577 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1580 | sb->level != refsb->level || |
| 1581 | sb->layout != refsb->layout || |
| 1582 | sb->chunksize != refsb->chunksize) { |
| 1583 | printk(KERN_WARNING "md: %s has strangely different" |
| 1584 | " superblock to %s\n", |
| 1585 | bdevname(rdev->bdev,b), |
| 1586 | bdevname(refdev->bdev,b2)); |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | ev1 = le64_to_cpu(sb->events); |
| 1590 | ev2 = le64_to_cpu(refsb->events); |
| 1591 | |
| 1592 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1593 | ret = 1; |
| 1594 | else |
| 1595 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1597 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1598 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1599 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1601 | rdev->sectors = rdev->sb_start; |
| 1602 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1604 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1605 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1606 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1607 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1611 | { |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1612 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1613 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1615 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1616 | clear_bit(Faulty, &rdev->flags); |
| 1617 | clear_bit(In_sync, &rdev->flags); |
| 1618 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | if (mddev->raid_disks == 0) { |
| 1621 | mddev->major_version = 1; |
| 1622 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1623 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1624 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1626 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1627 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1628 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | mddev->layout = le32_to_cpu(sb->layout); |
| 1630 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1631 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1632 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1633 | mddev->bitmap_info.offset = 0; |
| 1634 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
| 1636 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1637 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1638 | |
| 1639 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1640 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1641 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1642 | mddev->bitmap_info.file == NULL ) |
| 1643 | mddev->bitmap_info.offset = |
| 1644 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1645 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1646 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1647 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1648 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1649 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1650 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1651 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1652 | } else { |
| 1653 | mddev->reshape_position = MaxSector; |
| 1654 | mddev->delta_disks = 0; |
| 1655 | mddev->new_level = mddev->level; |
| 1656 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1657 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1658 | } |
| 1659 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1660 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1661 | /* Insist of good event counter while assembling, except for |
| 1662 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1664 | if (rdev->desc_nr >= 0 && |
| 1665 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1666 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1667 | if (ev1 < mddev->events) |
| 1668 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1669 | } else if (mddev->bitmap) { |
| 1670 | /* If adding to array with a bitmap, then we can accept an |
| 1671 | * older device, but not too old. |
| 1672 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1673 | if (ev1 < mddev->bitmap->events_cleared) |
| 1674 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1675 | } else { |
| 1676 | if (ev1 < mddev->events) |
| 1677 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1678 | return 0; |
| 1679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1681 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1682 | if (rdev->desc_nr < 0 || |
| 1683 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1684 | role = 0xffff; |
| 1685 | rdev->desc_nr = -1; |
| 1686 | } else |
| 1687 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | switch(role) { |
| 1689 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | break; |
| 1691 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1692 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | break; |
| 1694 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1695 | if ((le32_to_cpu(sb->feature_map) & |
| 1696 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1697 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1698 | else |
| 1699 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | rdev->raid_disk = role; |
| 1701 | break; |
| 1702 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1703 | if (sb->devflags & WriteMostly1) |
| 1704 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1705 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1706 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1712 | { |
| 1713 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | mdk_rdev_t *rdev2; |
| 1715 | int max_dev, i; |
| 1716 | /* make rdev->sb match mddev and rdev data. */ |
| 1717 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1718 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
| 1720 | sb->feature_map = 0; |
| 1721 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1722 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1725 | |
| 1726 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1727 | sb->events = cpu_to_le64(mddev->events); |
| 1728 | if (mddev->in_sync) |
| 1729 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1730 | else |
| 1731 | sb->resync_offset = cpu_to_le64(0); |
| 1732 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1733 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1734 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1735 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1736 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1737 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1738 | sb->level = cpu_to_le32(mddev->level); |
| 1739 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1740 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1741 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1742 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1743 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1744 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1745 | |
| 1746 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1747 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1748 | sb->feature_map |= |
| 1749 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1750 | sb->recovery_offset = |
| 1751 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1754 | if (mddev->reshape_position != MaxSector) { |
| 1755 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1756 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1757 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1758 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1759 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1760 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1761 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1762 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1763 | if (rdev->badblocks.count == 0) |
| 1764 | /* Nothing to do for bad blocks*/ ; |
| 1765 | else if (sb->bblog_offset == 0) |
| 1766 | /* Cannot record bad blocks on this device */ |
| 1767 | md_error(mddev, rdev); |
| 1768 | else { |
| 1769 | struct badblocks *bb = &rdev->badblocks; |
| 1770 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1771 | u64 *p = bb->page; |
| 1772 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1773 | if (bb->changed) { |
| 1774 | unsigned seq; |
| 1775 | |
| 1776 | retry: |
| 1777 | seq = read_seqbegin(&bb->lock); |
| 1778 | |
| 1779 | memset(bbp, 0xff, PAGE_SIZE); |
| 1780 | |
| 1781 | for (i = 0 ; i < bb->count ; i++) { |
| 1782 | u64 internal_bb = *p++; |
| 1783 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1784 | | BB_LEN(internal_bb)); |
| 1785 | *bbp++ = cpu_to_le64(store_bb); |
| 1786 | } |
| 1787 | if (read_seqretry(&bb->lock, seq)) |
| 1788 | goto retry; |
| 1789 | |
| 1790 | bb->sector = (rdev->sb_start + |
| 1791 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1792 | bb->size = le16_to_cpu(sb->bblog_size); |
| 1793 | bb->changed = 0; |
| 1794 | } |
| 1795 | } |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1798 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | if (rdev2->desc_nr+1 > max_dev) |
| 1800 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1801 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1802 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1803 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1804 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1805 | rdev->sb_size = max_dev * 2 + 256; |
| 1806 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1807 | if (rdev->sb_size & bmask) |
| 1808 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1809 | } else |
| 1810 | max_dev = le32_to_cpu(sb->max_dev); |
| 1811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | for (i=0; i<max_dev;i++) |
| 1813 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1814 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1815 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1817 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1819 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1821 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1822 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | else |
| 1824 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1825 | } |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1828 | } |
| 1829 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1830 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1831 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1832 | { |
| 1833 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1834 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1835 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1836 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1837 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1838 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1839 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1840 | max_sectors -= rdev->data_offset; |
| 1841 | if (!num_sectors || num_sectors > max_sectors) |
| 1842 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1843 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1844 | /* minor version 0 with bitmap we can't move */ |
| 1845 | return 0; |
| 1846 | } else { |
| 1847 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1848 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1849 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1850 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1851 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1852 | if (!num_sectors || num_sectors > max_sectors) |
| 1853 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1854 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1855 | } |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1856 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1857 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1858 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1859 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1860 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1861 | rdev->sb_page); |
| 1862 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1863 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1866 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | [0] = { |
| 1868 | .name = "0.90.0", |
| 1869 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1870 | .load_super = super_90_load, |
| 1871 | .validate_super = super_90_validate, |
| 1872 | .sync_super = super_90_sync, |
| 1873 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | }, |
| 1875 | [1] = { |
| 1876 | .name = "md-1", |
| 1877 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1878 | .load_super = super_1_load, |
| 1879 | .validate_super = super_1_validate, |
| 1880 | .sync_super = super_1_sync, |
| 1881 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | }, |
| 1883 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1885 | static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1886 | { |
| 1887 | if (mddev->sync_super) { |
| 1888 | mddev->sync_super(mddev, rdev); |
| 1889 | return; |
| 1890 | } |
| 1891 | |
| 1892 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1893 | |
| 1894 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1895 | } |
| 1896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1898 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1899 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1901 | rcu_read_lock(); |
| 1902 | rdev_for_each_rcu(rdev, mddev1) |
| 1903 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1904 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1905 | rdev2->bdev->bd_contains) { |
| 1906 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1907 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1908 | } |
| 1909 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | static LIST_HEAD(pending_raid_disks); |
| 1914 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1915 | /* |
| 1916 | * Try to register data integrity profile for an mddev |
| 1917 | * |
| 1918 | * This is called when an array is started and after a disk has been kicked |
| 1919 | * from the array. It only succeeds if all working and active component devices |
| 1920 | * are integrity capable with matching profiles. |
| 1921 | */ |
| 1922 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1923 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1924 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1925 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1926 | if (list_empty(&mddev->disks)) |
| 1927 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1928 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1929 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1930 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1931 | /* skip spares and non-functional disks */ |
| 1932 | if (test_bit(Faulty, &rdev->flags)) |
| 1933 | continue; |
| 1934 | if (rdev->raid_disk < 0) |
| 1935 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1936 | if (!reference) { |
| 1937 | /* Use the first rdev as the reference */ |
| 1938 | reference = rdev; |
| 1939 | continue; |
| 1940 | } |
| 1941 | /* does this rdev's profile match the reference profile? */ |
| 1942 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1943 | rdev->bdev->bd_disk) < 0) |
| 1944 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1945 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1946 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1947 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1948 | /* |
| 1949 | * All component devices are integrity capable and have matching |
| 1950 | * profiles, register the common profile for the md device. |
| 1951 | */ |
| 1952 | if (blk_integrity_register(mddev->gendisk, |
| 1953 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1954 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1955 | mdname(mddev)); |
| 1956 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1957 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1958 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1959 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1960 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1961 | mdname(mddev)); |
| 1962 | return -EINVAL; |
| 1963 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1964 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1965 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1966 | EXPORT_SYMBOL(md_integrity_register); |
| 1967 | |
| 1968 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1969 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1970 | { |
| 1971 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1972 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1973 | |
| 1974 | if (!bi_mddev) /* nothing to do */ |
| 1975 | return; |
| 1976 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1977 | return; |
| 1978 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1979 | rdev->bdev->bd_disk) >= 0) |
| 1980 | return; |
| 1981 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1982 | blk_integrity_unregister(mddev->gendisk); |
| 1983 | } |
| 1984 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1987 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1988 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1989 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1990 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1991 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
| 1993 | if (rdev->mddev) { |
| 1994 | MD_BUG(); |
| 1995 | return -EINVAL; |
| 1996 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1997 | |
| 1998 | /* prevent duplicates */ |
| 1999 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2000 | return -EEXIST; |
| 2001 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2002 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2003 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2004 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2005 | if (mddev->pers) { |
| 2006 | /* Cannot change size, so fail |
| 2007 | * If mddev->level <= 0, then we don't care |
| 2008 | * about aligning sizes (e.g. linear) |
| 2009 | */ |
| 2010 | if (mddev->level > 0) |
| 2011 | return -ENOSPC; |
| 2012 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2013 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
| 2016 | /* Verify rdev->desc_nr is unique. |
| 2017 | * If it is -1, assign a free number, else |
| 2018 | * check number is not in use |
| 2019 | */ |
| 2020 | if (rdev->desc_nr < 0) { |
| 2021 | int choice = 0; |
| 2022 | if (mddev->pers) choice = mddev->raid_disks; |
| 2023 | while (find_rdev_nr(mddev, choice)) |
| 2024 | choice++; |
| 2025 | rdev->desc_nr = choice; |
| 2026 | } else { |
| 2027 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2028 | return -EBUSY; |
| 2029 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2030 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2031 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2032 | mdname(mddev), mddev->max_disks); |
| 2033 | return -EBUSY; |
| 2034 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2035 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2036 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2037 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2040 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2041 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2042 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2043 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2044 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2045 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2046 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2047 | /* failure here is OK */; |
| 2048 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2049 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2050 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2051 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2052 | |
| 2053 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2054 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2057 | |
| 2058 | fail: |
| 2059 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2060 | b, mdname(mddev)); |
| 2061 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2064 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2065 | { |
| 2066 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 2067 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2068 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 2072 | { |
| 2073 | char b[BDEVNAME_SIZE]; |
| 2074 | if (!rdev->mddev) { |
| 2075 | MD_BUG(); |
| 2076 | return; |
| 2077 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2078 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2079 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2081 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2082 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2083 | sysfs_put(rdev->sysfs_state); |
| 2084 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2085 | kfree(rdev->badblocks.page); |
| 2086 | rdev->badblocks.count = 0; |
| 2087 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2088 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2089 | * writing to 'remove' to "dev/state". We also need |
| 2090 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2091 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2092 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2093 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2094 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2095 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * prevent the device from being mounted, repartitioned or |
| 2100 | * otherwise reused by a RAID array (or any other kernel |
| 2101 | * subsystem), by bd_claiming the device. |
| 2102 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2103 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | { |
| 2105 | int err = 0; |
| 2106 | struct block_device *bdev; |
| 2107 | char b[BDEVNAME_SIZE]; |
| 2108 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2109 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 2110 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | if (IS_ERR(bdev)) { |
| 2112 | printk(KERN_ERR "md: could not open %s.\n", |
| 2113 | __bdevname(dev, b)); |
| 2114 | return PTR_ERR(bdev); |
| 2115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | rdev->bdev = bdev; |
| 2117 | return err; |
| 2118 | } |
| 2119 | |
| 2120 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 2121 | { |
| 2122 | struct block_device *bdev = rdev->bdev; |
| 2123 | rdev->bdev = NULL; |
| 2124 | if (!bdev) |
| 2125 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2126 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | void md_autodetect_dev(dev_t dev); |
| 2130 | |
| 2131 | static void export_rdev(mdk_rdev_t * rdev) |
| 2132 | { |
| 2133 | char b[BDEVNAME_SIZE]; |
| 2134 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2135 | bdevname(rdev->bdev,b)); |
| 2136 | if (rdev->mddev) |
| 2137 | MD_BUG(); |
| 2138 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2140 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2141 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | #endif |
| 2143 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2144 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 2148 | { |
| 2149 | unbind_rdev_from_array(rdev); |
| 2150 | export_rdev(rdev); |
| 2151 | } |
| 2152 | |
| 2153 | static void export_array(mddev_t *mddev) |
| 2154 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2155 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2157 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | if (!rdev->mddev) { |
| 2159 | MD_BUG(); |
| 2160 | continue; |
| 2161 | } |
| 2162 | kick_rdev_from_array(rdev); |
| 2163 | } |
| 2164 | if (!list_empty(&mddev->disks)) |
| 2165 | MD_BUG(); |
| 2166 | mddev->raid_disks = 0; |
| 2167 | mddev->major_version = 0; |
| 2168 | } |
| 2169 | |
| 2170 | static void print_desc(mdp_disk_t *desc) |
| 2171 | { |
| 2172 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2173 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2174 | } |
| 2175 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2176 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | { |
| 2178 | int i; |
| 2179 | |
| 2180 | printk(KERN_INFO |
| 2181 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2182 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2183 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2184 | sb->ctime); |
| 2185 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2186 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2187 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2188 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2189 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2190 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2191 | sb->failed_disks, sb->spare_disks, |
| 2192 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2193 | |
| 2194 | printk(KERN_INFO); |
| 2195 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2196 | mdp_disk_t *desc; |
| 2197 | |
| 2198 | desc = sb->disks + i; |
| 2199 | if (desc->number || desc->major || desc->minor || |
| 2200 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2201 | printk(" D %2d: ", i); |
| 2202 | print_desc(desc); |
| 2203 | } |
| 2204 | } |
| 2205 | printk(KERN_INFO "md: THIS: "); |
| 2206 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2209 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2210 | { |
| 2211 | __u8 *uuid; |
| 2212 | |
| 2213 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2214 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2215 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2216 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2217 | le32_to_cpu(sb->major_version), |
| 2218 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2219 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2220 | sb->set_name, |
| 2221 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2222 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2223 | |
| 2224 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2225 | printk(KERN_INFO |
| 2226 | "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] | 2227 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2228 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2229 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2230 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2231 | le32_to_cpu(sb->level), |
| 2232 | (unsigned long long)le64_to_cpu(sb->size), |
| 2233 | le32_to_cpu(sb->raid_disks), |
| 2234 | le32_to_cpu(sb->layout), |
| 2235 | le32_to_cpu(sb->chunksize), |
| 2236 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2237 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2238 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2239 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2240 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2241 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2242 | sb->devflags, |
| 2243 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2244 | (unsigned long long)le64_to_cpu(sb->events), |
| 2245 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2246 | le32_to_cpu(sb->sb_csum), |
| 2247 | le32_to_cpu(sb->max_dev) |
| 2248 | ); |
| 2249 | } |
| 2250 | |
| 2251 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | { |
| 2253 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2254 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2255 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2256 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2257 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2259 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2260 | switch (major_version) { |
| 2261 | case 0: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2262 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2263 | break; |
| 2264 | case 1: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2265 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2266 | break; |
| 2267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } else |
| 2269 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2270 | } |
| 2271 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2272 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2274 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | mdk_rdev_t *rdev; |
| 2276 | mddev_t *mddev; |
| 2277 | char b[BDEVNAME_SIZE]; |
| 2278 | |
| 2279 | printk("\n"); |
| 2280 | printk("md: **********************************\n"); |
| 2281 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2282 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2283 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2285 | if (mddev->bitmap) |
| 2286 | bitmap_print_sb(mddev->bitmap); |
| 2287 | else |
| 2288 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2289 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2291 | printk("\n"); |
| 2292 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2293 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2294 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | } |
| 2296 | printk("md: **********************************\n"); |
| 2297 | printk("\n"); |
| 2298 | } |
| 2299 | |
| 2300 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2301 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2303 | /* Update each superblock (in-memory image), but |
| 2304 | * if we are allowed to, skip spares which already |
| 2305 | * have the right event counter, or have one earlier |
| 2306 | * (which would mean they aren't being marked as dirty |
| 2307 | * with the rest of the array) |
| 2308 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2310 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2311 | if (rdev->sb_events == mddev->events || |
| 2312 | (nospares && |
| 2313 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2314 | rdev->sb_events+1 == mddev->events)) { |
| 2315 | /* Don't update this superblock */ |
| 2316 | rdev->sb_loaded = 2; |
| 2317 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2318 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2319 | rdev->sb_loaded = 1; |
| 2320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2324 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2327 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2328 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2329 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2332 | /* First make sure individual recovery_offsets are correct */ |
| 2333 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2334 | if (rdev->raid_disk >= 0 && |
| 2335 | mddev->delta_disks >= 0 && |
| 2336 | !test_bit(In_sync, &rdev->flags) && |
| 2337 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2338 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2339 | |
| 2340 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2341 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2342 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2343 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2344 | if (!mddev->external) |
| 2345 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2346 | wake_up(&mddev->sb_wait); |
| 2347 | return; |
| 2348 | } |
| 2349 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2350 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2351 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2352 | mddev->utime = get_seconds(); |
| 2353 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2354 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2355 | force_change = 1; |
| 2356 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2357 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2358 | * though if events isn't the right even/odd, we will have to do |
| 2359 | * spares after all |
| 2360 | */ |
| 2361 | nospares = 1; |
| 2362 | if (force_change) |
| 2363 | nospares = 0; |
| 2364 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2365 | /* If the array is degraded, then skipping spares is both |
| 2366 | * dangerous and fairly pointless. |
| 2367 | * Dangerous because a device that was removed from the array |
| 2368 | * might have a event_count that still looks up-to-date, |
| 2369 | * so it can be re-added without a resync. |
| 2370 | * Pointless because if there are any spares to skip, |
| 2371 | * then a recovery will happen and soon that array won't |
| 2372 | * be degraded any more and the spare can go back to sleep then. |
| 2373 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2374 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2375 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2376 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2377 | |
| 2378 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2379 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2380 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2381 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2382 | && mddev->can_decrease_events |
| 2383 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2384 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2385 | mddev->can_decrease_events = 0; |
| 2386 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2387 | /* otherwise we have to go forward and ... */ |
| 2388 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2389 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
| 2392 | if (!mddev->events) { |
| 2393 | /* |
| 2394 | * oops, this 64-bit counter should never wrap. |
| 2395 | * Either we are in around ~1 trillion A.C., assuming |
| 2396 | * 1 reboot per second, or we have a bug: |
| 2397 | */ |
| 2398 | MD_BUG(); |
| 2399 | mddev->events --; |
| 2400 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2401 | |
| 2402 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2403 | if (rdev->badblocks.changed) |
| 2404 | any_badblocks_changed++; |
| 2405 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2406 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2407 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | |
| 2409 | dprintk(KERN_INFO |
| 2410 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2411 | mdname(mddev),mddev->in_sync); |
| 2412 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2413 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2414 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | char b[BDEVNAME_SIZE]; |
| 2416 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2417 | if (rdev->sb_loaded != 1) |
| 2418 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2419 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | dprintk("(skipping faulty "); |
| 2421 | |
| 2422 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2423 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2424 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2425 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2426 | rdev->sb_page); |
| 2427 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2428 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2429 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2430 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2431 | if (rdev->badblocks.size) { |
| 2432 | md_super_write(mddev, rdev, |
| 2433 | rdev->badblocks.sector, |
| 2434 | rdev->badblocks.size << 9, |
| 2435 | rdev->bb_page); |
| 2436 | rdev->badblocks.size = 0; |
| 2437 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | } else |
| 2440 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2441 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | /* only need to write one superblock... */ |
| 2443 | break; |
| 2444 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2445 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2446 | /* 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] | 2447 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2448 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2449 | if (mddev->in_sync != sync_req || |
| 2450 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2451 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2452 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2453 | goto repeat; |
| 2454 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2455 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2456 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2457 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2458 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2459 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2460 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2461 | if (any_badblocks_changed) |
| 2462 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2463 | md_ack_all_badblocks(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2466 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2467 | * We want to accept with case. For this we use cmd_match. |
| 2468 | */ |
| 2469 | static int cmd_match(const char *cmd, const char *str) |
| 2470 | { |
| 2471 | /* See if cmd, written into a sysfs file, matches |
| 2472 | * str. They must either be the same, or cmd can |
| 2473 | * have a trailing newline |
| 2474 | */ |
| 2475 | while (*cmd && *str && *cmd == *str) { |
| 2476 | cmd++; |
| 2477 | str++; |
| 2478 | } |
| 2479 | if (*cmd == '\n') |
| 2480 | cmd++; |
| 2481 | if (*str || *cmd) |
| 2482 | return 0; |
| 2483 | return 1; |
| 2484 | } |
| 2485 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2486 | struct rdev_sysfs_entry { |
| 2487 | struct attribute attr; |
| 2488 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2489 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2490 | }; |
| 2491 | |
| 2492 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2493 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2494 | { |
| 2495 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2496 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2497 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2498 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2499 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2500 | sep = ","; |
| 2501 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2502 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2503 | len += sprintf(page+len, "%sin_sync",sep); |
| 2504 | sep = ","; |
| 2505 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2506 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2507 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2508 | sep = ","; |
| 2509 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2510 | if (test_bit(Blocked, &rdev->flags)) { |
| 2511 | len += sprintf(page+len, "%sblocked", sep); |
| 2512 | sep = ","; |
| 2513 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2514 | if (!test_bit(Faulty, &rdev->flags) && |
| 2515 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2516 | len += sprintf(page+len, "%sspare", sep); |
| 2517 | sep = ","; |
| 2518 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame^] | 2519 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2520 | len += sprintf(page+len, "%swrite_error", sep); |
| 2521 | sep = ","; |
| 2522 | } |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2523 | return len+sprintf(page+len, "\n"); |
| 2524 | } |
| 2525 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2526 | static ssize_t |
| 2527 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2528 | { |
| 2529 | /* can write |
| 2530 | * faulty - simulates and error |
| 2531 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2532 | * writemostly - sets write_mostly |
| 2533 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2534 | * blocked - sets the Blocked flag |
| 2535 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2536 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame^] | 2537 | * write_error - sets WriteErrorSeen |
| 2538 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2539 | */ |
| 2540 | int err = -EINVAL; |
| 2541 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2542 | md_error(rdev->mddev, rdev); |
| 2543 | err = 0; |
| 2544 | } else if (cmd_match(buf, "remove")) { |
| 2545 | if (rdev->raid_disk >= 0) |
| 2546 | err = -EBUSY; |
| 2547 | else { |
| 2548 | mddev_t *mddev = rdev->mddev; |
| 2549 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2550 | if (mddev->pers) |
| 2551 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2552 | md_new_event(mddev); |
| 2553 | err = 0; |
| 2554 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2555 | } else if (cmd_match(buf, "writemostly")) { |
| 2556 | set_bit(WriteMostly, &rdev->flags); |
| 2557 | err = 0; |
| 2558 | } else if (cmd_match(buf, "-writemostly")) { |
| 2559 | clear_bit(WriteMostly, &rdev->flags); |
| 2560 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2561 | } else if (cmd_match(buf, "blocked")) { |
| 2562 | set_bit(Blocked, &rdev->flags); |
| 2563 | err = 0; |
| 2564 | } else if (cmd_match(buf, "-blocked")) { |
| 2565 | clear_bit(Blocked, &rdev->flags); |
| 2566 | wake_up(&rdev->blocked_wait); |
| 2567 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2568 | md_wakeup_thread(rdev->mddev->thread); |
| 2569 | |
| 2570 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2571 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2572 | set_bit(In_sync, &rdev->flags); |
| 2573 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame^] | 2574 | } else if (cmd_match(buf, "write_error")) { |
| 2575 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2576 | err = 0; |
| 2577 | } else if (cmd_match(buf, "-write_error")) { |
| 2578 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2579 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2580 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2581 | if (!err) |
| 2582 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2583 | return err ? err : len; |
| 2584 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2585 | static struct rdev_sysfs_entry rdev_state = |
| 2586 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2587 | |
| 2588 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2589 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2590 | { |
| 2591 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2592 | } |
| 2593 | |
| 2594 | static ssize_t |
| 2595 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2596 | { |
| 2597 | char *e; |
| 2598 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2599 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2600 | atomic_set(&rdev->corrected_errors, n); |
| 2601 | return len; |
| 2602 | } |
| 2603 | return -EINVAL; |
| 2604 | } |
| 2605 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2606 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2607 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2608 | static ssize_t |
| 2609 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2610 | { |
| 2611 | if (rdev->raid_disk < 0) |
| 2612 | return sprintf(page, "none\n"); |
| 2613 | else |
| 2614 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2615 | } |
| 2616 | |
| 2617 | static ssize_t |
| 2618 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2619 | { |
| 2620 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2621 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2622 | int slot = simple_strtoul(buf, &e, 10); |
| 2623 | if (strncmp(buf, "none", 4)==0) |
| 2624 | slot = -1; |
| 2625 | else if (e==buf || (*e && *e!= '\n')) |
| 2626 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2627 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2628 | /* Setting 'slot' on an active array requires also |
| 2629 | * updating the 'rd%d' link, and communicating |
| 2630 | * with the personality with ->hot_*_disk. |
| 2631 | * For now we only support removing |
| 2632 | * failed/spare devices. This normally happens automatically, |
| 2633 | * but not when the metadata is externally managed. |
| 2634 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2635 | if (rdev->raid_disk == -1) |
| 2636 | return -EEXIST; |
| 2637 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2638 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2639 | return -EINVAL; |
| 2640 | err = rdev->mddev->pers-> |
| 2641 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2642 | if (err) |
| 2643 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2644 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2645 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2646 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2647 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2648 | } else if (rdev->mddev->pers) { |
| 2649 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2650 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2651 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2652 | */ |
| 2653 | |
| 2654 | if (rdev->raid_disk != -1) |
| 2655 | return -EBUSY; |
| 2656 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2657 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2658 | return -EBUSY; |
| 2659 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2660 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2661 | return -EINVAL; |
| 2662 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2663 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2664 | if (rdev2->raid_disk == slot) |
| 2665 | return -EEXIST; |
| 2666 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2667 | if (slot >= rdev->mddev->raid_disks && |
| 2668 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2669 | return -ENOSPC; |
| 2670 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2671 | rdev->raid_disk = slot; |
| 2672 | if (test_bit(In_sync, &rdev->flags)) |
| 2673 | rdev->saved_raid_disk = slot; |
| 2674 | else |
| 2675 | rdev->saved_raid_disk = -1; |
| 2676 | err = rdev->mddev->pers-> |
| 2677 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2678 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2679 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2680 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2681 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2682 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2683 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2684 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2685 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2686 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2687 | if (slot >= rdev->mddev->raid_disks && |
| 2688 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2689 | return -ENOSPC; |
| 2690 | rdev->raid_disk = slot; |
| 2691 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2692 | clear_bit(Faulty, &rdev->flags); |
| 2693 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2694 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2695 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2696 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2697 | return len; |
| 2698 | } |
| 2699 | |
| 2700 | |
| 2701 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2702 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2703 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2704 | static ssize_t |
| 2705 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2706 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2707 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | static ssize_t |
| 2711 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2712 | { |
| 2713 | char *e; |
| 2714 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2715 | if (e==buf || (*e && *e != '\n')) |
| 2716 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2717 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2718 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2719 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2720 | /* Must set offset before size, so overlap checks |
| 2721 | * can be sane */ |
| 2722 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2723 | rdev->data_offset = offset; |
| 2724 | return len; |
| 2725 | } |
| 2726 | |
| 2727 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2728 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2729 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2730 | static ssize_t |
| 2731 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2732 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2733 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2736 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2737 | { |
| 2738 | /* check if two start/length pairs overlap */ |
| 2739 | if (s1+l1 <= s2) |
| 2740 | return 0; |
| 2741 | if (s2+l2 <= s1) |
| 2742 | return 0; |
| 2743 | return 1; |
| 2744 | } |
| 2745 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2746 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2747 | { |
| 2748 | unsigned long long blocks; |
| 2749 | sector_t new; |
| 2750 | |
| 2751 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2752 | return -EINVAL; |
| 2753 | |
| 2754 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2755 | return -EINVAL; /* sector conversion overflow */ |
| 2756 | |
| 2757 | new = blocks * 2; |
| 2758 | if (new != blocks * 2) |
| 2759 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2760 | |
| 2761 | *sectors = new; |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2765 | static ssize_t |
| 2766 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2767 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2768 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2769 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2770 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2771 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2772 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2773 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2774 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2775 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2776 | sectors = super_types[my_mddev->major_version]. |
| 2777 | rdev_size_change(rdev, sectors); |
| 2778 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2779 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2780 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2781 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2782 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2783 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2784 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2785 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2786 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2787 | rdev->sectors = sectors; |
| 2788 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2789 | /* need to check that all other rdevs with the same ->bdev |
| 2790 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2791 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2792 | * we have to change it back, we will have the lock again. |
| 2793 | */ |
| 2794 | mddev_t *mddev; |
| 2795 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2796 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2797 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2798 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2799 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2800 | mdk_rdev_t *rdev2; |
| 2801 | |
| 2802 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2803 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2804 | if (rdev->bdev == rdev2->bdev && |
| 2805 | rdev != rdev2 && |
| 2806 | overlaps(rdev->data_offset, rdev->sectors, |
| 2807 | rdev2->data_offset, |
| 2808 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2809 | overlap = 1; |
| 2810 | break; |
| 2811 | } |
| 2812 | mddev_unlock(mddev); |
| 2813 | if (overlap) { |
| 2814 | mddev_put(mddev); |
| 2815 | break; |
| 2816 | } |
| 2817 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2818 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2819 | if (overlap) { |
| 2820 | /* Someone else could have slipped in a size |
| 2821 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2822 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2823 | * safe, and trust userspace not to race with |
| 2824 | * itself |
| 2825 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2826 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2827 | return -EBUSY; |
| 2828 | } |
| 2829 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2830 | return len; |
| 2831 | } |
| 2832 | |
| 2833 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2834 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2835 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2836 | |
| 2837 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2838 | { |
| 2839 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2840 | |
| 2841 | if (test_bit(In_sync, &rdev->flags) || |
| 2842 | recovery_start == MaxSector) |
| 2843 | return sprintf(page, "none\n"); |
| 2844 | |
| 2845 | return sprintf(page, "%llu\n", recovery_start); |
| 2846 | } |
| 2847 | |
| 2848 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2849 | { |
| 2850 | unsigned long long recovery_start; |
| 2851 | |
| 2852 | if (cmd_match(buf, "none")) |
| 2853 | recovery_start = MaxSector; |
| 2854 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2855 | return -EINVAL; |
| 2856 | |
| 2857 | if (rdev->mddev->pers && |
| 2858 | rdev->raid_disk >= 0) |
| 2859 | return -EBUSY; |
| 2860 | |
| 2861 | rdev->recovery_offset = recovery_start; |
| 2862 | if (recovery_start == MaxSector) |
| 2863 | set_bit(In_sync, &rdev->flags); |
| 2864 | else |
| 2865 | clear_bit(In_sync, &rdev->flags); |
| 2866 | return len; |
| 2867 | } |
| 2868 | |
| 2869 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2870 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2871 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2872 | |
| 2873 | static ssize_t |
| 2874 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2875 | static ssize_t |
| 2876 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2877 | |
| 2878 | static ssize_t bb_show(mdk_rdev_t *rdev, char *page) |
| 2879 | { |
| 2880 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2881 | } |
| 2882 | static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2883 | { |
| 2884 | return badblocks_store(&rdev->badblocks, page, len, 0); |
| 2885 | } |
| 2886 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2887 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2888 | |
| 2889 | |
| 2890 | static ssize_t ubb_show(mdk_rdev_t *rdev, char *page) |
| 2891 | { |
| 2892 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2893 | } |
| 2894 | static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2895 | { |
| 2896 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2897 | } |
| 2898 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2899 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2900 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2901 | static struct attribute *rdev_default_attrs[] = { |
| 2902 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2903 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2904 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2905 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2906 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2907 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2908 | &rdev_bad_blocks.attr, |
| 2909 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2910 | NULL, |
| 2911 | }; |
| 2912 | static ssize_t |
| 2913 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2914 | { |
| 2915 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2916 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2917 | mddev_t *mddev = rdev->mddev; |
| 2918 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2919 | |
| 2920 | if (!entry->show) |
| 2921 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2922 | |
| 2923 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2924 | if (!rv) { |
| 2925 | if (rdev->mddev == NULL) |
| 2926 | rv = -EBUSY; |
| 2927 | else |
| 2928 | rv = entry->show(rdev, page); |
| 2929 | mddev_unlock(mddev); |
| 2930 | } |
| 2931 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | static ssize_t |
| 2935 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2936 | const char *page, size_t length) |
| 2937 | { |
| 2938 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2939 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2940 | ssize_t rv; |
| 2941 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2942 | |
| 2943 | if (!entry->store) |
| 2944 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2945 | if (!capable(CAP_SYS_ADMIN)) |
| 2946 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2947 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2948 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2949 | if (rdev->mddev == NULL) |
| 2950 | rv = -EBUSY; |
| 2951 | else |
| 2952 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2953 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2954 | } |
| 2955 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | static void rdev_free(struct kobject *ko) |
| 2959 | { |
| 2960 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2961 | kfree(rdev); |
| 2962 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2963 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2964 | .show = rdev_attr_show, |
| 2965 | .store = rdev_attr_store, |
| 2966 | }; |
| 2967 | static struct kobj_type rdev_ktype = { |
| 2968 | .release = rdev_free, |
| 2969 | .sysfs_ops = &rdev_sysfs_ops, |
| 2970 | .default_attrs = rdev_default_attrs, |
| 2971 | }; |
| 2972 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2973 | int md_rdev_init(mdk_rdev_t *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2974 | { |
| 2975 | rdev->desc_nr = -1; |
| 2976 | rdev->saved_raid_disk = -1; |
| 2977 | rdev->raid_disk = -1; |
| 2978 | rdev->flags = 0; |
| 2979 | rdev->data_offset = 0; |
| 2980 | rdev->sb_events = 0; |
| 2981 | rdev->last_read_error.tv_sec = 0; |
| 2982 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2983 | rdev->sb_loaded = 0; |
| 2984 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2985 | atomic_set(&rdev->nr_pending, 0); |
| 2986 | atomic_set(&rdev->read_errors, 0); |
| 2987 | atomic_set(&rdev->corrected_errors, 0); |
| 2988 | |
| 2989 | INIT_LIST_HEAD(&rdev->same_set); |
| 2990 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2991 | |
| 2992 | /* Add space to store bad block list. |
| 2993 | * This reserves the space even on arrays where it cannot |
| 2994 | * be used - I wonder if that matters |
| 2995 | */ |
| 2996 | rdev->badblocks.count = 0; |
| 2997 | rdev->badblocks.shift = 0; |
| 2998 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2999 | seqlock_init(&rdev->badblocks.lock); |
| 3000 | if (rdev->badblocks.page == NULL) |
| 3001 | return -ENOMEM; |
| 3002 | |
| 3003 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3004 | } |
| 3005 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | /* |
| 3007 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3008 | * |
| 3009 | * mark the device faulty if: |
| 3010 | * |
| 3011 | * - the device is nonexistent (zero size) |
| 3012 | * - the device has no valid superblock |
| 3013 | * |
| 3014 | * a faulty rdev _never_ has rdev->sb set. |
| 3015 | */ |
| 3016 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 3017 | { |
| 3018 | char b[BDEVNAME_SIZE]; |
| 3019 | int err; |
| 3020 | mdk_rdev_t *rdev; |
| 3021 | sector_t size; |
| 3022 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3023 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | if (!rdev) { |
| 3025 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3026 | return ERR_PTR(-ENOMEM); |
| 3027 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3029 | err = md_rdev_init(rdev); |
| 3030 | if (err) |
| 3031 | goto abort_free; |
| 3032 | err = alloc_disk_sb(rdev); |
| 3033 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | goto abort_free; |
| 3035 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3036 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | if (err) |
| 3038 | goto abort_free; |
| 3039 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3040 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3041 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3042 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | if (!size) { |
| 3044 | printk(KERN_WARNING |
| 3045 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3046 | bdevname(rdev->bdev,b)); |
| 3047 | err = -EINVAL; |
| 3048 | goto abort_free; |
| 3049 | } |
| 3050 | |
| 3051 | if (super_format >= 0) { |
| 3052 | err = super_types[super_format]. |
| 3053 | load_super(rdev, NULL, super_minor); |
| 3054 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3055 | printk(KERN_WARNING |
| 3056 | "md: %s does not have a valid v%d.%d " |
| 3057 | "superblock, not importing!\n", |
| 3058 | bdevname(rdev->bdev,b), |
| 3059 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | goto abort_free; |
| 3061 | } |
| 3062 | if (err < 0) { |
| 3063 | printk(KERN_WARNING |
| 3064 | "md: could not read %s's sb, not importing!\n", |
| 3065 | bdevname(rdev->bdev,b)); |
| 3066 | goto abort_free; |
| 3067 | } |
| 3068 | } |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3069 | if (super_format == -1) |
| 3070 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3071 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | return rdev; |
| 3074 | |
| 3075 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3076 | if (rdev->bdev) |
| 3077 | unlock_rdev(rdev); |
| 3078 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3079 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | kfree(rdev); |
| 3081 | return ERR_PTR(err); |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * Check a full RAID array for plausibility |
| 3086 | */ |
| 3087 | |
| 3088 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3089 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | { |
| 3091 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3092 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | char b[BDEVNAME_SIZE]; |
| 3094 | |
| 3095 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3096 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | switch (super_types[mddev->major_version]. |
| 3098 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3099 | case 1: |
| 3100 | freshest = rdev; |
| 3101 | break; |
| 3102 | case 0: |
| 3103 | break; |
| 3104 | default: |
| 3105 | printk( KERN_ERR \ |
| 3106 | "md: fatal superblock inconsistency in %s" |
| 3107 | " -- removing from array\n", |
| 3108 | bdevname(rdev->bdev,b)); |
| 3109 | kick_rdev_from_array(rdev); |
| 3110 | } |
| 3111 | |
| 3112 | |
| 3113 | super_types[mddev->major_version]. |
| 3114 | validate_super(mddev, freshest); |
| 3115 | |
| 3116 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3117 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3118 | if (mddev->max_disks && |
| 3119 | (rdev->desc_nr >= mddev->max_disks || |
| 3120 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3121 | printk(KERN_WARNING |
| 3122 | "md: %s: %s: only %d devices permitted\n", |
| 3123 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3124 | mddev->max_disks); |
| 3125 | kick_rdev_from_array(rdev); |
| 3126 | continue; |
| 3127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | if (rdev != freshest) |
| 3129 | if (super_types[mddev->major_version]. |
| 3130 | validate_super(mddev, rdev)) { |
| 3131 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3132 | " from array!\n", |
| 3133 | bdevname(rdev->bdev,b)); |
| 3134 | kick_rdev_from_array(rdev); |
| 3135 | continue; |
| 3136 | } |
| 3137 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3138 | rdev->desc_nr = i++; |
| 3139 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3140 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3141 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3142 | rdev->raid_disk = -1; |
| 3143 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | } |
| 3145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3148 | /* Read a fixed-point number. |
| 3149 | * Numbers in sysfs attributes should be in "standard" units where |
| 3150 | * possible, so time should be in seconds. |
| 3151 | * However we internally use a a much smaller unit such as |
| 3152 | * milliseconds or jiffies. |
| 3153 | * This function takes a decimal number with a possible fractional |
| 3154 | * component, and produces an integer which is the result of |
| 3155 | * multiplying that number by 10^'scale'. |
| 3156 | * all without any floating-point arithmetic. |
| 3157 | */ |
| 3158 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3159 | { |
| 3160 | unsigned long result = 0; |
| 3161 | long decimals = -1; |
| 3162 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3163 | if (*cp == '.') |
| 3164 | decimals = 0; |
| 3165 | else if (decimals < scale) { |
| 3166 | unsigned int value; |
| 3167 | value = *cp - '0'; |
| 3168 | result = result * 10 + value; |
| 3169 | if (decimals >= 0) |
| 3170 | decimals++; |
| 3171 | } |
| 3172 | cp++; |
| 3173 | } |
| 3174 | if (*cp == '\n') |
| 3175 | cp++; |
| 3176 | if (*cp) |
| 3177 | return -EINVAL; |
| 3178 | if (decimals < 0) |
| 3179 | decimals = 0; |
| 3180 | while (decimals < scale) { |
| 3181 | result *= 10; |
| 3182 | decimals ++; |
| 3183 | } |
| 3184 | *res = result; |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
| 3188 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3189 | static void md_safemode_timeout(unsigned long data); |
| 3190 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3191 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3192 | safe_delay_show(mddev_t *mddev, char *page) |
| 3193 | { |
| 3194 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3195 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3196 | } |
| 3197 | static ssize_t |
| 3198 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 3199 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3200 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3201 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3202 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3203 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3204 | if (msec == 0) |
| 3205 | mddev->safemode_delay = 0; |
| 3206 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3207 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3208 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3209 | if (mddev->safemode_delay == 0) |
| 3210 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3211 | if (mddev->safemode_delay < old_delay) |
| 3212 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3213 | } |
| 3214 | return len; |
| 3215 | } |
| 3216 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3217 | __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] | 3218 | |
| 3219 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3220 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3221 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3222 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3223 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3224 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3225 | else if (mddev->clevel[0]) |
| 3226 | return sprintf(page, "%s\n", mddev->clevel); |
| 3227 | else if (mddev->level != LEVEL_NONE) |
| 3228 | return sprintf(page, "%d\n", mddev->level); |
| 3229 | else |
| 3230 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3233 | static ssize_t |
| 3234 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3235 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3236 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3237 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3238 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3239 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3240 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3241 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3242 | |
| 3243 | if (mddev->pers == NULL) { |
| 3244 | if (len == 0) |
| 3245 | return 0; |
| 3246 | if (len >= sizeof(mddev->clevel)) |
| 3247 | return -ENOSPC; |
| 3248 | strncpy(mddev->clevel, buf, len); |
| 3249 | if (mddev->clevel[len-1] == '\n') |
| 3250 | len--; |
| 3251 | mddev->clevel[len] = 0; |
| 3252 | mddev->level = LEVEL_NONE; |
| 3253 | return rv; |
| 3254 | } |
| 3255 | |
| 3256 | /* request to change the personality. Need to ensure: |
| 3257 | * - array is not engaged in resync/recovery/reshape |
| 3258 | * - old personality can be suspended |
| 3259 | * - new personality will access other array. |
| 3260 | */ |
| 3261 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3262 | if (mddev->sync_thread || |
| 3263 | mddev->reshape_position != MaxSector || |
| 3264 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3265 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3266 | |
| 3267 | if (!mddev->pers->quiesce) { |
| 3268 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3269 | mdname(mddev), mddev->pers->name); |
| 3270 | return -EINVAL; |
| 3271 | } |
| 3272 | |
| 3273 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3274 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3275 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3276 | strncpy(clevel, buf, len); |
| 3277 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3278 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3279 | clevel[len] = 0; |
| 3280 | if (strict_strtol(clevel, 10, &level)) |
| 3281 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3282 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3283 | if (request_module("md-%s", clevel) != 0) |
| 3284 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3285 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3286 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3287 | if (!pers || !try_module_get(pers->owner)) { |
| 3288 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3289 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3290 | return -EINVAL; |
| 3291 | } |
| 3292 | spin_unlock(&pers_lock); |
| 3293 | |
| 3294 | if (pers == mddev->pers) { |
| 3295 | /* Nothing to do! */ |
| 3296 | module_put(pers->owner); |
| 3297 | return rv; |
| 3298 | } |
| 3299 | if (!pers->takeover) { |
| 3300 | module_put(pers->owner); |
| 3301 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3302 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3303 | return -EINVAL; |
| 3304 | } |
| 3305 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3306 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3307 | rdev->new_raid_disk = rdev->raid_disk; |
| 3308 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3309 | /* ->takeover must set new_* and/or delta_disks |
| 3310 | * if it succeeds, and may set them when it fails. |
| 3311 | */ |
| 3312 | priv = pers->takeover(mddev); |
| 3313 | if (IS_ERR(priv)) { |
| 3314 | mddev->new_level = mddev->level; |
| 3315 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3316 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3317 | mddev->raid_disks -= mddev->delta_disks; |
| 3318 | mddev->delta_disks = 0; |
| 3319 | module_put(pers->owner); |
| 3320 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3321 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3322 | return PTR_ERR(priv); |
| 3323 | } |
| 3324 | |
| 3325 | /* Looks like we have a winner */ |
| 3326 | mddev_suspend(mddev); |
| 3327 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3328 | |
| 3329 | if (mddev->pers->sync_request == NULL && |
| 3330 | pers->sync_request != NULL) { |
| 3331 | /* need to add the md_redundancy_group */ |
| 3332 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3333 | printk(KERN_WARNING |
| 3334 | "md: cannot register extra attributes for %s\n", |
| 3335 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3336 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3337 | } |
| 3338 | if (mddev->pers->sync_request != NULL && |
| 3339 | pers->sync_request == NULL) { |
| 3340 | /* need to remove the md_redundancy_group */ |
| 3341 | if (mddev->to_remove == NULL) |
| 3342 | mddev->to_remove = &md_redundancy_group; |
| 3343 | } |
| 3344 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3345 | if (mddev->pers->sync_request == NULL && |
| 3346 | mddev->external) { |
| 3347 | /* We are converting from a no-redundancy array |
| 3348 | * to a redundancy array and metadata is managed |
| 3349 | * externally so we need to be sure that writes |
| 3350 | * won't block due to a need to transition |
| 3351 | * clean->dirty |
| 3352 | * until external management is started. |
| 3353 | */ |
| 3354 | mddev->in_sync = 0; |
| 3355 | mddev->safemode_delay = 0; |
| 3356 | mddev->safemode = 0; |
| 3357 | } |
| 3358 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3359 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3360 | if (rdev->raid_disk < 0) |
| 3361 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3362 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3363 | rdev->new_raid_disk = -1; |
| 3364 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3365 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3366 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3367 | } |
| 3368 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3369 | if (rdev->raid_disk < 0) |
| 3370 | continue; |
| 3371 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3372 | continue; |
| 3373 | rdev->raid_disk = rdev->new_raid_disk; |
| 3374 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3375 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3376 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3377 | if (sysfs_link_rdev(mddev, rdev)) |
| 3378 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3379 | " for %s after level change\n", |
| 3380 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3381 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3385 | mddev->pers = pers; |
| 3386 | mddev->private = priv; |
| 3387 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3388 | mddev->level = mddev->new_level; |
| 3389 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3390 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3391 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3392 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3393 | if (mddev->pers->sync_request == NULL) { |
| 3394 | /* this is now an array without redundancy, so |
| 3395 | * it must always be in_sync |
| 3396 | */ |
| 3397 | mddev->in_sync = 1; |
| 3398 | del_timer_sync(&mddev->safemode_timer); |
| 3399 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3400 | pers->run(mddev); |
| 3401 | mddev_resume(mddev); |
| 3402 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3403 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3404 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3405 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3406 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3407 | return rv; |
| 3408 | } |
| 3409 | |
| 3410 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3411 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3412 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3413 | |
| 3414 | static ssize_t |
| 3415 | layout_show(mddev_t *mddev, char *page) |
| 3416 | { |
| 3417 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3418 | if (mddev->reshape_position != MaxSector && |
| 3419 | mddev->layout != mddev->new_layout) |
| 3420 | return sprintf(page, "%d (%d)\n", |
| 3421 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3422 | return sprintf(page, "%d\n", mddev->layout); |
| 3423 | } |
| 3424 | |
| 3425 | static ssize_t |
| 3426 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3427 | { |
| 3428 | char *e; |
| 3429 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3430 | |
| 3431 | if (!*buf || (*e && *e != '\n')) |
| 3432 | return -EINVAL; |
| 3433 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3434 | if (mddev->pers) { |
| 3435 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3436 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3437 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3438 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3439 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3440 | if (err) { |
| 3441 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3442 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3443 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3444 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3445 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3446 | if (mddev->reshape_position == MaxSector) |
| 3447 | mddev->layout = n; |
| 3448 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3449 | return len; |
| 3450 | } |
| 3451 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3452 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3453 | |
| 3454 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3455 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3456 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3457 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3458 | if (mddev->raid_disks == 0) |
| 3459 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3460 | if (mddev->reshape_position != MaxSector && |
| 3461 | mddev->delta_disks != 0) |
| 3462 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3463 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3464 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3465 | } |
| 3466 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3467 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3468 | |
| 3469 | static ssize_t |
| 3470 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3471 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3472 | char *e; |
| 3473 | int rv = 0; |
| 3474 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3475 | |
| 3476 | if (!*buf || (*e && *e != '\n')) |
| 3477 | return -EINVAL; |
| 3478 | |
| 3479 | if (mddev->pers) |
| 3480 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3481 | else if (mddev->reshape_position != MaxSector) { |
| 3482 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3483 | mddev->delta_disks = n - olddisks; |
| 3484 | mddev->raid_disks = n; |
| 3485 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3486 | mddev->raid_disks = n; |
| 3487 | return rv ? rv : len; |
| 3488 | } |
| 3489 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3490 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3491 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3492 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3493 | chunk_size_show(mddev_t *mddev, char *page) |
| 3494 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3495 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3496 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3497 | return sprintf(page, "%d (%d)\n", |
| 3498 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3499 | mddev->chunk_sectors << 9); |
| 3500 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | static ssize_t |
| 3504 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3505 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3506 | char *e; |
| 3507 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3508 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3509 | if (!*buf || (*e && *e != '\n')) |
| 3510 | return -EINVAL; |
| 3511 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3512 | if (mddev->pers) { |
| 3513 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3514 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3515 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3516 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3517 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3518 | if (err) { |
| 3519 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3520 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3521 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3522 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3523 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3524 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3525 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3526 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3527 | return len; |
| 3528 | } |
| 3529 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3530 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3531 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3532 | static ssize_t |
| 3533 | resync_start_show(mddev_t *mddev, char *page) |
| 3534 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3535 | if (mddev->recovery_cp == MaxSector) |
| 3536 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3537 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3538 | } |
| 3539 | |
| 3540 | static ssize_t |
| 3541 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3542 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3543 | char *e; |
| 3544 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3545 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3546 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3547 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3548 | if (cmd_match(buf, "none")) |
| 3549 | n = MaxSector; |
| 3550 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3551 | return -EINVAL; |
| 3552 | |
| 3553 | mddev->recovery_cp = n; |
| 3554 | return len; |
| 3555 | } |
| 3556 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3557 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3558 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3559 | /* |
| 3560 | * The array state can be: |
| 3561 | * |
| 3562 | * clear |
| 3563 | * No devices, no size, no level |
| 3564 | * Equivalent to STOP_ARRAY ioctl |
| 3565 | * inactive |
| 3566 | * May have some settings, but array is not active |
| 3567 | * all IO results in error |
| 3568 | * When written, doesn't tear down array, but just stops it |
| 3569 | * suspended (not supported yet) |
| 3570 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3571 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3572 | * readonly |
| 3573 | * no resync can happen. no superblocks get written. |
| 3574 | * write requests fail |
| 3575 | * read-auto |
| 3576 | * like readonly, but behaves like 'clean' on a write request. |
| 3577 | * |
| 3578 | * clean - no pending writes, but otherwise active. |
| 3579 | * When written to inactive array, starts without resync |
| 3580 | * If a write request arrives then |
| 3581 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3582 | * if not known, block and switch to write-pending |
| 3583 | * If written to an active array that has pending writes, then fails. |
| 3584 | * active |
| 3585 | * fully active: IO and resync can be happening. |
| 3586 | * When written to inactive array, starts with resync |
| 3587 | * |
| 3588 | * write-pending |
| 3589 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3590 | * |
| 3591 | * active-idle |
| 3592 | * like active, but no writes have been seen for a while (100msec). |
| 3593 | * |
| 3594 | */ |
| 3595 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3596 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3597 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3598 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3599 | "write-pending", "active-idle", NULL }; |
| 3600 | |
| 3601 | static int match_word(const char *word, char **list) |
| 3602 | { |
| 3603 | int n; |
| 3604 | for (n=0; list[n]; n++) |
| 3605 | if (cmd_match(word, list[n])) |
| 3606 | break; |
| 3607 | return n; |
| 3608 | } |
| 3609 | |
| 3610 | static ssize_t |
| 3611 | array_state_show(mddev_t *mddev, char *page) |
| 3612 | { |
| 3613 | enum array_state st = inactive; |
| 3614 | |
| 3615 | if (mddev->pers) |
| 3616 | switch(mddev->ro) { |
| 3617 | case 1: |
| 3618 | st = readonly; |
| 3619 | break; |
| 3620 | case 2: |
| 3621 | st = read_auto; |
| 3622 | break; |
| 3623 | case 0: |
| 3624 | if (mddev->in_sync) |
| 3625 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3626 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3627 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3628 | else if (mddev->safemode) |
| 3629 | st = active_idle; |
| 3630 | else |
| 3631 | st = active; |
| 3632 | } |
| 3633 | else { |
| 3634 | if (list_empty(&mddev->disks) && |
| 3635 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3636 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3637 | st = clear; |
| 3638 | else |
| 3639 | st = inactive; |
| 3640 | } |
| 3641 | return sprintf(page, "%s\n", array_states[st]); |
| 3642 | } |
| 3643 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3644 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3645 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3646 | static int do_md_run(mddev_t * mddev); |
| 3647 | static int restart_array(mddev_t *mddev); |
| 3648 | |
| 3649 | static ssize_t |
| 3650 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3651 | { |
| 3652 | int err = -EINVAL; |
| 3653 | enum array_state st = match_word(buf, array_states); |
| 3654 | switch(st) { |
| 3655 | case bad_word: |
| 3656 | break; |
| 3657 | case clear: |
| 3658 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3659 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3660 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3661 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3662 | break; |
| 3663 | case inactive: |
| 3664 | /* stopping an active array */ |
| 3665 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3666 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3667 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3668 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3669 | } else |
| 3670 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3671 | break; |
| 3672 | case suspended: |
| 3673 | break; /* not supported yet */ |
| 3674 | case readonly: |
| 3675 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3676 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3677 | else { |
| 3678 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3679 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3680 | err = do_md_run(mddev); |
| 3681 | } |
| 3682 | break; |
| 3683 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3684 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3685 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3686 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3687 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3688 | err = restart_array(mddev); |
| 3689 | if (err == 0) { |
| 3690 | mddev->ro = 2; |
| 3691 | set_disk_ro(mddev->gendisk, 0); |
| 3692 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3693 | } else { |
| 3694 | mddev->ro = 2; |
| 3695 | err = do_md_run(mddev); |
| 3696 | } |
| 3697 | break; |
| 3698 | case clean: |
| 3699 | if (mddev->pers) { |
| 3700 | restart_array(mddev); |
| 3701 | spin_lock_irq(&mddev->write_lock); |
| 3702 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3703 | if (mddev->in_sync == 0) { |
| 3704 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3705 | if (mddev->safemode == 1) |
| 3706 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3707 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3708 | } |
| 3709 | err = 0; |
| 3710 | } else |
| 3711 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3712 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3713 | } else |
| 3714 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3715 | break; |
| 3716 | case active: |
| 3717 | if (mddev->pers) { |
| 3718 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3719 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3720 | wake_up(&mddev->sb_wait); |
| 3721 | err = 0; |
| 3722 | } else { |
| 3723 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3724 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3725 | err = do_md_run(mddev); |
| 3726 | } |
| 3727 | break; |
| 3728 | case write_pending: |
| 3729 | case active_idle: |
| 3730 | /* these cannot be set */ |
| 3731 | break; |
| 3732 | } |
| 3733 | if (err) |
| 3734 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3735 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3736 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3737 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3738 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3739 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3740 | static struct md_sysfs_entry md_array_state = |
| 3741 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3742 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3743 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3744 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3745 | return sprintf(page, "%d\n", |
| 3746 | atomic_read(&mddev->max_corr_read_errors)); |
| 3747 | } |
| 3748 | |
| 3749 | static ssize_t |
| 3750 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3751 | { |
| 3752 | char *e; |
| 3753 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3754 | |
| 3755 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3756 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3757 | return len; |
| 3758 | } |
| 3759 | return -EINVAL; |
| 3760 | } |
| 3761 | |
| 3762 | static struct md_sysfs_entry max_corr_read_errors = |
| 3763 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3764 | max_corrected_read_errors_store); |
| 3765 | |
| 3766 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3767 | null_show(mddev_t *mddev, char *page) |
| 3768 | { |
| 3769 | return -EINVAL; |
| 3770 | } |
| 3771 | |
| 3772 | static ssize_t |
| 3773 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3774 | { |
| 3775 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3776 | /* The new device is added to the array. |
| 3777 | * If the array has a persistent superblock, we read the |
| 3778 | * superblock to initialise info and check validity. |
| 3779 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3780 | * which mainly checks size. |
| 3781 | */ |
| 3782 | char *e; |
| 3783 | int major = simple_strtoul(buf, &e, 10); |
| 3784 | int minor; |
| 3785 | dev_t dev; |
| 3786 | mdk_rdev_t *rdev; |
| 3787 | int err; |
| 3788 | |
| 3789 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3790 | return -EINVAL; |
| 3791 | minor = simple_strtoul(e+1, &e, 10); |
| 3792 | if (*e && *e != '\n') |
| 3793 | return -EINVAL; |
| 3794 | dev = MKDEV(major, minor); |
| 3795 | if (major != MAJOR(dev) || |
| 3796 | minor != MINOR(dev)) |
| 3797 | return -EOVERFLOW; |
| 3798 | |
| 3799 | |
| 3800 | if (mddev->persistent) { |
| 3801 | rdev = md_import_device(dev, mddev->major_version, |
| 3802 | mddev->minor_version); |
| 3803 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3804 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3805 | mdk_rdev_t, same_set); |
| 3806 | err = super_types[mddev->major_version] |
| 3807 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3808 | if (err < 0) |
| 3809 | goto out; |
| 3810 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3811 | } else if (mddev->external) |
| 3812 | rdev = md_import_device(dev, -2, -1); |
| 3813 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3814 | rdev = md_import_device(dev, -1, -1); |
| 3815 | |
| 3816 | if (IS_ERR(rdev)) |
| 3817 | return PTR_ERR(rdev); |
| 3818 | err = bind_rdev_to_array(rdev, mddev); |
| 3819 | out: |
| 3820 | if (err) |
| 3821 | export_rdev(rdev); |
| 3822 | return err ? err : len; |
| 3823 | } |
| 3824 | |
| 3825 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3826 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3827 | |
| 3828 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3829 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3830 | { |
| 3831 | char *end; |
| 3832 | unsigned long chunk, end_chunk; |
| 3833 | |
| 3834 | if (!mddev->bitmap) |
| 3835 | goto out; |
| 3836 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3837 | while (*buf) { |
| 3838 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3839 | if (buf == end) break; |
| 3840 | if (*end == '-') { /* range */ |
| 3841 | buf = end + 1; |
| 3842 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3843 | if (buf == end) break; |
| 3844 | } |
| 3845 | if (*end && !isspace(*end)) break; |
| 3846 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3847 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3848 | } |
| 3849 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3850 | out: |
| 3851 | return len; |
| 3852 | } |
| 3853 | |
| 3854 | static struct md_sysfs_entry md_bitmap = |
| 3855 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3856 | |
| 3857 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3858 | size_show(mddev_t *mddev, char *page) |
| 3859 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3860 | return sprintf(page, "%llu\n", |
| 3861 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3862 | } |
| 3863 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3864 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3865 | |
| 3866 | static ssize_t |
| 3867 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3868 | { |
| 3869 | /* If array is inactive, we can reduce the component size, but |
| 3870 | * not increase it (except from 0). |
| 3871 | * If array is active, we can try an on-line resize |
| 3872 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3873 | sector_t sectors; |
| 3874 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3875 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3876 | if (err < 0) |
| 3877 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3878 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3879 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3880 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3881 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3882 | if (mddev->dev_sectors == 0 || |
| 3883 | mddev->dev_sectors > sectors) |
| 3884 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3885 | else |
| 3886 | err = -ENOSPC; |
| 3887 | } |
| 3888 | return err ? err : len; |
| 3889 | } |
| 3890 | |
| 3891 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3892 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3893 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3894 | |
| 3895 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3896 | * This is one of |
| 3897 | * 'none' for arrays with no metadata (good luck...) |
| 3898 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3899 | * or N.M for internally known formats |
| 3900 | */ |
| 3901 | static ssize_t |
| 3902 | metadata_show(mddev_t *mddev, char *page) |
| 3903 | { |
| 3904 | if (mddev->persistent) |
| 3905 | return sprintf(page, "%d.%d\n", |
| 3906 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3907 | else if (mddev->external) |
| 3908 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3909 | else |
| 3910 | return sprintf(page, "none\n"); |
| 3911 | } |
| 3912 | |
| 3913 | static ssize_t |
| 3914 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3915 | { |
| 3916 | int major, minor; |
| 3917 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3918 | /* Changing the details of 'external' metadata is |
| 3919 | * always permitted. Otherwise there must be |
| 3920 | * no devices attached to the array. |
| 3921 | */ |
| 3922 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3923 | ; |
| 3924 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3925 | return -EBUSY; |
| 3926 | |
| 3927 | if (cmd_match(buf, "none")) { |
| 3928 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3929 | mddev->external = 0; |
| 3930 | mddev->major_version = 0; |
| 3931 | mddev->minor_version = 90; |
| 3932 | return len; |
| 3933 | } |
| 3934 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3935 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3936 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3937 | namelen = sizeof(mddev->metadata_type)-1; |
| 3938 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3939 | mddev->metadata_type[namelen] = 0; |
| 3940 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3941 | mddev->metadata_type[--namelen] = 0; |
| 3942 | mddev->persistent = 0; |
| 3943 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3944 | mddev->major_version = 0; |
| 3945 | mddev->minor_version = 90; |
| 3946 | return len; |
| 3947 | } |
| 3948 | major = simple_strtoul(buf, &e, 10); |
| 3949 | if (e==buf || *e != '.') |
| 3950 | return -EINVAL; |
| 3951 | buf = e+1; |
| 3952 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3953 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3954 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3955 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3956 | return -ENOENT; |
| 3957 | mddev->major_version = major; |
| 3958 | mddev->minor_version = minor; |
| 3959 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3960 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3961 | return len; |
| 3962 | } |
| 3963 | |
| 3964 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3965 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3966 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3967 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3968 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3969 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3970 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3971 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3972 | type = "frozen"; |
| 3973 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3974 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3975 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3976 | type = "reshape"; |
| 3977 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3978 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3979 | type = "resync"; |
| 3980 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3981 | type = "check"; |
| 3982 | else |
| 3983 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3984 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3985 | type = "recover"; |
| 3986 | } |
| 3987 | return sprintf(page, "%s\n", type); |
| 3988 | } |
| 3989 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3990 | static void reap_sync_thread(mddev_t *mddev); |
| 3991 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3992 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3993 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3994 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3995 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3996 | return -EINVAL; |
| 3997 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3998 | if (cmd_match(page, "frozen")) |
| 3999 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4000 | else |
| 4001 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4002 | |
| 4003 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4004 | if (mddev->sync_thread) { |
| 4005 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4006 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4007 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4008 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4009 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4010 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4011 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4012 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4013 | else if (cmd_match(page, "recover")) { |
| 4014 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4015 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4016 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4017 | int err; |
| 4018 | if (mddev->pers->start_reshape == NULL) |
| 4019 | return -EINVAL; |
| 4020 | err = mddev->pers->start_reshape(mddev); |
| 4021 | if (err) |
| 4022 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4023 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4024 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4025 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4026 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4027 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4028 | return -EINVAL; |
| 4029 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4030 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4031 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4032 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4033 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4034 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4035 | return len; |
| 4036 | } |
| 4037 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4038 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4039 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4040 | { |
| 4041 | return sprintf(page, "%llu\n", |
| 4042 | (unsigned long long) mddev->resync_mismatches); |
| 4043 | } |
| 4044 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4045 | static struct md_sysfs_entry md_scan_mode = |
| 4046 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4047 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4048 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4049 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4050 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4051 | static ssize_t |
| 4052 | sync_min_show(mddev_t *mddev, char *page) |
| 4053 | { |
| 4054 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4055 | mddev->sync_speed_min ? "local": "system"); |
| 4056 | } |
| 4057 | |
| 4058 | static ssize_t |
| 4059 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 4060 | { |
| 4061 | int min; |
| 4062 | char *e; |
| 4063 | if (strncmp(buf, "system", 6)==0) { |
| 4064 | mddev->sync_speed_min = 0; |
| 4065 | return len; |
| 4066 | } |
| 4067 | min = simple_strtoul(buf, &e, 10); |
| 4068 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4069 | return -EINVAL; |
| 4070 | mddev->sync_speed_min = min; |
| 4071 | return len; |
| 4072 | } |
| 4073 | |
| 4074 | static struct md_sysfs_entry md_sync_min = |
| 4075 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4076 | |
| 4077 | static ssize_t |
| 4078 | sync_max_show(mddev_t *mddev, char *page) |
| 4079 | { |
| 4080 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4081 | mddev->sync_speed_max ? "local": "system"); |
| 4082 | } |
| 4083 | |
| 4084 | static ssize_t |
| 4085 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 4086 | { |
| 4087 | int max; |
| 4088 | char *e; |
| 4089 | if (strncmp(buf, "system", 6)==0) { |
| 4090 | mddev->sync_speed_max = 0; |
| 4091 | return len; |
| 4092 | } |
| 4093 | max = simple_strtoul(buf, &e, 10); |
| 4094 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4095 | return -EINVAL; |
| 4096 | mddev->sync_speed_max = max; |
| 4097 | return len; |
| 4098 | } |
| 4099 | |
| 4100 | static struct md_sysfs_entry md_sync_max = |
| 4101 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4102 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4103 | static ssize_t |
| 4104 | degraded_show(mddev_t *mddev, char *page) |
| 4105 | { |
| 4106 | return sprintf(page, "%d\n", mddev->degraded); |
| 4107 | } |
| 4108 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4109 | |
| 4110 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4111 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 4112 | { |
| 4113 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4114 | } |
| 4115 | |
| 4116 | static ssize_t |
| 4117 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 4118 | { |
| 4119 | long n; |
| 4120 | |
| 4121 | if (strict_strtol(buf, 10, &n)) |
| 4122 | return -EINVAL; |
| 4123 | |
| 4124 | if (n != 0 && n != 1) |
| 4125 | return -EINVAL; |
| 4126 | |
| 4127 | mddev->parallel_resync = n; |
| 4128 | |
| 4129 | if (mddev->sync_thread) |
| 4130 | wake_up(&resync_wait); |
| 4131 | |
| 4132 | return len; |
| 4133 | } |
| 4134 | |
| 4135 | /* force parallel resync, even with shared block devices */ |
| 4136 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4137 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4138 | sync_force_parallel_show, sync_force_parallel_store); |
| 4139 | |
| 4140 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4141 | sync_speed_show(mddev_t *mddev, char *page) |
| 4142 | { |
| 4143 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4144 | if (mddev->curr_resync == 0) |
| 4145 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4146 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4147 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4148 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4149 | db = resync - mddev->resync_mark_cnt; |
| 4150 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4153 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4154 | |
| 4155 | static ssize_t |
| 4156 | sync_completed_show(mddev_t *mddev, char *page) |
| 4157 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4158 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4159 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4160 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4161 | return sprintf(page, "none\n"); |
| 4162 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4163 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4164 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4165 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4166 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4167 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4168 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4169 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4172 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4173 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4174 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4175 | min_sync_show(mddev_t *mddev, char *page) |
| 4176 | { |
| 4177 | return sprintf(page, "%llu\n", |
| 4178 | (unsigned long long)mddev->resync_min); |
| 4179 | } |
| 4180 | static ssize_t |
| 4181 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4182 | { |
| 4183 | unsigned long long min; |
| 4184 | if (strict_strtoull(buf, 10, &min)) |
| 4185 | return -EINVAL; |
| 4186 | if (min > mddev->resync_max) |
| 4187 | return -EINVAL; |
| 4188 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4189 | return -EBUSY; |
| 4190 | |
| 4191 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4192 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4193 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4194 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4195 | return -EINVAL; |
| 4196 | } |
| 4197 | mddev->resync_min = min; |
| 4198 | |
| 4199 | return len; |
| 4200 | } |
| 4201 | |
| 4202 | static struct md_sysfs_entry md_min_sync = |
| 4203 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4204 | |
| 4205 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4206 | max_sync_show(mddev_t *mddev, char *page) |
| 4207 | { |
| 4208 | if (mddev->resync_max == MaxSector) |
| 4209 | return sprintf(page, "max\n"); |
| 4210 | else |
| 4211 | return sprintf(page, "%llu\n", |
| 4212 | (unsigned long long)mddev->resync_max); |
| 4213 | } |
| 4214 | static ssize_t |
| 4215 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4216 | { |
| 4217 | if (strncmp(buf, "max", 3) == 0) |
| 4218 | mddev->resync_max = MaxSector; |
| 4219 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4220 | unsigned long long max; |
| 4221 | if (strict_strtoull(buf, 10, &max)) |
| 4222 | return -EINVAL; |
| 4223 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4224 | return -EINVAL; |
| 4225 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4226 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4227 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4228 | return -EBUSY; |
| 4229 | |
| 4230 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4231 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4232 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4233 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4234 | return -EINVAL; |
| 4235 | } |
| 4236 | mddev->resync_max = max; |
| 4237 | } |
| 4238 | wake_up(&mddev->recovery_wait); |
| 4239 | return len; |
| 4240 | } |
| 4241 | |
| 4242 | static struct md_sysfs_entry md_max_sync = |
| 4243 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4244 | |
| 4245 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4246 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4247 | { |
| 4248 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4249 | } |
| 4250 | |
| 4251 | static ssize_t |
| 4252 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4253 | { |
| 4254 | char *e; |
| 4255 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4256 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4257 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4258 | if (mddev->pers == NULL || |
| 4259 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4260 | return -EINVAL; |
| 4261 | if (buf == e || (*e && *e != '\n')) |
| 4262 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4263 | |
| 4264 | mddev->suspend_lo = new; |
| 4265 | if (new >= old) |
| 4266 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4267 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4268 | else { |
| 4269 | /* Expanding suspended region - need to wait */ |
| 4270 | mddev->pers->quiesce(mddev, 1); |
| 4271 | mddev->pers->quiesce(mddev, 0); |
| 4272 | } |
| 4273 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4274 | } |
| 4275 | static struct md_sysfs_entry md_suspend_lo = |
| 4276 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4277 | |
| 4278 | |
| 4279 | static ssize_t |
| 4280 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4281 | { |
| 4282 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4283 | } |
| 4284 | |
| 4285 | static ssize_t |
| 4286 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4287 | { |
| 4288 | char *e; |
| 4289 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4290 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4291 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4292 | if (mddev->pers == NULL || |
| 4293 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4294 | return -EINVAL; |
| 4295 | if (buf == e || (*e && *e != '\n')) |
| 4296 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4297 | |
| 4298 | mddev->suspend_hi = new; |
| 4299 | if (new <= old) |
| 4300 | /* Shrinking suspended region */ |
| 4301 | mddev->pers->quiesce(mddev, 2); |
| 4302 | else { |
| 4303 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4304 | mddev->pers->quiesce(mddev, 1); |
| 4305 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4306 | } |
| 4307 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4308 | } |
| 4309 | static struct md_sysfs_entry md_suspend_hi = |
| 4310 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4311 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4312 | static ssize_t |
| 4313 | reshape_position_show(mddev_t *mddev, char *page) |
| 4314 | { |
| 4315 | if (mddev->reshape_position != MaxSector) |
| 4316 | return sprintf(page, "%llu\n", |
| 4317 | (unsigned long long)mddev->reshape_position); |
| 4318 | strcpy(page, "none\n"); |
| 4319 | return 5; |
| 4320 | } |
| 4321 | |
| 4322 | static ssize_t |
| 4323 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4324 | { |
| 4325 | char *e; |
| 4326 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4327 | if (mddev->pers) |
| 4328 | return -EBUSY; |
| 4329 | if (buf == e || (*e && *e != '\n')) |
| 4330 | return -EINVAL; |
| 4331 | mddev->reshape_position = new; |
| 4332 | mddev->delta_disks = 0; |
| 4333 | mddev->new_level = mddev->level; |
| 4334 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4335 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4336 | return len; |
| 4337 | } |
| 4338 | |
| 4339 | static struct md_sysfs_entry md_reshape_position = |
| 4340 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4341 | reshape_position_store); |
| 4342 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4343 | static ssize_t |
| 4344 | array_size_show(mddev_t *mddev, char *page) |
| 4345 | { |
| 4346 | if (mddev->external_size) |
| 4347 | return sprintf(page, "%llu\n", |
| 4348 | (unsigned long long)mddev->array_sectors/2); |
| 4349 | else |
| 4350 | return sprintf(page, "default\n"); |
| 4351 | } |
| 4352 | |
| 4353 | static ssize_t |
| 4354 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4355 | { |
| 4356 | sector_t sectors; |
| 4357 | |
| 4358 | if (strncmp(buf, "default", 7) == 0) { |
| 4359 | if (mddev->pers) |
| 4360 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4361 | else |
| 4362 | sectors = mddev->array_sectors; |
| 4363 | |
| 4364 | mddev->external_size = 0; |
| 4365 | } else { |
| 4366 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4367 | return -EINVAL; |
| 4368 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4369 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4370 | |
| 4371 | mddev->external_size = 1; |
| 4372 | } |
| 4373 | |
| 4374 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4375 | if (mddev->pers) { |
| 4376 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4377 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4378 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4379 | return len; |
| 4380 | } |
| 4381 | |
| 4382 | static struct md_sysfs_entry md_array_size = |
| 4383 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4384 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4385 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4386 | static struct attribute *md_default_attrs[] = { |
| 4387 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4388 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4389 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4390 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4391 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4392 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4393 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4394 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4395 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4396 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4397 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4398 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4399 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4400 | NULL, |
| 4401 | }; |
| 4402 | |
| 4403 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4404 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4405 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4406 | &md_sync_min.attr, |
| 4407 | &md_sync_max.attr, |
| 4408 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4409 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4410 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4411 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4412 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4413 | &md_suspend_lo.attr, |
| 4414 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4415 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4416 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4417 | NULL, |
| 4418 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4419 | static struct attribute_group md_redundancy_group = { |
| 4420 | .name = NULL, |
| 4421 | .attrs = md_redundancy_attrs, |
| 4422 | }; |
| 4423 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4424 | |
| 4425 | static ssize_t |
| 4426 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4427 | { |
| 4428 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4429 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4430 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4431 | |
| 4432 | if (!entry->show) |
| 4433 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4434 | rv = mddev_lock(mddev); |
| 4435 | if (!rv) { |
| 4436 | rv = entry->show(mddev, page); |
| 4437 | mddev_unlock(mddev); |
| 4438 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4439 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | static ssize_t |
| 4443 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4444 | const char *page, size_t length) |
| 4445 | { |
| 4446 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4447 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4448 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4449 | |
| 4450 | if (!entry->store) |
| 4451 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4452 | if (!capable(CAP_SYS_ADMIN)) |
| 4453 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4454 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4455 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4456 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4457 | if (!rv) { |
| 4458 | rv = entry->store(mddev, page, length); |
| 4459 | mddev_unlock(mddev); |
| 4460 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4461 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | static void md_free(struct kobject *ko) |
| 4465 | { |
| 4466 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4467 | |
| 4468 | if (mddev->sysfs_state) |
| 4469 | sysfs_put(mddev->sysfs_state); |
| 4470 | |
| 4471 | if (mddev->gendisk) { |
| 4472 | del_gendisk(mddev->gendisk); |
| 4473 | put_disk(mddev->gendisk); |
| 4474 | } |
| 4475 | if (mddev->queue) |
| 4476 | blk_cleanup_queue(mddev->queue); |
| 4477 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4478 | kfree(mddev); |
| 4479 | } |
| 4480 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4481 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4482 | .show = md_attr_show, |
| 4483 | .store = md_attr_store, |
| 4484 | }; |
| 4485 | static struct kobj_type md_ktype = { |
| 4486 | .release = md_free, |
| 4487 | .sysfs_ops = &md_sysfs_ops, |
| 4488 | .default_attrs = md_default_attrs, |
| 4489 | }; |
| 4490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | int mdp_major = 0; |
| 4492 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4493 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4494 | { |
| 4495 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4496 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4497 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4498 | kobject_del(&mddev->kobj); |
| 4499 | kobject_put(&mddev->kobj); |
| 4500 | } |
| 4501 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4502 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4504 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | mddev_t *mddev = mddev_find(dev); |
| 4506 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4507 | int partitioned; |
| 4508 | int shift; |
| 4509 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4510 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | |
| 4512 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4513 | return -ENODEV; |
| 4514 | |
| 4515 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4516 | shift = partitioned ? MdpMinorShift : 0; |
| 4517 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4519 | /* wait for any previous instance of this device to be |
| 4520 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4521 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4522 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4523 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4524 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4525 | error = -EEXIST; |
| 4526 | if (mddev->gendisk) |
| 4527 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4528 | |
| 4529 | if (name) { |
| 4530 | /* Need to ensure that 'name' is not a duplicate. |
| 4531 | */ |
| 4532 | mddev_t *mddev2; |
| 4533 | spin_lock(&all_mddevs_lock); |
| 4534 | |
| 4535 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4536 | if (mddev2->gendisk && |
| 4537 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4538 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4539 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4540 | } |
| 4541 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4543 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4544 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4545 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4546 | if (!mddev->queue) |
| 4547 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4548 | mddev->queue->queuedata = mddev; |
| 4549 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4550 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | disk = alloc_disk(1 << shift); |
| 4553 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4554 | blk_cleanup_queue(mddev->queue); |
| 4555 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4556 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4558 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4560 | if (name) |
| 4561 | strcpy(disk->disk_name, name); |
| 4562 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4564 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | disk->fops = &md_fops; |
| 4567 | disk->private_data = mddev; |
| 4568 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4569 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4570 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4571 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4572 | * remove it now. |
| 4573 | */ |
| 4574 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4576 | /* As soon as we call add_disk(), another thread could get |
| 4577 | * through to md_open, so make sure it doesn't get too far |
| 4578 | */ |
| 4579 | mutex_lock(&mddev->open_mutex); |
| 4580 | add_disk(disk); |
| 4581 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4582 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4583 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4584 | if (error) { |
| 4585 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4586 | * __must_check, we must do something with the result |
| 4587 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4588 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4589 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4590 | error = 0; |
| 4591 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4592 | if (mddev->kobj.sd && |
| 4593 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4594 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4595 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4596 | abort: |
| 4597 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4598 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4599 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4600 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4601 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4602 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4603 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4607 | { |
| 4608 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | return NULL; |
| 4610 | } |
| 4611 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4612 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4613 | { |
| 4614 | /* val must be "md_*" where * is not all digits. |
| 4615 | * We allocate an array with a large free minor number, and |
| 4616 | * set the name to val. val must not already be an active name. |
| 4617 | */ |
| 4618 | int len = strlen(val); |
| 4619 | char buf[DISK_NAME_LEN]; |
| 4620 | |
| 4621 | while (len && val[len-1] == '\n') |
| 4622 | len--; |
| 4623 | if (len >= DISK_NAME_LEN) |
| 4624 | return -E2BIG; |
| 4625 | strlcpy(buf, val, len+1); |
| 4626 | if (strncmp(buf, "md_", 3) != 0) |
| 4627 | return -EINVAL; |
| 4628 | return md_alloc(0, buf); |
| 4629 | } |
| 4630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | static void md_safemode_timeout(unsigned long data) |
| 4632 | { |
| 4633 | mddev_t *mddev = (mddev_t *) data; |
| 4634 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4635 | if (!atomic_read(&mddev->writes_pending)) { |
| 4636 | mddev->safemode = 1; |
| 4637 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4638 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | md_wakeup_thread(mddev->thread); |
| 4641 | } |
| 4642 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4643 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4645 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4647 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4649 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4651 | if (list_empty(&mddev->disks)) |
| 4652 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | |
| 4655 | if (mddev->pers) |
| 4656 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4657 | /* Cannot run until previous stop completes properly */ |
| 4658 | if (mddev->sysfs_active) |
| 4659 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | /* |
| 4662 | * Analyze all RAID superblock(s) |
| 4663 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4664 | if (!mddev->raid_disks) { |
| 4665 | if (!mddev->persistent) |
| 4666 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4667 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4670 | if (mddev->level != LEVEL_NONE) |
| 4671 | request_module("md-level-%d", mddev->level); |
| 4672 | else if (mddev->clevel[0]) |
| 4673 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | |
| 4675 | /* |
| 4676 | * Drop all container device buffers, from now on |
| 4677 | * the only valid external interface is through the md |
| 4678 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4680 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4681 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | continue; |
| 4683 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4684 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4685 | |
| 4686 | /* perform some consistency tests on the device. |
| 4687 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4688 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4689 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4690 | if (rdev->meta_bdev) { |
| 4691 | /* Nothing to check */; |
| 4692 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4693 | if (mddev->dev_sectors && |
| 4694 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4695 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4696 | printk("md: %s: data overlaps metadata\n", |
| 4697 | mdname(mddev)); |
| 4698 | return -EINVAL; |
| 4699 | } |
| 4700 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4701 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4702 | > rdev->data_offset) { |
| 4703 | printk("md: %s: metadata overlaps data\n", |
| 4704 | mdname(mddev)); |
| 4705 | return -EINVAL; |
| 4706 | } |
| 4707 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4708 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4711 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4712 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
| 4713 | sizeof(mddev_t *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4716 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4717 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4719 | if (mddev->level != LEVEL_NONE) |
| 4720 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4721 | mddev->level); |
| 4722 | else |
| 4723 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4724 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | return -EINVAL; |
| 4726 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4727 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4729 | if (mddev->level != pers->level) { |
| 4730 | mddev->level = pers->level; |
| 4731 | mddev->new_level = pers->level; |
| 4732 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4733 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4735 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4736 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4737 | /* This personality cannot handle reshaping... */ |
| 4738 | mddev->pers = NULL; |
| 4739 | module_put(pers->owner); |
| 4740 | return -EINVAL; |
| 4741 | } |
| 4742 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4743 | if (pers->sync_request) { |
| 4744 | /* Warn if this is a potentially silly |
| 4745 | * configuration. |
| 4746 | */ |
| 4747 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4748 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4749 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4750 | |
| 4751 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4752 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4753 | if (rdev < rdev2 && |
| 4754 | rdev->bdev->bd_contains == |
| 4755 | rdev2->bdev->bd_contains) { |
| 4756 | printk(KERN_WARNING |
| 4757 | "%s: WARNING: %s appears to be" |
| 4758 | " on the same physical disk as" |
| 4759 | " %s.\n", |
| 4760 | mdname(mddev), |
| 4761 | bdevname(rdev->bdev,b), |
| 4762 | bdevname(rdev2->bdev,b2)); |
| 4763 | warned = 1; |
| 4764 | } |
| 4765 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4766 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4767 | if (warned) |
| 4768 | printk(KERN_WARNING |
| 4769 | "True protection against single-disk" |
| 4770 | " failure might be compromised.\n"); |
| 4771 | } |
| 4772 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4773 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4774 | /* may be over-ridden by personality */ |
| 4775 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4776 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4777 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4779 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4780 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4781 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4782 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4783 | if (err) |
| 4784 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4785 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4786 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4787 | " but 'external_size' not in effect?\n", __func__); |
| 4788 | printk(KERN_ERR |
| 4789 | "md: invalid array_size %llu > default size %llu\n", |
| 4790 | (unsigned long long)mddev->array_sectors / 2, |
| 4791 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4792 | err = -EINVAL; |
| 4793 | mddev->pers->stop(mddev); |
| 4794 | } |
| 4795 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4796 | err = bitmap_create(mddev); |
| 4797 | if (err) { |
| 4798 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4799 | mdname(mddev), err); |
| 4800 | mddev->pers->stop(mddev); |
| 4801 | } |
| 4802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | module_put(mddev->pers->owner); |
| 4805 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4806 | bitmap_destroy(mddev); |
| 4807 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4809 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4810 | if (mddev->kobj.sd && |
| 4811 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4812 | printk(KERN_WARNING |
| 4813 | "md: cannot register extra attributes for %s\n", |
| 4814 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4815 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4816 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4817 | mddev->ro = 0; |
| 4818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4820 | atomic_set(&mddev->max_corr_read_errors, |
| 4821 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | mddev->safemode = 0; |
| 4823 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4824 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4825 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4827 | smp_wmb(); |
| 4828 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4829 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4830 | if (rdev->raid_disk >= 0) |
| 4831 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4832 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
| 4834 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4835 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4836 | if (mddev->flags) |
| 4837 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4839 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4840 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4841 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4842 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | return 0; |
| 4844 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4845 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4847 | static int do_md_run(mddev_t *mddev) |
| 4848 | { |
| 4849 | int err; |
| 4850 | |
| 4851 | err = md_run(mddev); |
| 4852 | if (err) |
| 4853 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4854 | err = bitmap_load(mddev); |
| 4855 | if (err) { |
| 4856 | bitmap_destroy(mddev); |
| 4857 | goto out; |
| 4858 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4859 | |
| 4860 | md_wakeup_thread(mddev->thread); |
| 4861 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4862 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4863 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4864 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4865 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4866 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4867 | out: |
| 4868 | return err; |
| 4869 | } |
| 4870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | static int restart_array(mddev_t *mddev) |
| 4872 | { |
| 4873 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4875 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4877 | return -ENXIO; |
| 4878 | if (!mddev->pers) |
| 4879 | return -EINVAL; |
| 4880 | if (!mddev->ro) |
| 4881 | return -EBUSY; |
| 4882 | mddev->safemode = 0; |
| 4883 | mddev->ro = 0; |
| 4884 | set_disk_ro(disk, 0); |
| 4885 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4886 | mdname(mddev)); |
| 4887 | /* Kick recovery or resync if necessary */ |
| 4888 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4889 | md_wakeup_thread(mddev->thread); |
| 4890 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4891 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4895 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4896 | * to the file ourselves */ |
| 4897 | static int deny_bitmap_write_access(struct file * file) |
| 4898 | { |
| 4899 | struct inode *inode = file->f_mapping->host; |
| 4900 | |
| 4901 | spin_lock(&inode->i_lock); |
| 4902 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4903 | spin_unlock(&inode->i_lock); |
| 4904 | return -ETXTBSY; |
| 4905 | } |
| 4906 | atomic_set(&inode->i_writecount, -1); |
| 4907 | spin_unlock(&inode->i_lock); |
| 4908 | |
| 4909 | return 0; |
| 4910 | } |
| 4911 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4912 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4913 | { |
| 4914 | struct inode *inode = file->f_mapping->host; |
| 4915 | |
| 4916 | spin_lock(&inode->i_lock); |
| 4917 | atomic_set(&inode->i_writecount, 1); |
| 4918 | spin_unlock(&inode->i_lock); |
| 4919 | } |
| 4920 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4921 | static void md_clean(mddev_t *mddev) |
| 4922 | { |
| 4923 | mddev->array_sectors = 0; |
| 4924 | mddev->external_size = 0; |
| 4925 | mddev->dev_sectors = 0; |
| 4926 | mddev->raid_disks = 0; |
| 4927 | mddev->recovery_cp = 0; |
| 4928 | mddev->resync_min = 0; |
| 4929 | mddev->resync_max = MaxSector; |
| 4930 | mddev->reshape_position = MaxSector; |
| 4931 | mddev->external = 0; |
| 4932 | mddev->persistent = 0; |
| 4933 | mddev->level = LEVEL_NONE; |
| 4934 | mddev->clevel[0] = 0; |
| 4935 | mddev->flags = 0; |
| 4936 | mddev->ro = 0; |
| 4937 | mddev->metadata_type[0] = 0; |
| 4938 | mddev->chunk_sectors = 0; |
| 4939 | mddev->ctime = mddev->utime = 0; |
| 4940 | mddev->layout = 0; |
| 4941 | mddev->max_disks = 0; |
| 4942 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4943 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4944 | mddev->delta_disks = 0; |
| 4945 | mddev->new_level = LEVEL_NONE; |
| 4946 | mddev->new_layout = 0; |
| 4947 | mddev->new_chunk_sectors = 0; |
| 4948 | mddev->curr_resync = 0; |
| 4949 | mddev->resync_mismatches = 0; |
| 4950 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4951 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4952 | mddev->recovery = 0; |
| 4953 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4954 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4955 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4956 | mddev->safemode = 0; |
| 4957 | mddev->bitmap_info.offset = 0; |
| 4958 | mddev->bitmap_info.default_offset = 0; |
| 4959 | mddev->bitmap_info.chunksize = 0; |
| 4960 | mddev->bitmap_info.daemon_sleep = 0; |
| 4961 | mddev->bitmap_info.max_write_behind = 0; |
| 4962 | } |
| 4963 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4964 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4965 | { |
| 4966 | if (mddev->sync_thread) { |
| 4967 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4968 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4969 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | del_timer_sync(&mddev->safemode_timer); |
| 4973 | |
| 4974 | bitmap_flush(mddev); |
| 4975 | md_super_wait(mddev); |
| 4976 | |
| 4977 | if (!mddev->in_sync || mddev->flags) { |
| 4978 | /* mark array as shutdown cleanly */ |
| 4979 | mddev->in_sync = 1; |
| 4980 | md_update_sb(mddev, 1); |
| 4981 | } |
| 4982 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4983 | |
| 4984 | void md_stop_writes(mddev_t *mddev) |
| 4985 | { |
| 4986 | mddev_lock(mddev); |
| 4987 | __md_stop_writes(mddev); |
| 4988 | mddev_unlock(mddev); |
| 4989 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4990 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4991 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4992 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4993 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4994 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4995 | mddev->pers->stop(mddev); |
| 4996 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4997 | mddev->to_remove = &md_redundancy_group; |
| 4998 | module_put(mddev->pers->owner); |
| 4999 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5000 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5001 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5002 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5003 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5004 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 5005 | { |
| 5006 | int err = 0; |
| 5007 | mutex_lock(&mddev->open_mutex); |
| 5008 | if (atomic_read(&mddev->openers) > is_open) { |
| 5009 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5010 | err = -EBUSY; |
| 5011 | goto out; |
| 5012 | } |
| 5013 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5014 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5015 | |
| 5016 | err = -ENXIO; |
| 5017 | if (mddev->ro==1) |
| 5018 | goto out; |
| 5019 | mddev->ro = 1; |
| 5020 | set_disk_ro(mddev->gendisk, 1); |
| 5021 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5022 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5023 | err = 0; |
| 5024 | } |
| 5025 | out: |
| 5026 | mutex_unlock(&mddev->open_mutex); |
| 5027 | return err; |
| 5028 | } |
| 5029 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5030 | /* mode: |
| 5031 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5032 | * 2 - stop but do not disassemble array |
| 5033 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5034 | static int do_md_stop(mddev_t * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5037 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5039 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5040 | if (atomic_read(&mddev->openers) > is_open || |
| 5041 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5042 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5043 | mutex_unlock(&mddev->open_mutex); |
| 5044 | return -EBUSY; |
| 5045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5047 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5048 | if (mddev->ro) |
| 5049 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5050 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5051 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5052 | md_stop(mddev); |
| 5053 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5054 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5055 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5056 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5057 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5058 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5059 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5060 | if (rdev->raid_disk >= 0) |
| 5061 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5062 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5063 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5064 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5065 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5066 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5067 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5068 | if (mddev->ro) |
| 5069 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5070 | } else |
| 5071 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | /* |
| 5073 | * Free resources if final stop |
| 5074 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5075 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5077 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5078 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5079 | if (mddev->bitmap_info.file) { |
| 5080 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5081 | fput(mddev->bitmap_info.file); |
| 5082 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5083 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5084 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | export_array(mddev); |
| 5087 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5088 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5089 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5090 | if (mddev->hold_active == UNTIL_STOP) |
| 5091 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5092 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5093 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5094 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5095 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5096 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | } |
| 5098 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5099 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | static void autorun_array(mddev_t *mddev) |
| 5101 | { |
| 5102 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | int err; |
| 5104 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5105 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | |
| 5108 | printk(KERN_INFO "md: running: "); |
| 5109 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5110 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | char b[BDEVNAME_SIZE]; |
| 5112 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5113 | } |
| 5114 | printk("\n"); |
| 5115 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5116 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | if (err) { |
| 5118 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5119 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | /* |
| 5124 | * lets try to run arrays based on all disks that have arrived |
| 5125 | * until now. (those are in pending_raid_disks) |
| 5126 | * |
| 5127 | * the method: pick the first pending disk, collect all disks with |
| 5128 | * the same UUID, remove all from the pending list and put them into |
| 5129 | * the 'same_array' list. Then order this list based on superblock |
| 5130 | * update time (freshest comes first), kick out 'old' disks and |
| 5131 | * compare superblocks. If everything's fine then run it. |
| 5132 | * |
| 5133 | * If "unit" is allocated, then bump its reference count |
| 5134 | */ |
| 5135 | static void autorun_devices(int part) |
| 5136 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5137 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | mddev_t *mddev; |
| 5139 | char b[BDEVNAME_SIZE]; |
| 5140 | |
| 5141 | printk(KERN_INFO "md: autorun ...\n"); |
| 5142 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5143 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5145 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | rdev0 = list_entry(pending_raid_disks.next, |
| 5147 | mdk_rdev_t, same_set); |
| 5148 | |
| 5149 | printk(KERN_INFO "md: considering %s ...\n", |
| 5150 | bdevname(rdev0->bdev,b)); |
| 5151 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5152 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5154 | printk(KERN_INFO "md: adding %s ...\n", |
| 5155 | bdevname(rdev->bdev,b)); |
| 5156 | list_move(&rdev->same_set, &candidates); |
| 5157 | } |
| 5158 | /* |
| 5159 | * now we have a set of devices, with all of them having |
| 5160 | * mostly sane superblocks. It's time to allocate the |
| 5161 | * mddev. |
| 5162 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5163 | if (part) { |
| 5164 | dev = MKDEV(mdp_major, |
| 5165 | rdev0->preferred_minor << MdpMinorShift); |
| 5166 | unit = MINOR(dev) >> MdpMinorShift; |
| 5167 | } else { |
| 5168 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5169 | unit = MINOR(dev); |
| 5170 | } |
| 5171 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5173 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5174 | break; |
| 5175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | |
| 5177 | md_probe(dev, NULL, NULL); |
| 5178 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5179 | if (!mddev || !mddev->gendisk) { |
| 5180 | if (mddev) |
| 5181 | mddev_put(mddev); |
| 5182 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | "md: cannot allocate memory for md drive.\n"); |
| 5184 | break; |
| 5185 | } |
| 5186 | if (mddev_lock(mddev)) |
| 5187 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5188 | mdname(mddev)); |
| 5189 | else if (mddev->raid_disks || mddev->major_version |
| 5190 | || !list_empty(&mddev->disks)) { |
| 5191 | printk(KERN_WARNING |
| 5192 | "md: %s already running, cannot run %s\n", |
| 5193 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5194 | mddev_unlock(mddev); |
| 5195 | } else { |
| 5196 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5197 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5198 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | list_del_init(&rdev->same_set); |
| 5200 | if (bind_rdev_to_array(rdev, mddev)) |
| 5201 | export_rdev(rdev); |
| 5202 | } |
| 5203 | autorun_array(mddev); |
| 5204 | mddev_unlock(mddev); |
| 5205 | } |
| 5206 | /* on success, candidates will be empty, on error |
| 5207 | * it won't... |
| 5208 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5209 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5210 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | mddev_put(mddev); |
| 5214 | } |
| 5215 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5216 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5217 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | static int get_version(void __user * arg) |
| 5220 | { |
| 5221 | mdu_version_t ver; |
| 5222 | |
| 5223 | ver.major = MD_MAJOR_VERSION; |
| 5224 | ver.minor = MD_MINOR_VERSION; |
| 5225 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5226 | |
| 5227 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5228 | return -EFAULT; |
| 5229 | |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
| 5233 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5234 | { |
| 5235 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5236 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5239 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5240 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5242 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | failed++; |
| 5244 | else { |
| 5245 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5246 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5247 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | else |
| 5249 | spare++; |
| 5250 | } |
| 5251 | } |
| 5252 | |
| 5253 | info.major_version = mddev->major_version; |
| 5254 | info.minor_version = mddev->minor_version; |
| 5255 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5256 | info.ctime = mddev->ctime; |
| 5257 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5258 | info.size = mddev->dev_sectors / 2; |
| 5259 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5260 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | info.nr_disks = nr; |
| 5262 | info.raid_disks = mddev->raid_disks; |
| 5263 | info.md_minor = mddev->md_minor; |
| 5264 | info.not_persistent= !mddev->persistent; |
| 5265 | |
| 5266 | info.utime = mddev->utime; |
| 5267 | info.state = 0; |
| 5268 | if (mddev->in_sync) |
| 5269 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5270 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5271 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5272 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | info.working_disks = working; |
| 5274 | info.failed_disks = failed; |
| 5275 | info.spare_disks = spare; |
| 5276 | |
| 5277 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5278 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
| 5280 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5281 | return -EFAULT; |
| 5282 | |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5286 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5287 | { |
| 5288 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5289 | char *ptr, *buf = NULL; |
| 5290 | int err = -ENOMEM; |
| 5291 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5292 | if (md_allow_write(mddev)) |
| 5293 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5294 | else |
| 5295 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5296 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5297 | if (!file) |
| 5298 | goto out; |
| 5299 | |
| 5300 | /* bitmap disabled, zero the first byte and copy out */ |
| 5301 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5302 | file->pathname[0] = '\0'; |
| 5303 | goto copy_out; |
| 5304 | } |
| 5305 | |
| 5306 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5307 | if (!buf) |
| 5308 | goto out; |
| 5309 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5310 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5311 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5312 | goto out; |
| 5313 | |
| 5314 | strcpy(file->pathname, ptr); |
| 5315 | |
| 5316 | copy_out: |
| 5317 | err = 0; |
| 5318 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5319 | err = -EFAULT; |
| 5320 | out: |
| 5321 | kfree(buf); |
| 5322 | kfree(file); |
| 5323 | return err; |
| 5324 | } |
| 5325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5327 | { |
| 5328 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | mdk_rdev_t *rdev; |
| 5330 | |
| 5331 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5332 | return -EFAULT; |
| 5333 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5334 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | if (rdev) { |
| 5336 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5337 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5338 | info.raid_disk = rdev->raid_disk; |
| 5339 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5340 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5342 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5344 | info.state |= (1<<MD_DISK_SYNC); |
| 5345 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5346 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5347 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | } else { |
| 5349 | info.major = info.minor = 0; |
| 5350 | info.raid_disk = -1; |
| 5351 | info.state = (1<<MD_DISK_REMOVED); |
| 5352 | } |
| 5353 | |
| 5354 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5355 | return -EFAULT; |
| 5356 | |
| 5357 | return 0; |
| 5358 | } |
| 5359 | |
| 5360 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5361 | { |
| 5362 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5363 | mdk_rdev_t *rdev; |
| 5364 | dev_t dev = MKDEV(info->major,info->minor); |
| 5365 | |
| 5366 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5367 | return -EOVERFLOW; |
| 5368 | |
| 5369 | if (!mddev->raid_disks) { |
| 5370 | int err; |
| 5371 | /* expecting a device which has a superblock */ |
| 5372 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5373 | if (IS_ERR(rdev)) { |
| 5374 | printk(KERN_WARNING |
| 5375 | "md: md_import_device returned %ld\n", |
| 5376 | PTR_ERR(rdev)); |
| 5377 | return PTR_ERR(rdev); |
| 5378 | } |
| 5379 | if (!list_empty(&mddev->disks)) { |
| 5380 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5381 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5382 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5384 | if (err < 0) { |
| 5385 | printk(KERN_WARNING |
| 5386 | "md: %s has different UUID to %s\n", |
| 5387 | bdevname(rdev->bdev,b), |
| 5388 | bdevname(rdev0->bdev,b2)); |
| 5389 | export_rdev(rdev); |
| 5390 | return -EINVAL; |
| 5391 | } |
| 5392 | } |
| 5393 | err = bind_rdev_to_array(rdev, mddev); |
| 5394 | if (err) |
| 5395 | export_rdev(rdev); |
| 5396 | return err; |
| 5397 | } |
| 5398 | |
| 5399 | /* |
| 5400 | * add_new_disk can be used once the array is assembled |
| 5401 | * to add "hot spares". They must already have a superblock |
| 5402 | * written |
| 5403 | */ |
| 5404 | if (mddev->pers) { |
| 5405 | int err; |
| 5406 | if (!mddev->pers->hot_add_disk) { |
| 5407 | printk(KERN_WARNING |
| 5408 | "%s: personality does not support diskops!\n", |
| 5409 | mdname(mddev)); |
| 5410 | return -EINVAL; |
| 5411 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5412 | if (mddev->persistent) |
| 5413 | rdev = md_import_device(dev, mddev->major_version, |
| 5414 | mddev->minor_version); |
| 5415 | else |
| 5416 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | if (IS_ERR(rdev)) { |
| 5418 | printk(KERN_WARNING |
| 5419 | "md: md_import_device returned %ld\n", |
| 5420 | PTR_ERR(rdev)); |
| 5421 | return PTR_ERR(rdev); |
| 5422 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5423 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5424 | if (!mddev->persistent) { |
| 5425 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5426 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5427 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5428 | set_bit(In_sync, &rdev->flags); |
| 5429 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5430 | rdev->raid_disk = -1; |
| 5431 | } else |
| 5432 | super_types[mddev->major_version]. |
| 5433 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5434 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5435 | (!test_bit(In_sync, &rdev->flags) || |
| 5436 | rdev->raid_disk != info->raid_disk)) { |
| 5437 | /* This was a hot-add request, but events doesn't |
| 5438 | * match, so reject it. |
| 5439 | */ |
| 5440 | export_rdev(rdev); |
| 5441 | return -EINVAL; |
| 5442 | } |
| 5443 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5444 | if (test_bit(In_sync, &rdev->flags)) |
| 5445 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5446 | else |
| 5447 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5448 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5449 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5450 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5451 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5452 | else |
| 5453 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | rdev->raid_disk = -1; |
| 5456 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5457 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5458 | /* If there is hot_add_disk but no hot_remove_disk |
| 5459 | * then added disks for geometry changes, |
| 5460 | * and should be added immediately. |
| 5461 | */ |
| 5462 | super_types[mddev->major_version]. |
| 5463 | validate_super(mddev, rdev); |
| 5464 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5465 | if (err) |
| 5466 | unbind_rdev_from_array(rdev); |
| 5467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | if (err) |
| 5469 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5470 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5471 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5472 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5473 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5474 | if (mddev->degraded) |
| 5475 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5476 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5477 | if (!err) |
| 5478 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5479 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | return err; |
| 5481 | } |
| 5482 | |
| 5483 | /* otherwise, add_new_disk is only allowed |
| 5484 | * for major_version==0 superblocks |
| 5485 | */ |
| 5486 | if (mddev->major_version != 0) { |
| 5487 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5488 | mdname(mddev)); |
| 5489 | return -EINVAL; |
| 5490 | } |
| 5491 | |
| 5492 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5493 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5494 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | if (IS_ERR(rdev)) { |
| 5496 | printk(KERN_WARNING |
| 5497 | "md: error, md_import_device() returned %ld\n", |
| 5498 | PTR_ERR(rdev)); |
| 5499 | return PTR_ERR(rdev); |
| 5500 | } |
| 5501 | rdev->desc_nr = info->number; |
| 5502 | if (info->raid_disk < mddev->raid_disks) |
| 5503 | rdev->raid_disk = info->raid_disk; |
| 5504 | else |
| 5505 | rdev->raid_disk = -1; |
| 5506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5508 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5509 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5511 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5512 | set_bit(WriteMostly, &rdev->flags); |
| 5513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | if (!mddev->persistent) { |
| 5515 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5516 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5517 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5518 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5519 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5521 | err = bind_rdev_to_array(rdev, mddev); |
| 5522 | if (err) { |
| 5523 | export_rdev(rdev); |
| 5524 | return err; |
| 5525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | return 0; |
| 5529 | } |
| 5530 | |
| 5531 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5532 | { |
| 5533 | char b[BDEVNAME_SIZE]; |
| 5534 | mdk_rdev_t *rdev; |
| 5535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | rdev = find_rdev(mddev, dev); |
| 5537 | if (!rdev) |
| 5538 | return -ENXIO; |
| 5539 | |
| 5540 | if (rdev->raid_disk >= 0) |
| 5541 | goto busy; |
| 5542 | |
| 5543 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5544 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5545 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | |
| 5547 | return 0; |
| 5548 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5549 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5551 | return -EBUSY; |
| 5552 | } |
| 5553 | |
| 5554 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5555 | { |
| 5556 | char b[BDEVNAME_SIZE]; |
| 5557 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | mdk_rdev_t *rdev; |
| 5559 | |
| 5560 | if (!mddev->pers) |
| 5561 | return -ENODEV; |
| 5562 | |
| 5563 | if (mddev->major_version != 0) { |
| 5564 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5565 | " version-0 superblocks.\n", |
| 5566 | mdname(mddev)); |
| 5567 | return -EINVAL; |
| 5568 | } |
| 5569 | if (!mddev->pers->hot_add_disk) { |
| 5570 | printk(KERN_WARNING |
| 5571 | "%s: personality does not support diskops!\n", |
| 5572 | mdname(mddev)); |
| 5573 | return -EINVAL; |
| 5574 | } |
| 5575 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5576 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | if (IS_ERR(rdev)) { |
| 5578 | printk(KERN_WARNING |
| 5579 | "md: error, md_import_device() returned %ld\n", |
| 5580 | PTR_ERR(rdev)); |
| 5581 | return -EINVAL; |
| 5582 | } |
| 5583 | |
| 5584 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5585 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5587 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5589 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5591 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | printk(KERN_WARNING |
| 5593 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5594 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5595 | err = -EINVAL; |
| 5596 | goto abort_export; |
| 5597 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5598 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5600 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5601 | err = bind_rdev_to_array(rdev, mddev); |
| 5602 | if (err) |
| 5603 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | |
| 5605 | /* |
| 5606 | * The rest should better be atomic, we can have disk failures |
| 5607 | * noticed in interrupt contexts ... |
| 5608 | */ |
| 5609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | rdev->raid_disk = -1; |
| 5611 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5612 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | |
| 5614 | /* |
| 5615 | * Kick recovery, maybe this spare has to be added to the |
| 5616 | * array immediately. |
| 5617 | */ |
| 5618 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5619 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5620 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | return 0; |
| 5622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | abort_export: |
| 5624 | export_rdev(rdev); |
| 5625 | return err; |
| 5626 | } |
| 5627 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5628 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5629 | { |
| 5630 | int err; |
| 5631 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5632 | if (mddev->pers) { |
| 5633 | if (!mddev->pers->quiesce) |
| 5634 | return -EBUSY; |
| 5635 | if (mddev->recovery || mddev->sync_thread) |
| 5636 | return -EBUSY; |
| 5637 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5638 | } |
| 5639 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5640 | |
| 5641 | if (fd >= 0) { |
| 5642 | if (mddev->bitmap) |
| 5643 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5644 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5645 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5646 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5647 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5648 | mdname(mddev)); |
| 5649 | return -EBADF; |
| 5650 | } |
| 5651 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5652 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5653 | if (err) { |
| 5654 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5655 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5656 | fput(mddev->bitmap_info.file); |
| 5657 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5658 | return err; |
| 5659 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5660 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5661 | } else if (mddev->bitmap == NULL) |
| 5662 | return -ENOENT; /* cannot remove what isn't there */ |
| 5663 | err = 0; |
| 5664 | if (mddev->pers) { |
| 5665 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5666 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5667 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5668 | if (!err) |
| 5669 | err = bitmap_load(mddev); |
| 5670 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5671 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5672 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5673 | fd = -1; /* make sure to put the file */ |
| 5674 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5675 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5676 | } |
| 5677 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5678 | if (mddev->bitmap_info.file) { |
| 5679 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5680 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5681 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5682 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5685 | return err; |
| 5686 | } |
| 5687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | /* |
| 5689 | * set_array_info is used two different ways |
| 5690 | * The original usage is when creating a new array. |
| 5691 | * In this usage, raid_disks is > 0 and it together with |
| 5692 | * level, size, not_persistent,layout,chunksize determine the |
| 5693 | * shape of the array. |
| 5694 | * This will always create an array with a type-0.90.0 superblock. |
| 5695 | * The newer usage is when assembling an array. |
| 5696 | * In this case raid_disks will be 0, and the major_version field is |
| 5697 | * use to determine which style super-blocks are to be found on the devices. |
| 5698 | * The minor and patch _version numbers are also kept incase the |
| 5699 | * super_block handler wishes to interpret them. |
| 5700 | */ |
| 5701 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5702 | { |
| 5703 | |
| 5704 | if (info->raid_disks == 0) { |
| 5705 | /* just setting version number for superblock loading */ |
| 5706 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5707 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | super_types[info->major_version].name == NULL) { |
| 5709 | /* maybe try to auto-load a module? */ |
| 5710 | printk(KERN_INFO |
| 5711 | "md: superblock version %d not known\n", |
| 5712 | info->major_version); |
| 5713 | return -EINVAL; |
| 5714 | } |
| 5715 | mddev->major_version = info->major_version; |
| 5716 | mddev->minor_version = info->minor_version; |
| 5717 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5718 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5719 | /* ensure mddev_put doesn't delete this now that there |
| 5720 | * is some minimal configuration. |
| 5721 | */ |
| 5722 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | return 0; |
| 5724 | } |
| 5725 | mddev->major_version = MD_MAJOR_VERSION; |
| 5726 | mddev->minor_version = MD_MINOR_VERSION; |
| 5727 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5728 | mddev->ctime = get_seconds(); |
| 5729 | |
| 5730 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5731 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5732 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | mddev->raid_disks = info->raid_disks; |
| 5734 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5735 | * openned |
| 5736 | */ |
| 5737 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5738 | mddev->recovery_cp = MaxSector; |
| 5739 | else |
| 5740 | mddev->recovery_cp = 0; |
| 5741 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5742 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | |
| 5744 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5745 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | |
| 5747 | mddev->max_disks = MD_SB_DISKS; |
| 5748 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5749 | if (mddev->persistent) |
| 5750 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5751 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5753 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5754 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5755 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5756 | mddev->reshape_position = MaxSector; |
| 5757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | /* |
| 5759 | * Generate a 128 bit UUID |
| 5760 | */ |
| 5761 | get_random_bytes(mddev->uuid, 16); |
| 5762 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5763 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5764 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5765 | mddev->new_layout = mddev->layout; |
| 5766 | mddev->delta_disks = 0; |
| 5767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | return 0; |
| 5769 | } |
| 5770 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5771 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5772 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5773 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5774 | |
| 5775 | if (mddev->external_size) |
| 5776 | return; |
| 5777 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5778 | mddev->array_sectors = array_sectors; |
| 5779 | } |
| 5780 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5781 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5782 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5783 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5784 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5785 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5786 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5787 | |
| 5788 | if (mddev->pers->resize == NULL) |
| 5789 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5790 | /* The "num_sectors" is the number of sectors of each device that |
| 5791 | * is used. This can only make sense for arrays with redundancy. |
| 5792 | * linear and raid0 always use whatever space is available. We can only |
| 5793 | * consider changing this number if no resync or reconstruction is |
| 5794 | * 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] | 5795 | * 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] | 5796 | * of each device. If num_sectors is zero, we find the largest size |
| 5797 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5798 | */ |
| 5799 | if (mddev->sync_thread) |
| 5800 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5801 | if (mddev->bitmap) |
| 5802 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5803 | * grow, and re-add. |
| 5804 | */ |
| 5805 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5806 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5807 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5808 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5809 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5810 | num_sectors = avail; |
| 5811 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5812 | return -ENOSPC; |
| 5813 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5814 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5815 | if (!rv) |
| 5816 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5817 | return rv; |
| 5818 | } |
| 5819 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5820 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5821 | { |
| 5822 | int rv; |
| 5823 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5824 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5825 | return -EINVAL; |
| 5826 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5827 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5828 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5829 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5830 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5831 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5832 | |
| 5833 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5834 | if (rv < 0) |
| 5835 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5836 | return rv; |
| 5837 | } |
| 5838 | |
| 5839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | /* |
| 5841 | * update_array_info is used to change the configuration of an |
| 5842 | * on-line array. |
| 5843 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5844 | * fields in the info are checked against the array. |
| 5845 | * Any differences that cannot be handled will cause an error. |
| 5846 | * Normally, only one change can be managed at a time. |
| 5847 | */ |
| 5848 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5849 | { |
| 5850 | int rv = 0; |
| 5851 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5852 | int state = 0; |
| 5853 | |
| 5854 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5855 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5856 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | |
| 5858 | if (mddev->major_version != info->major_version || |
| 5859 | mddev->minor_version != info->minor_version || |
| 5860 | /* mddev->patch_version != info->patch_version || */ |
| 5861 | mddev->ctime != info->ctime || |
| 5862 | mddev->level != info->level || |
| 5863 | /* mddev->layout != info->layout || */ |
| 5864 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5865 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5866 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5867 | ((state^info->state) & 0xfffffe00) |
| 5868 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | return -EINVAL; |
| 5870 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5871 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5872 | cnt++; |
| 5873 | if (mddev->raid_disks != info->raid_disks) |
| 5874 | cnt++; |
| 5875 | if (mddev->layout != info->layout) |
| 5876 | cnt++; |
| 5877 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5878 | cnt++; |
| 5879 | if (cnt == 0) |
| 5880 | return 0; |
| 5881 | if (cnt > 1) |
| 5882 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | |
| 5884 | if (mddev->layout != info->layout) { |
| 5885 | /* Change layout |
| 5886 | * we don't need to do anything at the md level, the |
| 5887 | * personality will take care of it all. |
| 5888 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5889 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5891 | else { |
| 5892 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5893 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5894 | if (rv) |
| 5895 | mddev->new_layout = mddev->layout; |
| 5896 | return rv; |
| 5897 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5899 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5900 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5902 | if (mddev->raid_disks != info->raid_disks) |
| 5903 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5905 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5906 | if (mddev->pers->quiesce == NULL) |
| 5907 | return -EINVAL; |
| 5908 | if (mddev->recovery || mddev->sync_thread) |
| 5909 | return -EBUSY; |
| 5910 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5911 | /* add the bitmap */ |
| 5912 | if (mddev->bitmap) |
| 5913 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5914 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5915 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5916 | mddev->bitmap_info.offset = |
| 5917 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5918 | mddev->pers->quiesce(mddev, 1); |
| 5919 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5920 | if (!rv) |
| 5921 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5922 | if (rv) |
| 5923 | bitmap_destroy(mddev); |
| 5924 | mddev->pers->quiesce(mddev, 0); |
| 5925 | } else { |
| 5926 | /* remove the bitmap */ |
| 5927 | if (!mddev->bitmap) |
| 5928 | return -ENOENT; |
| 5929 | if (mddev->bitmap->file) |
| 5930 | return -EINVAL; |
| 5931 | mddev->pers->quiesce(mddev, 1); |
| 5932 | bitmap_destroy(mddev); |
| 5933 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5934 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5935 | } |
| 5936 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5937 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | return rv; |
| 5939 | } |
| 5940 | |
| 5941 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5942 | { |
| 5943 | mdk_rdev_t *rdev; |
| 5944 | |
| 5945 | if (mddev->pers == NULL) |
| 5946 | return -ENODEV; |
| 5947 | |
| 5948 | rdev = find_rdev(mddev, dev); |
| 5949 | if (!rdev) |
| 5950 | return -ENODEV; |
| 5951 | |
| 5952 | md_error(mddev, rdev); |
| 5953 | return 0; |
| 5954 | } |
| 5955 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5956 | /* |
| 5957 | * We have a problem here : there is no easy way to give a CHS |
| 5958 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5959 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5960 | * dosfs just mad... ;-) |
| 5961 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5962 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5963 | { |
| 5964 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5965 | |
| 5966 | geo->heads = 2; |
| 5967 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5968 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5969 | return 0; |
| 5970 | } |
| 5971 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5972 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | unsigned int cmd, unsigned long arg) |
| 5974 | { |
| 5975 | int err = 0; |
| 5976 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5978 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | |
| 5980 | if (!capable(CAP_SYS_ADMIN)) |
| 5981 | return -EACCES; |
| 5982 | |
| 5983 | /* |
| 5984 | * Commands dealing with the RAID driver but not any |
| 5985 | * particular array: |
| 5986 | */ |
| 5987 | switch (cmd) |
| 5988 | { |
| 5989 | case RAID_VERSION: |
| 5990 | err = get_version(argp); |
| 5991 | goto done; |
| 5992 | |
| 5993 | case PRINT_RAID_DEBUG: |
| 5994 | err = 0; |
| 5995 | md_print_devices(); |
| 5996 | goto done; |
| 5997 | |
| 5998 | #ifndef MODULE |
| 5999 | case RAID_AUTORUN: |
| 6000 | err = 0; |
| 6001 | autostart_arrays(arg); |
| 6002 | goto done; |
| 6003 | #endif |
| 6004 | default:; |
| 6005 | } |
| 6006 | |
| 6007 | /* |
| 6008 | * Commands creating/starting a new array: |
| 6009 | */ |
| 6010 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6011 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | |
| 6013 | if (!mddev) { |
| 6014 | BUG(); |
| 6015 | goto abort; |
| 6016 | } |
| 6017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | err = mddev_lock(mddev); |
| 6019 | if (err) { |
| 6020 | printk(KERN_INFO |
| 6021 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6022 | err, cmd); |
| 6023 | goto abort; |
| 6024 | } |
| 6025 | |
| 6026 | switch (cmd) |
| 6027 | { |
| 6028 | case SET_ARRAY_INFO: |
| 6029 | { |
| 6030 | mdu_array_info_t info; |
| 6031 | if (!arg) |
| 6032 | memset(&info, 0, sizeof(info)); |
| 6033 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6034 | err = -EFAULT; |
| 6035 | goto abort_unlock; |
| 6036 | } |
| 6037 | if (mddev->pers) { |
| 6038 | err = update_array_info(mddev, &info); |
| 6039 | if (err) { |
| 6040 | printk(KERN_WARNING "md: couldn't update" |
| 6041 | " array info. %d\n", err); |
| 6042 | goto abort_unlock; |
| 6043 | } |
| 6044 | goto done_unlock; |
| 6045 | } |
| 6046 | if (!list_empty(&mddev->disks)) { |
| 6047 | printk(KERN_WARNING |
| 6048 | "md: array %s already has disks!\n", |
| 6049 | mdname(mddev)); |
| 6050 | err = -EBUSY; |
| 6051 | goto abort_unlock; |
| 6052 | } |
| 6053 | if (mddev->raid_disks) { |
| 6054 | printk(KERN_WARNING |
| 6055 | "md: array %s already initialised!\n", |
| 6056 | mdname(mddev)); |
| 6057 | err = -EBUSY; |
| 6058 | goto abort_unlock; |
| 6059 | } |
| 6060 | err = set_array_info(mddev, &info); |
| 6061 | if (err) { |
| 6062 | printk(KERN_WARNING "md: couldn't set" |
| 6063 | " array info. %d\n", err); |
| 6064 | goto abort_unlock; |
| 6065 | } |
| 6066 | } |
| 6067 | goto done_unlock; |
| 6068 | |
| 6069 | default:; |
| 6070 | } |
| 6071 | |
| 6072 | /* |
| 6073 | * Commands querying/configuring an existing array: |
| 6074 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6075 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6076 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6077 | if ((!mddev->raid_disks && !mddev->external) |
| 6078 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6079 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6080 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | err = -ENODEV; |
| 6082 | goto abort_unlock; |
| 6083 | } |
| 6084 | |
| 6085 | /* |
| 6086 | * Commands even a read-only array can execute: |
| 6087 | */ |
| 6088 | switch (cmd) |
| 6089 | { |
| 6090 | case GET_ARRAY_INFO: |
| 6091 | err = get_array_info(mddev, argp); |
| 6092 | goto done_unlock; |
| 6093 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6094 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6095 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6096 | goto done_unlock; |
| 6097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | case GET_DISK_INFO: |
| 6099 | err = get_disk_info(mddev, argp); |
| 6100 | goto done_unlock; |
| 6101 | |
| 6102 | case RESTART_ARRAY_RW: |
| 6103 | err = restart_array(mddev); |
| 6104 | goto done_unlock; |
| 6105 | |
| 6106 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6107 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | goto done_unlock; |
| 6109 | |
| 6110 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6111 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | goto done_unlock; |
| 6113 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6114 | case BLKROSET: |
| 6115 | if (get_user(ro, (int __user *)(arg))) { |
| 6116 | err = -EFAULT; |
| 6117 | goto done_unlock; |
| 6118 | } |
| 6119 | err = -EINVAL; |
| 6120 | |
| 6121 | /* if the bdev is going readonly the value of mddev->ro |
| 6122 | * does not matter, no writes are coming |
| 6123 | */ |
| 6124 | if (ro) |
| 6125 | goto done_unlock; |
| 6126 | |
| 6127 | /* are we are already prepared for writes? */ |
| 6128 | if (mddev->ro != 1) |
| 6129 | goto done_unlock; |
| 6130 | |
| 6131 | /* transitioning to readauto need only happen for |
| 6132 | * arrays that call md_write_start |
| 6133 | */ |
| 6134 | if (mddev->pers) { |
| 6135 | err = restart_array(mddev); |
| 6136 | if (err == 0) { |
| 6137 | mddev->ro = 2; |
| 6138 | set_disk_ro(mddev->gendisk, 0); |
| 6139 | } |
| 6140 | } |
| 6141 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | /* |
| 6145 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6146 | * superblock, so we do not allow them on read-only arrays. |
| 6147 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6148 | * here and hit the 'default' below, so only disallow |
| 6149 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6151 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6152 | if (mddev->ro == 2) { |
| 6153 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6154 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6155 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6156 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6157 | } else { |
| 6158 | err = -EROFS; |
| 6159 | goto abort_unlock; |
| 6160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | switch (cmd) |
| 6164 | { |
| 6165 | case ADD_NEW_DISK: |
| 6166 | { |
| 6167 | mdu_disk_info_t info; |
| 6168 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6169 | err = -EFAULT; |
| 6170 | else |
| 6171 | err = add_new_disk(mddev, &info); |
| 6172 | goto done_unlock; |
| 6173 | } |
| 6174 | |
| 6175 | case HOT_REMOVE_DISK: |
| 6176 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6177 | goto done_unlock; |
| 6178 | |
| 6179 | case HOT_ADD_DISK: |
| 6180 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6181 | goto done_unlock; |
| 6182 | |
| 6183 | case SET_DISK_FAULTY: |
| 6184 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6185 | goto done_unlock; |
| 6186 | |
| 6187 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6188 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | goto done_unlock; |
| 6190 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6191 | case SET_BITMAP_FILE: |
| 6192 | err = set_bitmap_file(mddev, (int)arg); |
| 6193 | goto done_unlock; |
| 6194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | err = -EINVAL; |
| 6197 | goto abort_unlock; |
| 6198 | } |
| 6199 | |
| 6200 | done_unlock: |
| 6201 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6202 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6203 | err != -EINVAL) |
| 6204 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | mddev_unlock(mddev); |
| 6206 | |
| 6207 | return err; |
| 6208 | done: |
| 6209 | if (err) |
| 6210 | MD_BUG(); |
| 6211 | abort: |
| 6212 | return err; |
| 6213 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6214 | #ifdef CONFIG_COMPAT |
| 6215 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6216 | unsigned int cmd, unsigned long arg) |
| 6217 | { |
| 6218 | switch (cmd) { |
| 6219 | case HOT_REMOVE_DISK: |
| 6220 | case HOT_ADD_DISK: |
| 6221 | case SET_DISK_FAULTY: |
| 6222 | case SET_BITMAP_FILE: |
| 6223 | /* These take in integer arg, do not convert */ |
| 6224 | break; |
| 6225 | default: |
| 6226 | arg = (unsigned long)compat_ptr(arg); |
| 6227 | break; |
| 6228 | } |
| 6229 | |
| 6230 | return md_ioctl(bdev, mode, cmd, arg); |
| 6231 | } |
| 6232 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6234 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | { |
| 6236 | /* |
| 6237 | * Succeed if we can lock the mddev, which confirms that |
| 6238 | * it isn't being stopped right now. |
| 6239 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6240 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | int err; |
| 6242 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6243 | if (mddev->gendisk != bdev->bd_disk) { |
| 6244 | /* we are racing with mddev_put which is discarding this |
| 6245 | * bd_disk. |
| 6246 | */ |
| 6247 | mddev_put(mddev); |
| 6248 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6249 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6250 | /* Then retry the open from the top */ |
| 6251 | return -ERESTARTSYS; |
| 6252 | } |
| 6253 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6254 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6255 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | goto out; |
| 6257 | |
| 6258 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6259 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6260 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6261 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6262 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | out: |
| 6264 | return err; |
| 6265 | } |
| 6266 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6267 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6269 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6271 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6272 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | mddev_put(mddev); |
| 6274 | |
| 6275 | return 0; |
| 6276 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6277 | |
| 6278 | static int md_media_changed(struct gendisk *disk) |
| 6279 | { |
| 6280 | mddev_t *mddev = disk->private_data; |
| 6281 | |
| 6282 | return mddev->changed; |
| 6283 | } |
| 6284 | |
| 6285 | static int md_revalidate(struct gendisk *disk) |
| 6286 | { |
| 6287 | mddev_t *mddev = disk->private_data; |
| 6288 | |
| 6289 | mddev->changed = 0; |
| 6290 | return 0; |
| 6291 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6292 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | { |
| 6294 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6295 | .open = md_open, |
| 6296 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6297 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6298 | #ifdef CONFIG_COMPAT |
| 6299 | .compat_ioctl = md_compat_ioctl, |
| 6300 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6301 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6302 | .media_changed = md_media_changed, |
| 6303 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | }; |
| 6305 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6306 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | { |
| 6308 | mdk_thread_t *thread = arg; |
| 6309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | /* |
| 6311 | * md_thread is a 'system-thread', it's priority should be very |
| 6312 | * high. We avoid resource deadlocks individually in each |
| 6313 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6314 | * the very same RT priority as kswapd, thus we will never get |
| 6315 | * into a priority inversion deadlock. |
| 6316 | * |
| 6317 | * we definitely have to have equal or higher priority than |
| 6318 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6319 | * many dirty RAID5 blocks. |
| 6320 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6322 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6323 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6325 | /* We need to wait INTERRUPTIBLE so that |
| 6326 | * we don't add to the load-average. |
| 6327 | * That means we need to be sure no signals are |
| 6328 | * pending |
| 6329 | */ |
| 6330 | if (signal_pending(current)) |
| 6331 | flush_signals(current); |
| 6332 | |
| 6333 | wait_event_interruptible_timeout |
| 6334 | (thread->wqueue, |
| 6335 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6336 | || kthread_should_stop(), |
| 6337 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6339 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6340 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6341 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | return 0; |
| 6345 | } |
| 6346 | |
| 6347 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6348 | { |
| 6349 | if (thread) { |
| 6350 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6351 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6352 | wake_up(&thread->wqueue); |
| 6353 | } |
| 6354 | } |
| 6355 | |
| 6356 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6357 | const char *name) |
| 6358 | { |
| 6359 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6361 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | if (!thread) |
| 6363 | return NULL; |
| 6364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | init_waitqueue_head(&thread->wqueue); |
| 6366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | thread->run = run; |
| 6368 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6369 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6370 | thread->tsk = kthread_run(md_thread, thread, |
| 6371 | "%s_%s", |
| 6372 | mdname(thread->mddev), |
| 6373 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6374 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | kfree(thread); |
| 6376 | return NULL; |
| 6377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | return thread; |
| 6379 | } |
| 6380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | void md_unregister_thread(mdk_thread_t *thread) |
| 6382 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6383 | if (!thread) |
| 6384 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6385 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6386 | |
| 6387 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | kfree(thread); |
| 6389 | } |
| 6390 | |
| 6391 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6392 | { |
| 6393 | if (!mddev) { |
| 6394 | MD_BUG(); |
| 6395 | return; |
| 6396 | } |
| 6397 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6398 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6400 | |
| 6401 | if (mddev->external) |
| 6402 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6403 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6405 | mdname(mddev), |
| 6406 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6407 | __builtin_return_address(0),__builtin_return_address(1), |
| 6408 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6409 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6410 | if (!mddev->pers) |
| 6411 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | if (!mddev->pers->error_handler) |
| 6413 | return; |
| 6414 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6415 | if (mddev->degraded) |
| 6416 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6417 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6419 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6420 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6421 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6422 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6423 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | } |
| 6425 | |
| 6426 | /* seq_file implementation /proc/mdstat */ |
| 6427 | |
| 6428 | static void status_unused(struct seq_file *seq) |
| 6429 | { |
| 6430 | int i = 0; |
| 6431 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | |
| 6433 | seq_printf(seq, "unused devices: "); |
| 6434 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6435 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | char b[BDEVNAME_SIZE]; |
| 6437 | i++; |
| 6438 | seq_printf(seq, "%s ", |
| 6439 | bdevname(rdev->bdev,b)); |
| 6440 | } |
| 6441 | if (!i) |
| 6442 | seq_printf(seq, "<none>"); |
| 6443 | |
| 6444 | seq_printf(seq, "\n"); |
| 6445 | } |
| 6446 | |
| 6447 | |
| 6448 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6449 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6450 | sector_t max_sectors, resync, res; |
| 6451 | unsigned long dt, db; |
| 6452 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6453 | int scale; |
| 6454 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6456 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | |
| 6458 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6459 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6461 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | |
| 6463 | /* |
| 6464 | * Should not happen. |
| 6465 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6466 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | MD_BUG(); |
| 6468 | return; |
| 6469 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6470 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6471 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6472 | * u32, as those are the requirements for sector_div. |
| 6473 | * Thus 'scale' must be at least 10 |
| 6474 | */ |
| 6475 | scale = 10; |
| 6476 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6477 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6478 | scale++; |
| 6479 | } |
| 6480 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6481 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6482 | |
| 6483 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6485 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | seq_printf(seq, "["); |
| 6487 | for (i = 0; i < x; i++) |
| 6488 | seq_printf(seq, "="); |
| 6489 | seq_printf(seq, ">"); |
| 6490 | for (i = 0; i < y; i++) |
| 6491 | seq_printf(seq, "."); |
| 6492 | seq_printf(seq, "] "); |
| 6493 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6494 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6495 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6496 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6497 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6498 | "check" : |
| 6499 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6500 | "resync" : "recovery"))), |
| 6501 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6502 | (unsigned long long) resync/2, |
| 6503 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | |
| 6505 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | * dt: time from mark until now |
| 6507 | * db: blocks written from mark until now |
| 6508 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6509 | * |
| 6510 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6511 | * So we divide before multiply in case it is 32bit and close |
| 6512 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6513 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6514 | * near the end of resync when the number of remaining sectors |
| 6515 | * is close to 'db'. |
| 6516 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6517 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | */ |
| 6519 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6520 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6521 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6522 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6524 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6525 | sector_div(rt, db/32+1); |
| 6526 | rt *= dt; |
| 6527 | rt >>= 5; |
| 6528 | |
| 6529 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6530 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6532 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6536 | { |
| 6537 | struct list_head *tmp; |
| 6538 | loff_t l = *pos; |
| 6539 | mddev_t *mddev; |
| 6540 | |
| 6541 | if (l >= 0x10000) |
| 6542 | return NULL; |
| 6543 | if (!l--) |
| 6544 | /* header */ |
| 6545 | return (void*)1; |
| 6546 | |
| 6547 | spin_lock(&all_mddevs_lock); |
| 6548 | list_for_each(tmp,&all_mddevs) |
| 6549 | if (!l--) { |
| 6550 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6551 | mddev_get(mddev); |
| 6552 | spin_unlock(&all_mddevs_lock); |
| 6553 | return mddev; |
| 6554 | } |
| 6555 | spin_unlock(&all_mddevs_lock); |
| 6556 | if (!l--) |
| 6557 | return (void*)2;/* tail */ |
| 6558 | return NULL; |
| 6559 | } |
| 6560 | |
| 6561 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6562 | { |
| 6563 | struct list_head *tmp; |
| 6564 | mddev_t *next_mddev, *mddev = v; |
| 6565 | |
| 6566 | ++*pos; |
| 6567 | if (v == (void*)2) |
| 6568 | return NULL; |
| 6569 | |
| 6570 | spin_lock(&all_mddevs_lock); |
| 6571 | if (v == (void*)1) |
| 6572 | tmp = all_mddevs.next; |
| 6573 | else |
| 6574 | tmp = mddev->all_mddevs.next; |
| 6575 | if (tmp != &all_mddevs) |
| 6576 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6577 | else { |
| 6578 | next_mddev = (void*)2; |
| 6579 | *pos = 0x10000; |
| 6580 | } |
| 6581 | spin_unlock(&all_mddevs_lock); |
| 6582 | |
| 6583 | if (v != (void*)1) |
| 6584 | mddev_put(mddev); |
| 6585 | return next_mddev; |
| 6586 | |
| 6587 | } |
| 6588 | |
| 6589 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6590 | { |
| 6591 | mddev_t *mddev = v; |
| 6592 | |
| 6593 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6594 | mddev_put(mddev); |
| 6595 | } |
| 6596 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6597 | struct mdstat_info { |
| 6598 | int event; |
| 6599 | }; |
| 6600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6602 | { |
| 6603 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6604 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6606 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6607 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6608 | |
| 6609 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6610 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | seq_printf(seq, "Personalities : "); |
| 6612 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6613 | list_for_each_entry(pers, &pers_list, list) |
| 6614 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | |
| 6616 | spin_unlock(&pers_lock); |
| 6617 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6618 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | return 0; |
| 6620 | } |
| 6621 | if (v == (void*)2) { |
| 6622 | status_unused(seq); |
| 6623 | return 0; |
| 6624 | } |
| 6625 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6626 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6630 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6631 | mddev->pers ? "" : "in"); |
| 6632 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6633 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6635 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6636 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | seq_printf(seq, " %s", mddev->pers->name); |
| 6638 | } |
| 6639 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6640 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6641 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | char b[BDEVNAME_SIZE]; |
| 6643 | seq_printf(seq, " %s[%d]", |
| 6644 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6645 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6646 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6647 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | seq_printf(seq, "(F)"); |
| 6649 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6650 | } else if (rdev->raid_disk < 0) |
| 6651 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6652 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | if (!list_empty(&mddev->disks)) { |
| 6656 | if (mddev->pers) |
| 6657 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6658 | (unsigned long long) |
| 6659 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | else |
| 6661 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6662 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6664 | if (mddev->persistent) { |
| 6665 | if (mddev->major_version != 0 || |
| 6666 | mddev->minor_version != 90) { |
| 6667 | seq_printf(seq," super %d.%d", |
| 6668 | mddev->major_version, |
| 6669 | mddev->minor_version); |
| 6670 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6671 | } else if (mddev->external) |
| 6672 | seq_printf(seq, " super external:%s", |
| 6673 | mddev->metadata_type); |
| 6674 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6675 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | |
| 6677 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6678 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6680 | if (mddev->pers->sync_request) { |
| 6681 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6682 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6683 | seq_printf(seq, "\n "); |
| 6684 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6685 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6686 | else if (mddev->recovery_cp < MaxSector) |
| 6687 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6688 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6689 | } else |
| 6690 | seq_printf(seq, "\n "); |
| 6691 | |
| 6692 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6693 | unsigned long chunk_kb; |
| 6694 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6695 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6696 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6697 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6698 | "%lu%s chunk", |
| 6699 | bitmap->pages - bitmap->missing_pages, |
| 6700 | bitmap->pages, |
| 6701 | (bitmap->pages - bitmap->missing_pages) |
| 6702 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6703 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6704 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6705 | if (bitmap->file) { |
| 6706 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6707 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6708 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6709 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6710 | seq_printf(seq, "\n"); |
| 6711 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | seq_printf(seq, "\n"); |
| 6715 | } |
| 6716 | mddev_unlock(mddev); |
| 6717 | |
| 6718 | return 0; |
| 6719 | } |
| 6720 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6721 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | .start = md_seq_start, |
| 6723 | .next = md_seq_next, |
| 6724 | .stop = md_seq_stop, |
| 6725 | .show = md_seq_show, |
| 6726 | }; |
| 6727 | |
| 6728 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6729 | { |
| 6730 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6731 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6732 | if (mi == NULL) |
| 6733 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | |
| 6735 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6736 | if (error) |
| 6737 | kfree(mi); |
| 6738 | else { |
| 6739 | struct seq_file *p = file->private_data; |
| 6740 | p->private = mi; |
| 6741 | mi->event = atomic_read(&md_event_count); |
| 6742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | return error; |
| 6744 | } |
| 6745 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6746 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6747 | { |
| 6748 | struct seq_file *m = filp->private_data; |
| 6749 | struct mdstat_info *mi = m->private; |
| 6750 | int mask; |
| 6751 | |
| 6752 | poll_wait(filp, &md_event_waiters, wait); |
| 6753 | |
| 6754 | /* always allow read */ |
| 6755 | mask = POLLIN | POLLRDNORM; |
| 6756 | |
| 6757 | if (mi->event != atomic_read(&md_event_count)) |
| 6758 | mask |= POLLERR | POLLPRI; |
| 6759 | return mask; |
| 6760 | } |
| 6761 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6762 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6763 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6764 | .open = md_seq_open, |
| 6765 | .read = seq_read, |
| 6766 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6767 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6768 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | }; |
| 6770 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6771 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6774 | list_add_tail(&p->list, &pers_list); |
| 6775 | 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] | 6776 | spin_unlock(&pers_lock); |
| 6777 | return 0; |
| 6778 | } |
| 6779 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6780 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6782 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6784 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | spin_unlock(&pers_lock); |
| 6786 | return 0; |
| 6787 | } |
| 6788 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6789 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | { |
| 6791 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6793 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | |
| 6795 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6796 | rcu_read_lock(); |
| 6797 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6799 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6800 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6801 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6802 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6803 | * as sync_io is counted when a request starts, and |
| 6804 | * disk_stats is counted when it completes. |
| 6805 | * So resync activity will cause curr_events to be smaller than |
| 6806 | * when there was no such activity. |
| 6807 | * non-sync IO will cause disk_stat to increase without |
| 6808 | * increasing sync_io so curr_events will (eventually) |
| 6809 | * be larger than it was before. Once it becomes |
| 6810 | * substantially larger, the test below will cause |
| 6811 | * the array to appear non-idle, and resync will slow |
| 6812 | * down. |
| 6813 | * If there is a lot of outstanding resync activity when |
| 6814 | * we set last_event to curr_events, then all that activity |
| 6815 | * completing might cause the array to appear non-idle |
| 6816 | * and resync will be slowed down even though there might |
| 6817 | * not have been non-resync activity. This will only |
| 6818 | * happen once though. 'last_events' will soon reflect |
| 6819 | * the state where there is little or no outstanding |
| 6820 | * resync requests, and further resync activity will |
| 6821 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6822 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6824 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | rdev->last_events = curr_events; |
| 6826 | idle = 0; |
| 6827 | } |
| 6828 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6829 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | return idle; |
| 6831 | } |
| 6832 | |
| 6833 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6834 | { |
| 6835 | /* another "blocks" (512byte) blocks have been synced */ |
| 6836 | atomic_sub(blocks, &mddev->recovery_active); |
| 6837 | wake_up(&mddev->recovery_wait); |
| 6838 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6839 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | md_wakeup_thread(mddev->thread); |
| 6841 | // stop recovery, signal do_sync .... |
| 6842 | } |
| 6843 | } |
| 6844 | |
| 6845 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6846 | /* md_write_start(mddev, bi) |
| 6847 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6848 | * in superblock) before writing, schedule a superblock update |
| 6849 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6850 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6851 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6853 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6854 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6855 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6856 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6857 | BUG_ON(mddev->ro == 1); |
| 6858 | if (mddev->ro == 2) { |
| 6859 | /* need to switch to read/write */ |
| 6860 | mddev->ro = 0; |
| 6861 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6862 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6863 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6864 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6865 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6866 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6867 | if (mddev->safemode == 1) |
| 6868 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6869 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6870 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6871 | if (mddev->in_sync) { |
| 6872 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6873 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6874 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6875 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6876 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6877 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6878 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6879 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6880 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6881 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6882 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6883 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | } |
| 6885 | |
| 6886 | void md_write_end(mddev_t *mddev) |
| 6887 | { |
| 6888 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6889 | if (mddev->safemode == 2) |
| 6890 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6891 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6893 | } |
| 6894 | } |
| 6895 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6896 | /* md_allow_write(mddev) |
| 6897 | * Calling this ensures that the array is marked 'active' so that writes |
| 6898 | * may proceed without blocking. It is important to call this before |
| 6899 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6900 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6901 | * |
| 6902 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6903 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6904 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6905 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6906 | { |
| 6907 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6908 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6909 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6910 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6911 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6912 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6913 | |
| 6914 | spin_lock_irq(&mddev->write_lock); |
| 6915 | if (mddev->in_sync) { |
| 6916 | mddev->in_sync = 0; |
| 6917 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6918 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6919 | if (mddev->safemode_delay && |
| 6920 | mddev->safemode == 0) |
| 6921 | mddev->safemode = 1; |
| 6922 | spin_unlock_irq(&mddev->write_lock); |
| 6923 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6924 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6925 | } else |
| 6926 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6927 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6928 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6929 | return -EAGAIN; |
| 6930 | else |
| 6931 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6932 | } |
| 6933 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | #define SYNC_MARKS 10 |
| 6936 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6937 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | { |
| 6939 | mddev_t *mddev2; |
| 6940 | unsigned int currspeed = 0, |
| 6941 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6942 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | unsigned long mark[SYNC_MARKS]; |
| 6944 | sector_t mark_cnt[SYNC_MARKS]; |
| 6945 | int last_mark,m; |
| 6946 | struct list_head *tmp; |
| 6947 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6948 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6949 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6950 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | |
| 6952 | /* just incase thread restarts... */ |
| 6953 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6954 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6955 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6956 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6958 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6959 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6960 | desc = "data-check"; |
| 6961 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6962 | desc = "requested-resync"; |
| 6963 | else |
| 6964 | desc = "resync"; |
| 6965 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6966 | desc = "reshape"; |
| 6967 | else |
| 6968 | desc = "recovery"; |
| 6969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | /* we overload curr_resync somewhat here. |
| 6971 | * 0 == not engaged in resync at all |
| 6972 | * 2 == checking that there is no conflict with another sync |
| 6973 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6974 | * commense |
| 6975 | * other == active in resync - this many blocks |
| 6976 | * |
| 6977 | * Before starting a resync we must have set curr_resync to |
| 6978 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6979 | * less than ours. When we find one that is the same or higher |
| 6980 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6981 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6982 | * This will mean we have to start checking from the beginning again. |
| 6983 | * |
| 6984 | */ |
| 6985 | |
| 6986 | do { |
| 6987 | mddev->curr_resync = 2; |
| 6988 | |
| 6989 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6990 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6991 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6992 | |
| 6993 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6995 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | if (mddev2 == mddev) |
| 6997 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6998 | if (!mddev->parallel_resync |
| 6999 | && mddev2->curr_resync |
| 7000 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | DEFINE_WAIT(wq); |
| 7002 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7003 | /* arbitrarily yield */ |
| 7004 | mddev->curr_resync = 1; |
| 7005 | wake_up(&resync_wait); |
| 7006 | } |
| 7007 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7008 | /* no need to wait here, we can wait the next |
| 7009 | * time 'round when curr_resync == 2 |
| 7010 | */ |
| 7011 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7012 | /* We need to wait 'interruptible' so as not to |
| 7013 | * contribute to the load average, and not to |
| 7014 | * be caught by 'softlockup' |
| 7015 | */ |
| 7016 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7017 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7018 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7019 | printk(KERN_INFO "md: delaying %s of %s" |
| 7020 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7022 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7024 | if (signal_pending(current)) |
| 7025 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | schedule(); |
| 7027 | finish_wait(&resync_wait, &wq); |
| 7028 | goto try_again; |
| 7029 | } |
| 7030 | finish_wait(&resync_wait, &wq); |
| 7031 | } |
| 7032 | } |
| 7033 | } while (mddev->curr_resync < 2); |
| 7034 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7035 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7036 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7038 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | */ |
| 7040 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7041 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7042 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7043 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7044 | j = mddev->resync_min; |
| 7045 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7046 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7047 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7048 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7049 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7050 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7052 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7053 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7054 | rcu_read_lock(); |
| 7055 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7056 | if (rdev->raid_disk >= 0 && |
| 7057 | !test_bit(Faulty, &rdev->flags) && |
| 7058 | !test_bit(In_sync, &rdev->flags) && |
| 7059 | rdev->recovery_offset < j) |
| 7060 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7061 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7064 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7065 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7066 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7067 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7068 | "(but not more than %d KB/sec) for %s.\n", |
| 7069 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7071 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7072 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7073 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7075 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7076 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | } |
| 7078 | last_mark = 0; |
| 7079 | mddev->resync_mark = mark[last_mark]; |
| 7080 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7081 | |
| 7082 | /* |
| 7083 | * Tune reconstruction: |
| 7084 | */ |
| 7085 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7086 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7087 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | |
| 7089 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7090 | last_check = 0; |
| 7091 | |
| 7092 | if (j>2) { |
| 7093 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7094 | "md: resuming %s of %s from checkpoint.\n", |
| 7095 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | mddev->curr_resync = j; |
| 7097 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7098 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7099 | |
| 7100 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7101 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7103 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7104 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7105 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7106 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7107 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7108 | > (max_sectors >> 4)) || |
| 7109 | (j - mddev->curr_resync_completed)*2 |
| 7110 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7111 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7112 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7113 | wait_event(mddev->recovery_wait, |
| 7114 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7115 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7116 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7117 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7118 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7119 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7120 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7121 | /* As this condition is controlled by user-space, |
| 7122 | * we can block indefinitely, so use '_interruptible' |
| 7123 | * to avoid triggering warnings. |
| 7124 | */ |
| 7125 | flush_signals(current); /* just in case */ |
| 7126 | wait_event_interruptible(mddev->recovery_wait, |
| 7127 | mddev->resync_max > j |
| 7128 | || kthread_should_stop()); |
| 7129 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7130 | |
| 7131 | if (kthread_should_stop()) |
| 7132 | goto interrupted; |
| 7133 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7134 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7135 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7136 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7137 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | goto out; |
| 7139 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7140 | |
| 7141 | if (!skipped) { /* actual IO requested */ |
| 7142 | io_sectors += sectors; |
| 7143 | atomic_add(sectors, &mddev->recovery_active); |
| 7144 | } |
| 7145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | j += sectors; |
| 7147 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7148 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7149 | if (last_check == 0) |
| 7150 | /* this is the earliers that rebuilt will be |
| 7151 | * visible in /proc/mdstat |
| 7152 | */ |
| 7153 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7154 | |
| 7155 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | continue; |
| 7157 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7158 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7160 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | break; |
| 7162 | |
| 7163 | repeat: |
| 7164 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7165 | /* step marks */ |
| 7166 | int next = (last_mark+1) % SYNC_MARKS; |
| 7167 | |
| 7168 | mddev->resync_mark = mark[next]; |
| 7169 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7170 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7171 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | last_mark = next; |
| 7173 | } |
| 7174 | |
| 7175 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7176 | if (kthread_should_stop()) |
| 7177 | goto interrupted; |
| 7178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | |
| 7180 | /* |
| 7181 | * this loop exits only if either when we are slower than |
| 7182 | * the 'hard' speed limit, or the system was IO-idle for |
| 7183 | * a jiffy. |
| 7184 | * the system might be non-idle CPU-wise, but we only care |
| 7185 | * about not overloading the IO subsystem. (things like an |
| 7186 | * e2fsck being done on the RAID array should execute fast) |
| 7187 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | cond_resched(); |
| 7189 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7190 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7191 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7193 | if (currspeed > speed_min(mddev)) { |
| 7194 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7195 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7196 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | goto repeat; |
| 7198 | } |
| 7199 | } |
| 7200 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7201 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | /* |
| 7203 | * this also signals 'finished resyncing' to md_stop |
| 7204 | */ |
| 7205 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7207 | |
| 7208 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7209 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7211 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7212 | mddev->curr_resync > 2) { |
| 7213 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7214 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7215 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7216 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7217 | "md: checkpointing %s of %s.\n", |
| 7218 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7219 | mddev->recovery_cp = mddev->curr_resync; |
| 7220 | } |
| 7221 | } else |
| 7222 | mddev->recovery_cp = MaxSector; |
| 7223 | } else { |
| 7224 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7225 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7226 | rcu_read_lock(); |
| 7227 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7228 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7229 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7230 | !test_bit(Faulty, &rdev->flags) && |
| 7231 | !test_bit(In_sync, &rdev->flags) && |
| 7232 | rdev->recovery_offset < mddev->curr_resync) |
| 7233 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7234 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7237 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7240 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7241 | /* We completed so min/max setting can be forgotten if used. */ |
| 7242 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7243 | mddev->resync_min = 0; |
| 7244 | mddev->resync_max = MaxSector; |
| 7245 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7246 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7247 | mddev->curr_resync = 0; |
| 7248 | wake_up(&resync_wait); |
| 7249 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7250 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7251 | return; |
| 7252 | |
| 7253 | interrupted: |
| 7254 | /* |
| 7255 | * got a signal, exit. |
| 7256 | */ |
| 7257 | printk(KERN_INFO |
| 7258 | "md: md_do_sync() got signal ... exiting\n"); |
| 7259 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7260 | goto out; |
| 7261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7263 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7264 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7265 | static int remove_and_add_spares(mddev_t *mddev) |
| 7266 | { |
| 7267 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7268 | int spares = 0; |
| 7269 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7270 | mddev->curr_resync_completed = 0; |
| 7271 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7272 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7273 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7274 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7275 | (test_bit(Faulty, &rdev->flags) || |
| 7276 | ! test_bit(In_sync, &rdev->flags)) && |
| 7277 | atomic_read(&rdev->nr_pending)==0) { |
| 7278 | if (mddev->pers->hot_remove_disk( |
| 7279 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7280 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7281 | rdev->raid_disk = -1; |
| 7282 | } |
| 7283 | } |
| 7284 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7285 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7286 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7287 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7288 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | 4274215 | 2011-06-28 16:59:42 +1000 | [diff] [blame] | 7289 | !test_bit(Faulty, &rdev->flags) && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7290 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7291 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7292 | if (rdev->raid_disk < 0 |
| 7293 | && !test_bit(Faulty, &rdev->flags)) { |
| 7294 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7295 | if (mddev->pers-> |
| 7296 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7297 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7298 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7299 | spares++; |
| 7300 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7301 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7302 | } else |
| 7303 | break; |
| 7304 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7305 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7306 | } |
| 7307 | return spares; |
| 7308 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7309 | |
| 7310 | static void reap_sync_thread(mddev_t *mddev) |
| 7311 | { |
| 7312 | mdk_rdev_t *rdev; |
| 7313 | |
| 7314 | /* resync has finished, collect result */ |
| 7315 | md_unregister_thread(mddev->sync_thread); |
| 7316 | mddev->sync_thread = NULL; |
| 7317 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7318 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7319 | /* success...*/ |
| 7320 | /* activate any spares */ |
| 7321 | if (mddev->pers->spare_active(mddev)) |
| 7322 | sysfs_notify(&mddev->kobj, NULL, |
| 7323 | "degraded"); |
| 7324 | } |
| 7325 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7326 | mddev->pers->finish_reshape) |
| 7327 | mddev->pers->finish_reshape(mddev); |
| 7328 | md_update_sb(mddev, 1); |
| 7329 | |
| 7330 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7331 | * information must be scrapped |
| 7332 | */ |
| 7333 | if (!mddev->degraded) |
| 7334 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7335 | rdev->saved_raid_disk = -1; |
| 7336 | |
| 7337 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7338 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7339 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7340 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7341 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7342 | /* flag recovery needed just to double check */ |
| 7343 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7344 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7345 | md_new_event(mddev); |
Jonathan Brassow | 768e587 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7346 | if (mddev->event_work.func) |
| 7347 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7348 | } |
| 7349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7350 | /* |
| 7351 | * This routine is regularly called by all per-raid-array threads to |
| 7352 | * deal with generic issues like resync and super-block update. |
| 7353 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7354 | * need this as they never do any recovery or update the superblock. |
| 7355 | * |
| 7356 | * It does not do any resync itself, but rather "forks" off other threads |
| 7357 | * to do that as needed. |
| 7358 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7359 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7360 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | * and wakeups up this thread which will reap the thread and finish up. |
| 7362 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7363 | * |
| 7364 | * The overall approach is: |
| 7365 | * 1/ if the superblock needs updating, update it. |
| 7366 | * 2/ If a recovery thread is running, don't do anything else. |
| 7367 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7368 | * 4/ If there are any faulty devices, remove them. |
| 7369 | * 5/ If array is degraded, try to add spares devices |
| 7370 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7371 | */ |
| 7372 | void md_check_recovery(mddev_t *mddev) |
| 7373 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7374 | if (mddev->suspended) |
| 7375 | return; |
| 7376 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7377 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7378 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7379 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7380 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7381 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7382 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7383 | mdname(mddev)); |
| 7384 | mddev->safemode = 2; |
| 7385 | } |
| 7386 | flush_signals(current); |
| 7387 | } |
| 7388 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7389 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7390 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7392 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7394 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7395 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7396 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7397 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | )) |
| 7399 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7400 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7401 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7402 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7403 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7404 | if (mddev->ro) { |
| 7405 | /* Only thing we do on a ro array is remove |
| 7406 | * failed devices. |
| 7407 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7408 | mdk_rdev_t *rdev; |
| 7409 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7410 | if (rdev->raid_disk >= 0 && |
| 7411 | !test_bit(Blocked, &rdev->flags) && |
| 7412 | test_bit(Faulty, &rdev->flags) && |
| 7413 | atomic_read(&rdev->nr_pending)==0) { |
| 7414 | if (mddev->pers->hot_remove_disk( |
| 7415 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7416 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7417 | rdev->raid_disk = -1; |
| 7418 | } |
| 7419 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7420 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7421 | goto unlock; |
| 7422 | } |
| 7423 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7424 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7425 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7426 | spin_lock_irq(&mddev->write_lock); |
| 7427 | if (mddev->safemode && |
| 7428 | !atomic_read(&mddev->writes_pending) && |
| 7429 | !mddev->in_sync && |
| 7430 | mddev->recovery_cp == MaxSector) { |
| 7431 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7432 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7433 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7434 | } |
| 7435 | if (mddev->safemode == 1) |
| 7436 | mddev->safemode = 0; |
| 7437 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7438 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7439 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7440 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7441 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7442 | if (mddev->flags) |
| 7443 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7446 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7447 | /* resync/recovery still happening */ |
| 7448 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7449 | goto unlock; |
| 7450 | } |
| 7451 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7452 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | goto unlock; |
| 7454 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7455 | /* Set RUNNING before clearing NEEDED to avoid |
| 7456 | * any transients in the value of "sync_action". |
| 7457 | */ |
| 7458 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7459 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7460 | /* Clear some bits that don't mean anything, but |
| 7461 | * might be left set |
| 7462 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7463 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7464 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7466 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7467 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | /* no recovery is running. |
| 7469 | * remove any failed drives, then |
| 7470 | * add spares if possible. |
| 7471 | * Spare are also removed and re-added, to allow |
| 7472 | * the personality to fail the re-add. |
| 7473 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7475 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7476 | if (mddev->pers->check_reshape == NULL || |
| 7477 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7478 | /* Cannot proceed */ |
| 7479 | goto unlock; |
| 7480 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7481 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7482 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7483 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7484 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7485 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7486 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7487 | } else if (mddev->recovery_cp < MaxSector) { |
| 7488 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7489 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7490 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7491 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7492 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7495 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7496 | /* We are adding a device or devices to an array |
| 7497 | * which has the bitmap stored on all devices. |
| 7498 | * So make sure all bitmap pages get written |
| 7499 | */ |
| 7500 | bitmap_write_all(mddev->bitmap); |
| 7501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7502 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7503 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7504 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7505 | if (!mddev->sync_thread) { |
| 7506 | printk(KERN_ERR "%s: could not start resync" |
| 7507 | " thread...\n", |
| 7508 | mdname(mddev)); |
| 7509 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7510 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7511 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7512 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7513 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7514 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7515 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7517 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7518 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7519 | } |
| 7520 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7521 | if (!mddev->sync_thread) { |
| 7522 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7523 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7524 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7525 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7526 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | mddev_unlock(mddev); |
| 7529 | } |
| 7530 | } |
| 7531 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7532 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7533 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7534 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7535 | wait_event_timeout(rdev->blocked_wait, |
| 7536 | !test_bit(Blocked, &rdev->flags), |
| 7537 | msecs_to_jiffies(5000)); |
| 7538 | rdev_dec_pending(rdev, mddev); |
| 7539 | } |
| 7540 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7541 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7542 | |
| 7543 | /* Bad block management. |
| 7544 | * We can record which blocks on each device are 'bad' and so just |
| 7545 | * fail those blocks, or that stripe, rather than the whole device. |
| 7546 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7547 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7548 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7549 | * A 'shift' can be set so that larger blocks are tracked and |
| 7550 | * consequently larger devices can be covered. |
| 7551 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7552 | * |
| 7553 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7554 | * might need to retry if it is very unlucky. |
| 7555 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7556 | * so we use the write_seqlock_irq variant. |
| 7557 | * |
| 7558 | * When looking for a bad block we specify a range and want to |
| 7559 | * know if any block in the range is bad. So we binary-search |
| 7560 | * to the last range that starts at-or-before the given endpoint, |
| 7561 | * (or "before the sector after the target range") |
| 7562 | * then see if it ends after the given start. |
| 7563 | * We return |
| 7564 | * 0 if there are no known bad blocks in the range |
| 7565 | * 1 if there are known bad block which are all acknowledged |
| 7566 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7567 | * plus the start/length of the first bad section we overlap. |
| 7568 | */ |
| 7569 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7570 | sector_t *first_bad, int *bad_sectors) |
| 7571 | { |
| 7572 | int hi; |
| 7573 | int lo = 0; |
| 7574 | u64 *p = bb->page; |
| 7575 | int rv = 0; |
| 7576 | sector_t target = s + sectors; |
| 7577 | unsigned seq; |
| 7578 | |
| 7579 | if (bb->shift > 0) { |
| 7580 | /* round the start down, and the end up */ |
| 7581 | s >>= bb->shift; |
| 7582 | target += (1<<bb->shift) - 1; |
| 7583 | target >>= bb->shift; |
| 7584 | sectors = target - s; |
| 7585 | } |
| 7586 | /* 'target' is now the first block after the bad range */ |
| 7587 | |
| 7588 | retry: |
| 7589 | seq = read_seqbegin(&bb->lock); |
| 7590 | |
| 7591 | hi = bb->count; |
| 7592 | |
| 7593 | /* Binary search between lo and hi for 'target' |
| 7594 | * i.e. for the last range that starts before 'target' |
| 7595 | */ |
| 7596 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7597 | * are known not to be the last range before target. |
| 7598 | * VARIANT: hi-lo is the number of possible |
| 7599 | * ranges, and decreases until it reaches 1 |
| 7600 | */ |
| 7601 | while (hi - lo > 1) { |
| 7602 | int mid = (lo + hi) / 2; |
| 7603 | sector_t a = BB_OFFSET(p[mid]); |
| 7604 | if (a < target) |
| 7605 | /* This could still be the one, earlier ranges |
| 7606 | * could not. */ |
| 7607 | lo = mid; |
| 7608 | else |
| 7609 | /* This and later ranges are definitely out. */ |
| 7610 | hi = mid; |
| 7611 | } |
| 7612 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7613 | if (hi > lo) { |
| 7614 | /* need to check all range that end after 's' to see if |
| 7615 | * any are unacknowledged. |
| 7616 | */ |
| 7617 | while (lo >= 0 && |
| 7618 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7619 | if (BB_OFFSET(p[lo]) < target) { |
| 7620 | /* starts before the end, and finishes after |
| 7621 | * the start, so they must overlap |
| 7622 | */ |
| 7623 | if (rv != -1 && BB_ACK(p[lo])) |
| 7624 | rv = 1; |
| 7625 | else |
| 7626 | rv = -1; |
| 7627 | *first_bad = BB_OFFSET(p[lo]); |
| 7628 | *bad_sectors = BB_LEN(p[lo]); |
| 7629 | } |
| 7630 | lo--; |
| 7631 | } |
| 7632 | } |
| 7633 | |
| 7634 | if (read_seqretry(&bb->lock, seq)) |
| 7635 | goto retry; |
| 7636 | |
| 7637 | return rv; |
| 7638 | } |
| 7639 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7640 | |
| 7641 | /* |
| 7642 | * Add a range of bad blocks to the table. |
| 7643 | * This might extend the table, or might contract it |
| 7644 | * if two adjacent ranges can be merged. |
| 7645 | * We binary-search to find the 'insertion' point, then |
| 7646 | * decide how best to handle it. |
| 7647 | */ |
| 7648 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7649 | int acknowledged) |
| 7650 | { |
| 7651 | u64 *p; |
| 7652 | int lo, hi; |
| 7653 | int rv = 1; |
| 7654 | |
| 7655 | if (bb->shift < 0) |
| 7656 | /* badblocks are disabled */ |
| 7657 | return 0; |
| 7658 | |
| 7659 | if (bb->shift) { |
| 7660 | /* round the start down, and the end up */ |
| 7661 | sector_t next = s + sectors; |
| 7662 | s >>= bb->shift; |
| 7663 | next += (1<<bb->shift) - 1; |
| 7664 | next >>= bb->shift; |
| 7665 | sectors = next - s; |
| 7666 | } |
| 7667 | |
| 7668 | write_seqlock_irq(&bb->lock); |
| 7669 | |
| 7670 | p = bb->page; |
| 7671 | lo = 0; |
| 7672 | hi = bb->count; |
| 7673 | /* Find the last range that starts at-or-before 's' */ |
| 7674 | while (hi - lo > 1) { |
| 7675 | int mid = (lo + hi) / 2; |
| 7676 | sector_t a = BB_OFFSET(p[mid]); |
| 7677 | if (a <= s) |
| 7678 | lo = mid; |
| 7679 | else |
| 7680 | hi = mid; |
| 7681 | } |
| 7682 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7683 | hi = lo; |
| 7684 | |
| 7685 | if (hi > lo) { |
| 7686 | /* we found a range that might merge with the start |
| 7687 | * of our new range |
| 7688 | */ |
| 7689 | sector_t a = BB_OFFSET(p[lo]); |
| 7690 | sector_t e = a + BB_LEN(p[lo]); |
| 7691 | int ack = BB_ACK(p[lo]); |
| 7692 | if (e >= s) { |
| 7693 | /* Yes, we can merge with a previous range */ |
| 7694 | if (s == a && s + sectors >= e) |
| 7695 | /* new range covers old */ |
| 7696 | ack = acknowledged; |
| 7697 | else |
| 7698 | ack = ack && acknowledged; |
| 7699 | |
| 7700 | if (e < s + sectors) |
| 7701 | e = s + sectors; |
| 7702 | if (e - a <= BB_MAX_LEN) { |
| 7703 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7704 | s = e; |
| 7705 | } else { |
| 7706 | /* does not all fit in one range, |
| 7707 | * make p[lo] maximal |
| 7708 | */ |
| 7709 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7710 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7711 | s = a + BB_MAX_LEN; |
| 7712 | } |
| 7713 | sectors = e - s; |
| 7714 | } |
| 7715 | } |
| 7716 | if (sectors && hi < bb->count) { |
| 7717 | /* 'hi' points to the first range that starts after 's'. |
| 7718 | * Maybe we can merge with the start of that range */ |
| 7719 | sector_t a = BB_OFFSET(p[hi]); |
| 7720 | sector_t e = a + BB_LEN(p[hi]); |
| 7721 | int ack = BB_ACK(p[hi]); |
| 7722 | if (a <= s + sectors) { |
| 7723 | /* merging is possible */ |
| 7724 | if (e <= s + sectors) { |
| 7725 | /* full overlap */ |
| 7726 | e = s + sectors; |
| 7727 | ack = acknowledged; |
| 7728 | } else |
| 7729 | ack = ack && acknowledged; |
| 7730 | |
| 7731 | a = s; |
| 7732 | if (e - a <= BB_MAX_LEN) { |
| 7733 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7734 | s = e; |
| 7735 | } else { |
| 7736 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7737 | s = a + BB_MAX_LEN; |
| 7738 | } |
| 7739 | sectors = e - s; |
| 7740 | lo = hi; |
| 7741 | hi++; |
| 7742 | } |
| 7743 | } |
| 7744 | if (sectors == 0 && hi < bb->count) { |
| 7745 | /* we might be able to combine lo and hi */ |
| 7746 | /* Note: 's' is at the end of 'lo' */ |
| 7747 | sector_t a = BB_OFFSET(p[hi]); |
| 7748 | int lolen = BB_LEN(p[lo]); |
| 7749 | int hilen = BB_LEN(p[hi]); |
| 7750 | int newlen = lolen + hilen - (s - a); |
| 7751 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7752 | /* yes, we can combine them */ |
| 7753 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7754 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7755 | memmove(p + hi, p + hi + 1, |
| 7756 | (bb->count - hi - 1) * 8); |
| 7757 | bb->count--; |
| 7758 | } |
| 7759 | } |
| 7760 | while (sectors) { |
| 7761 | /* didn't merge (it all). |
| 7762 | * Need to add a range just before 'hi' */ |
| 7763 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7764 | /* No room for more */ |
| 7765 | rv = 0; |
| 7766 | break; |
| 7767 | } else { |
| 7768 | int this_sectors = sectors; |
| 7769 | memmove(p + hi + 1, p + hi, |
| 7770 | (bb->count - hi) * 8); |
| 7771 | bb->count++; |
| 7772 | |
| 7773 | if (this_sectors > BB_MAX_LEN) |
| 7774 | this_sectors = BB_MAX_LEN; |
| 7775 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7776 | sectors -= this_sectors; |
| 7777 | s += this_sectors; |
| 7778 | } |
| 7779 | } |
| 7780 | |
| 7781 | bb->changed = 1; |
| 7782 | write_sequnlock_irq(&bb->lock); |
| 7783 | |
| 7784 | return rv; |
| 7785 | } |
| 7786 | |
| 7787 | int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors, |
| 7788 | int acknowledged) |
| 7789 | { |
| 7790 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7791 | s + rdev->data_offset, sectors, acknowledged); |
| 7792 | if (rv) { |
| 7793 | /* Make sure they get written out promptly */ |
| 7794 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7795 | md_wakeup_thread(rdev->mddev->thread); |
| 7796 | } |
| 7797 | return rv; |
| 7798 | } |
| 7799 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7800 | |
| 7801 | /* |
| 7802 | * Remove a range of bad blocks from the table. |
| 7803 | * This may involve extending the table if we spilt a region, |
| 7804 | * but it must not fail. So if the table becomes full, we just |
| 7805 | * drop the remove request. |
| 7806 | */ |
| 7807 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7808 | { |
| 7809 | u64 *p; |
| 7810 | int lo, hi; |
| 7811 | sector_t target = s + sectors; |
| 7812 | int rv = 0; |
| 7813 | |
| 7814 | if (bb->shift > 0) { |
| 7815 | /* When clearing we round the start up and the end down. |
| 7816 | * This should not matter as the shift should align with |
| 7817 | * the block size and no rounding should ever be needed. |
| 7818 | * However it is better the think a block is bad when it |
| 7819 | * isn't than to think a block is not bad when it is. |
| 7820 | */ |
| 7821 | s += (1<<bb->shift) - 1; |
| 7822 | s >>= bb->shift; |
| 7823 | target >>= bb->shift; |
| 7824 | sectors = target - s; |
| 7825 | } |
| 7826 | |
| 7827 | write_seqlock_irq(&bb->lock); |
| 7828 | |
| 7829 | p = bb->page; |
| 7830 | lo = 0; |
| 7831 | hi = bb->count; |
| 7832 | /* Find the last range that starts before 'target' */ |
| 7833 | while (hi - lo > 1) { |
| 7834 | int mid = (lo + hi) / 2; |
| 7835 | sector_t a = BB_OFFSET(p[mid]); |
| 7836 | if (a < target) |
| 7837 | lo = mid; |
| 7838 | else |
| 7839 | hi = mid; |
| 7840 | } |
| 7841 | if (hi > lo) { |
| 7842 | /* p[lo] is the last range that could overlap the |
| 7843 | * current range. Earlier ranges could also overlap, |
| 7844 | * but only this one can overlap the end of the range. |
| 7845 | */ |
| 7846 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7847 | /* Partial overlap, leave the tail of this range */ |
| 7848 | int ack = BB_ACK(p[lo]); |
| 7849 | sector_t a = BB_OFFSET(p[lo]); |
| 7850 | sector_t end = a + BB_LEN(p[lo]); |
| 7851 | |
| 7852 | if (a < s) { |
| 7853 | /* we need to split this range */ |
| 7854 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7855 | rv = 0; |
| 7856 | goto out; |
| 7857 | } |
| 7858 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7859 | bb->count++; |
| 7860 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7861 | lo++; |
| 7862 | } |
| 7863 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7864 | /* there is no longer an overlap */ |
| 7865 | hi = lo; |
| 7866 | lo--; |
| 7867 | } |
| 7868 | while (lo >= 0 && |
| 7869 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7870 | /* This range does overlap */ |
| 7871 | if (BB_OFFSET(p[lo]) < s) { |
| 7872 | /* Keep the early parts of this range. */ |
| 7873 | int ack = BB_ACK(p[lo]); |
| 7874 | sector_t start = BB_OFFSET(p[lo]); |
| 7875 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7876 | /* now low doesn't overlap, so.. */ |
| 7877 | break; |
| 7878 | } |
| 7879 | lo--; |
| 7880 | } |
| 7881 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7882 | * anything between needs to be discarded |
| 7883 | */ |
| 7884 | if (hi - lo > 1) { |
| 7885 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7886 | bb->count -= (hi - lo - 1); |
| 7887 | } |
| 7888 | } |
| 7889 | |
| 7890 | bb->changed = 1; |
| 7891 | out: |
| 7892 | write_sequnlock_irq(&bb->lock); |
| 7893 | return rv; |
| 7894 | } |
| 7895 | |
| 7896 | int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors) |
| 7897 | { |
| 7898 | return md_clear_badblocks(&rdev->badblocks, |
| 7899 | s + rdev->data_offset, |
| 7900 | sectors); |
| 7901 | } |
| 7902 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 7903 | |
| 7904 | /* |
| 7905 | * Acknowledge all bad blocks in a list. |
| 7906 | * This only succeeds if ->changed is clear. It is used by |
| 7907 | * in-kernel metadata updates |
| 7908 | */ |
| 7909 | void md_ack_all_badblocks(struct badblocks *bb) |
| 7910 | { |
| 7911 | if (bb->page == NULL || bb->changed) |
| 7912 | /* no point even trying */ |
| 7913 | return; |
| 7914 | write_seqlock_irq(&bb->lock); |
| 7915 | |
| 7916 | if (bb->changed == 0) { |
| 7917 | u64 *p = bb->page; |
| 7918 | int i; |
| 7919 | for (i = 0; i < bb->count ; i++) { |
| 7920 | if (!BB_ACK(p[i])) { |
| 7921 | sector_t start = BB_OFFSET(p[i]); |
| 7922 | int len = BB_LEN(p[i]); |
| 7923 | p[i] = BB_MAKE(start, len, 1); |
| 7924 | } |
| 7925 | } |
| 7926 | } |
| 7927 | write_sequnlock_irq(&bb->lock); |
| 7928 | } |
| 7929 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 7930 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7931 | /* sysfs access to bad-blocks list. |
| 7932 | * We present two files. |
| 7933 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 7934 | * are recorded as bad. The list is truncated to fit within |
| 7935 | * the one-page limit of sysfs. |
| 7936 | * Writing "sector length" to this file adds an acknowledged |
| 7937 | * bad block list. |
| 7938 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 7939 | * been acknowledged. Writing to this file adds bad blocks |
| 7940 | * without acknowledging them. This is largely for testing. |
| 7941 | */ |
| 7942 | |
| 7943 | static ssize_t |
| 7944 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 7945 | { |
| 7946 | size_t len; |
| 7947 | int i; |
| 7948 | u64 *p = bb->page; |
| 7949 | unsigned seq; |
| 7950 | |
| 7951 | if (bb->shift < 0) |
| 7952 | return 0; |
| 7953 | |
| 7954 | retry: |
| 7955 | seq = read_seqbegin(&bb->lock); |
| 7956 | |
| 7957 | len = 0; |
| 7958 | i = 0; |
| 7959 | |
| 7960 | while (len < PAGE_SIZE && i < bb->count) { |
| 7961 | sector_t s = BB_OFFSET(p[i]); |
| 7962 | unsigned int length = BB_LEN(p[i]); |
| 7963 | int ack = BB_ACK(p[i]); |
| 7964 | i++; |
| 7965 | |
| 7966 | if (unack && ack) |
| 7967 | continue; |
| 7968 | |
| 7969 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 7970 | (unsigned long long)s << bb->shift, |
| 7971 | length << bb->shift); |
| 7972 | } |
| 7973 | |
| 7974 | if (read_seqretry(&bb->lock, seq)) |
| 7975 | goto retry; |
| 7976 | |
| 7977 | return len; |
| 7978 | } |
| 7979 | |
| 7980 | #define DO_DEBUG 1 |
| 7981 | |
| 7982 | static ssize_t |
| 7983 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 7984 | { |
| 7985 | unsigned long long sector; |
| 7986 | int length; |
| 7987 | char newline; |
| 7988 | #ifdef DO_DEBUG |
| 7989 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 7990 | * Normally only a successful write may clear a badblock |
| 7991 | */ |
| 7992 | int clear = 0; |
| 7993 | if (page[0] == '-') { |
| 7994 | clear = 1; |
| 7995 | page++; |
| 7996 | } |
| 7997 | #endif /* DO_DEBUG */ |
| 7998 | |
| 7999 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8000 | case 3: |
| 8001 | if (newline != '\n') |
| 8002 | return -EINVAL; |
| 8003 | case 2: |
| 8004 | if (length <= 0) |
| 8005 | return -EINVAL; |
| 8006 | break; |
| 8007 | default: |
| 8008 | return -EINVAL; |
| 8009 | } |
| 8010 | |
| 8011 | #ifdef DO_DEBUG |
| 8012 | if (clear) { |
| 8013 | md_clear_badblocks(bb, sector, length); |
| 8014 | return len; |
| 8015 | } |
| 8016 | #endif /* DO_DEBUG */ |
| 8017 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8018 | return len; |
| 8019 | else |
| 8020 | return -ENOSPC; |
| 8021 | } |
| 8022 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8023 | static int md_notify_reboot(struct notifier_block *this, |
| 8024 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8025 | { |
| 8026 | struct list_head *tmp; |
| 8027 | mddev_t *mddev; |
| 8028 | |
| 8029 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 8030 | |
| 8031 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 8032 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8033 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8034 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8035 | /* Force a switch to readonly even array |
| 8036 | * appears to still be in use. Hence |
| 8037 | * the '100'. |
| 8038 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 8039 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8040 | mddev_unlock(mddev); |
| 8041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8042 | /* |
| 8043 | * certain more exotic SCSI devices are known to be |
| 8044 | * volatile wrt too early system reboots. While the |
| 8045 | * right place to handle this issue is the given |
| 8046 | * driver, we do want to have a safe RAID driver ... |
| 8047 | */ |
| 8048 | mdelay(1000*1); |
| 8049 | } |
| 8050 | return NOTIFY_DONE; |
| 8051 | } |
| 8052 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8053 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8054 | .notifier_call = md_notify_reboot, |
| 8055 | .next = NULL, |
| 8056 | .priority = INT_MAX, /* before any real devices */ |
| 8057 | }; |
| 8058 | |
| 8059 | static void md_geninit(void) |
| 8060 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8061 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 8062 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8063 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8064 | } |
| 8065 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8066 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8067 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8068 | int ret = -ENOMEM; |
| 8069 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8070 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8071 | if (!md_wq) |
| 8072 | goto err_wq; |
| 8073 | |
| 8074 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8075 | if (!md_misc_wq) |
| 8076 | goto err_misc_wq; |
| 8077 | |
| 8078 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8079 | goto err_md; |
| 8080 | |
| 8081 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8082 | goto err_mdp; |
| 8083 | mdp_major = ret; |
| 8084 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8085 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8086 | md_probe, NULL, NULL); |
| 8087 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8088 | md_probe, NULL, NULL); |
| 8089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8090 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8091 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8092 | |
| 8093 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8094 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8095 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8096 | err_mdp: |
| 8097 | unregister_blkdev(MD_MAJOR, "md"); |
| 8098 | err_md: |
| 8099 | destroy_workqueue(md_misc_wq); |
| 8100 | err_misc_wq: |
| 8101 | destroy_workqueue(md_wq); |
| 8102 | err_wq: |
| 8103 | return ret; |
| 8104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8105 | |
| 8106 | #ifndef MODULE |
| 8107 | |
| 8108 | /* |
| 8109 | * Searches all registered partitions for autorun RAID arrays |
| 8110 | * at boot time. |
| 8111 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8112 | |
| 8113 | static LIST_HEAD(all_detected_devices); |
| 8114 | struct detected_devices_node { |
| 8115 | struct list_head list; |
| 8116 | dev_t dev; |
| 8117 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8118 | |
| 8119 | void md_autodetect_dev(dev_t dev) |
| 8120 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8121 | struct detected_devices_node *node_detected_dev; |
| 8122 | |
| 8123 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8124 | if (node_detected_dev) { |
| 8125 | node_detected_dev->dev = dev; |
| 8126 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8127 | } else { |
| 8128 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8129 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8131 | } |
| 8132 | |
| 8133 | |
| 8134 | static void autostart_arrays(int part) |
| 8135 | { |
| 8136 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8137 | struct detected_devices_node *node_detected_dev; |
| 8138 | dev_t dev; |
| 8139 | int i_scanned, i_passed; |
| 8140 | |
| 8141 | i_scanned = 0; |
| 8142 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8143 | |
| 8144 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8145 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8146 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8147 | i_scanned++; |
| 8148 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8149 | struct detected_devices_node, list); |
| 8150 | list_del(&node_detected_dev->list); |
| 8151 | dev = node_detected_dev->dev; |
| 8152 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8153 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8154 | if (IS_ERR(rdev)) |
| 8155 | continue; |
| 8156 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8157 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8158 | MD_BUG(); |
| 8159 | continue; |
| 8160 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8161 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8162 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8163 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8164 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8165 | |
| 8166 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8167 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8168 | |
| 8169 | autorun_devices(part); |
| 8170 | } |
| 8171 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8172 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8173 | |
| 8174 | static __exit void md_exit(void) |
| 8175 | { |
| 8176 | mddev_t *mddev; |
| 8177 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8178 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8179 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8180 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8181 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8182 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8183 | unregister_blkdev(mdp_major, "mdp"); |
| 8184 | unregister_reboot_notifier(&md_notifier); |
| 8185 | unregister_sysctl_table(raid_table_header); |
| 8186 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8187 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8188 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8189 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8190 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8191 | destroy_workqueue(md_misc_wq); |
| 8192 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8193 | } |
| 8194 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8195 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8196 | module_exit(md_exit) |
| 8197 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8198 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8199 | { |
| 8200 | return sprintf(buffer, "%d", start_readonly); |
| 8201 | } |
| 8202 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8203 | { |
| 8204 | char *e; |
| 8205 | int num = simple_strtoul(val, &e, 10); |
| 8206 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8207 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8208 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8209 | } |
| 8210 | return -EINVAL; |
| 8211 | } |
| 8212 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8213 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8214 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8215 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8216 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8218 | EXPORT_SYMBOL(register_md_personality); |
| 8219 | EXPORT_SYMBOL(unregister_md_personality); |
| 8220 | EXPORT_SYMBOL(md_error); |
| 8221 | EXPORT_SYMBOL(md_done_sync); |
| 8222 | EXPORT_SYMBOL(md_write_start); |
| 8223 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8224 | EXPORT_SYMBOL(md_register_thread); |
| 8225 | EXPORT_SYMBOL(md_unregister_thread); |
| 8226 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8227 | EXPORT_SYMBOL(md_check_recovery); |
| 8228 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8229 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8230 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8231 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |