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 | 65a06f06 | 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 | 9f2f383 | 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 | 65a06f06 | 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 | 65a06f06 | 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 | 0261cd9f | 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 | 65a06f06 | 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 | 0261cd9f | 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 | 0261cd9f | 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 | 0261cd9f | 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 | 0261cd9f | 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 | 65a06f06 | 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 | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1577 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1580 | sb->level != refsb->level || |
| 1581 | sb->layout != refsb->layout || |
| 1582 | sb->chunksize != refsb->chunksize) { |
| 1583 | printk(KERN_WARNING "md: %s has strangely different" |
| 1584 | " superblock to %s\n", |
| 1585 | bdevname(rdev->bdev,b), |
| 1586 | bdevname(refdev->bdev,b2)); |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | ev1 = le64_to_cpu(sb->events); |
| 1590 | ev2 = le64_to_cpu(refsb->events); |
| 1591 | |
| 1592 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1593 | ret = 1; |
| 1594 | else |
| 1595 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | } |
NeilBrown | 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 | 65a06f06 | 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 | 65a06f06 | 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 | 65a06f06 | 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 | 7dd5e7c3 | 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 | 7dd5e7c3 | 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 | 7dd5e7c3 | 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 | 7dd5e7c3 | 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 | 65a06f06 | 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 | 65a06f06 | 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 | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2344 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2345 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2346 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2347 | if (rdev->badblocks.changed) { |
| 2348 | md_ack_all_badblocks(&rdev->badblocks); |
| 2349 | md_error(mddev, rdev); |
| 2350 | } |
| 2351 | clear_bit(Blocked, &rdev->flags); |
| 2352 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2353 | wake_up(&rdev->blocked_wait); |
| 2354 | } |
| 2355 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2356 | wake_up(&mddev->sb_wait); |
| 2357 | return; |
| 2358 | } |
| 2359 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2360 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2361 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2362 | mddev->utime = get_seconds(); |
| 2363 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2364 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2365 | force_change = 1; |
| 2366 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2367 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2368 | * though if events isn't the right even/odd, we will have to do |
| 2369 | * spares after all |
| 2370 | */ |
| 2371 | nospares = 1; |
| 2372 | if (force_change) |
| 2373 | nospares = 0; |
| 2374 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2375 | /* If the array is degraded, then skipping spares is both |
| 2376 | * dangerous and fairly pointless. |
| 2377 | * Dangerous because a device that was removed from the array |
| 2378 | * might have a event_count that still looks up-to-date, |
| 2379 | * so it can be re-added without a resync. |
| 2380 | * Pointless because if there are any spares to skip, |
| 2381 | * then a recovery will happen and soon that array won't |
| 2382 | * be degraded any more and the spare can go back to sleep then. |
| 2383 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2384 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2385 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2386 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2387 | |
| 2388 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2389 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2390 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2391 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2392 | && mddev->can_decrease_events |
| 2393 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2394 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2395 | mddev->can_decrease_events = 0; |
| 2396 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2397 | /* otherwise we have to go forward and ... */ |
| 2398 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2399 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | |
| 2402 | if (!mddev->events) { |
| 2403 | /* |
| 2404 | * oops, this 64-bit counter should never wrap. |
| 2405 | * Either we are in around ~1 trillion A.C., assuming |
| 2406 | * 1 reboot per second, or we have a bug: |
| 2407 | */ |
| 2408 | MD_BUG(); |
| 2409 | mddev->events --; |
| 2410 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2411 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2412 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2413 | if (rdev->badblocks.changed) |
| 2414 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2415 | if (test_bit(Faulty, &rdev->flags)) |
| 2416 | set_bit(FaultRecorded, &rdev->flags); |
| 2417 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2418 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2419 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2420 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | |
| 2422 | dprintk(KERN_INFO |
| 2423 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2424 | mdname(mddev),mddev->in_sync); |
| 2425 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2426 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2427 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | char b[BDEVNAME_SIZE]; |
| 2429 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2430 | if (rdev->sb_loaded != 1) |
| 2431 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2432 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | dprintk("(skipping faulty "); |
| 2434 | |
| 2435 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2436 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2437 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2438 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2439 | rdev->sb_page); |
| 2440 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2441 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2442 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2443 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2444 | if (rdev->badblocks.size) { |
| 2445 | md_super_write(mddev, rdev, |
| 2446 | rdev->badblocks.sector, |
| 2447 | rdev->badblocks.size << 9, |
| 2448 | rdev->bb_page); |
| 2449 | rdev->badblocks.size = 0; |
| 2450 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | } else |
| 2453 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2454 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | /* only need to write one superblock... */ |
| 2456 | break; |
| 2457 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2458 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2459 | /* 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] | 2460 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2461 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2462 | if (mddev->in_sync != sync_req || |
| 2463 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2464 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2465 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2466 | goto repeat; |
| 2467 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2468 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2469 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2470 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2471 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2472 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2473 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2474 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2475 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2476 | clear_bit(Blocked, &rdev->flags); |
| 2477 | |
| 2478 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2479 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2480 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2481 | wake_up(&rdev->blocked_wait); |
| 2482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2485 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2486 | * We want to accept with case. For this we use cmd_match. |
| 2487 | */ |
| 2488 | static int cmd_match(const char *cmd, const char *str) |
| 2489 | { |
| 2490 | /* See if cmd, written into a sysfs file, matches |
| 2491 | * str. They must either be the same, or cmd can |
| 2492 | * have a trailing newline |
| 2493 | */ |
| 2494 | while (*cmd && *str && *cmd == *str) { |
| 2495 | cmd++; |
| 2496 | str++; |
| 2497 | } |
| 2498 | if (*cmd == '\n') |
| 2499 | cmd++; |
| 2500 | if (*str || *cmd) |
| 2501 | return 0; |
| 2502 | return 1; |
| 2503 | } |
| 2504 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2505 | struct rdev_sysfs_entry { |
| 2506 | struct attribute attr; |
| 2507 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2508 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2509 | }; |
| 2510 | |
| 2511 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2512 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2513 | { |
| 2514 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2515 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2516 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2517 | if (test_bit(Faulty, &rdev->flags) || |
| 2518 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2519 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2520 | sep = ","; |
| 2521 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2522 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2523 | len += sprintf(page+len, "%sin_sync",sep); |
| 2524 | sep = ","; |
| 2525 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2526 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2527 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2528 | sep = ","; |
| 2529 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2530 | if (test_bit(Blocked, &rdev->flags) || |
| 2531 | rdev->badblocks.unacked_exist) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2532 | len += sprintf(page+len, "%sblocked", sep); |
| 2533 | sep = ","; |
| 2534 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2535 | if (!test_bit(Faulty, &rdev->flags) && |
| 2536 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2537 | len += sprintf(page+len, "%sspare", sep); |
| 2538 | sep = ","; |
| 2539 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2540 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2541 | len += sprintf(page+len, "%swrite_error", sep); |
| 2542 | sep = ","; |
| 2543 | } |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2544 | return len+sprintf(page+len, "\n"); |
| 2545 | } |
| 2546 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2547 | static ssize_t |
| 2548 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2549 | { |
| 2550 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2551 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2552 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2553 | * writemostly - sets write_mostly |
| 2554 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2555 | * blocked - sets the Blocked flags |
| 2556 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2557 | * insync - sets Insync providing device isn't active |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2558 | * write_error - sets WriteErrorSeen |
| 2559 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2560 | */ |
| 2561 | int err = -EINVAL; |
| 2562 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2563 | md_error(rdev->mddev, rdev); |
| 2564 | err = 0; |
| 2565 | } else if (cmd_match(buf, "remove")) { |
| 2566 | if (rdev->raid_disk >= 0) |
| 2567 | err = -EBUSY; |
| 2568 | else { |
| 2569 | mddev_t *mddev = rdev->mddev; |
| 2570 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2571 | if (mddev->pers) |
| 2572 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2573 | md_new_event(mddev); |
| 2574 | err = 0; |
| 2575 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2576 | } else if (cmd_match(buf, "writemostly")) { |
| 2577 | set_bit(WriteMostly, &rdev->flags); |
| 2578 | err = 0; |
| 2579 | } else if (cmd_match(buf, "-writemostly")) { |
| 2580 | clear_bit(WriteMostly, &rdev->flags); |
| 2581 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2582 | } else if (cmd_match(buf, "blocked")) { |
| 2583 | set_bit(Blocked, &rdev->flags); |
| 2584 | err = 0; |
| 2585 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2586 | if (!test_bit(Faulty, &rdev->flags) && |
| 2587 | test_bit(BlockedBadBlocks, &rdev->flags)) { |
| 2588 | /* metadata handler doesn't understand badblocks, |
| 2589 | * so we need to fail the device |
| 2590 | */ |
| 2591 | md_error(rdev->mddev, rdev); |
| 2592 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2593 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2594 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2595 | wake_up(&rdev->blocked_wait); |
| 2596 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2597 | md_wakeup_thread(rdev->mddev->thread); |
| 2598 | |
| 2599 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2600 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2601 | set_bit(In_sync, &rdev->flags); |
| 2602 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2603 | } else if (cmd_match(buf, "write_error")) { |
| 2604 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2605 | err = 0; |
| 2606 | } else if (cmd_match(buf, "-write_error")) { |
| 2607 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2608 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2609 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2610 | if (!err) |
| 2611 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2612 | return err ? err : len; |
| 2613 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2614 | static struct rdev_sysfs_entry rdev_state = |
| 2615 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2616 | |
| 2617 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2618 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2619 | { |
| 2620 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2621 | } |
| 2622 | |
| 2623 | static ssize_t |
| 2624 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2625 | { |
| 2626 | char *e; |
| 2627 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2628 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2629 | atomic_set(&rdev->corrected_errors, n); |
| 2630 | return len; |
| 2631 | } |
| 2632 | return -EINVAL; |
| 2633 | } |
| 2634 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2635 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2636 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2637 | static ssize_t |
| 2638 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2639 | { |
| 2640 | if (rdev->raid_disk < 0) |
| 2641 | return sprintf(page, "none\n"); |
| 2642 | else |
| 2643 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2644 | } |
| 2645 | |
| 2646 | static ssize_t |
| 2647 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2648 | { |
| 2649 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2650 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2651 | int slot = simple_strtoul(buf, &e, 10); |
| 2652 | if (strncmp(buf, "none", 4)==0) |
| 2653 | slot = -1; |
| 2654 | else if (e==buf || (*e && *e!= '\n')) |
| 2655 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2656 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2657 | /* Setting 'slot' on an active array requires also |
| 2658 | * updating the 'rd%d' link, and communicating |
| 2659 | * with the personality with ->hot_*_disk. |
| 2660 | * For now we only support removing |
| 2661 | * failed/spare devices. This normally happens automatically, |
| 2662 | * but not when the metadata is externally managed. |
| 2663 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2664 | if (rdev->raid_disk == -1) |
| 2665 | return -EEXIST; |
| 2666 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2667 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2668 | return -EINVAL; |
| 2669 | err = rdev->mddev->pers-> |
| 2670 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2671 | if (err) |
| 2672 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2673 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2674 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2675 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2676 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2677 | } else if (rdev->mddev->pers) { |
| 2678 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2679 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2680 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2681 | */ |
| 2682 | |
| 2683 | if (rdev->raid_disk != -1) |
| 2684 | return -EBUSY; |
| 2685 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2686 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2687 | return -EBUSY; |
| 2688 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2689 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2690 | return -EINVAL; |
| 2691 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2692 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2693 | if (rdev2->raid_disk == slot) |
| 2694 | return -EEXIST; |
| 2695 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2696 | if (slot >= rdev->mddev->raid_disks && |
| 2697 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2698 | return -ENOSPC; |
| 2699 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2700 | rdev->raid_disk = slot; |
| 2701 | if (test_bit(In_sync, &rdev->flags)) |
| 2702 | rdev->saved_raid_disk = slot; |
| 2703 | else |
| 2704 | rdev->saved_raid_disk = -1; |
| 2705 | err = rdev->mddev->pers-> |
| 2706 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2707 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2708 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2709 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2710 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2711 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2712 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2713 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2714 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2715 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2716 | if (slot >= rdev->mddev->raid_disks && |
| 2717 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2718 | return -ENOSPC; |
| 2719 | rdev->raid_disk = slot; |
| 2720 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2721 | clear_bit(Faulty, &rdev->flags); |
| 2722 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2723 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2724 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2725 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2726 | return len; |
| 2727 | } |
| 2728 | |
| 2729 | |
| 2730 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2731 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2732 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2733 | static ssize_t |
| 2734 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2735 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2736 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | static ssize_t |
| 2740 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2741 | { |
| 2742 | char *e; |
| 2743 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2744 | if (e==buf || (*e && *e != '\n')) |
| 2745 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2746 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2747 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2748 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2749 | /* Must set offset before size, so overlap checks |
| 2750 | * can be sane */ |
| 2751 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2752 | rdev->data_offset = offset; |
| 2753 | return len; |
| 2754 | } |
| 2755 | |
| 2756 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2757 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2758 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2759 | static ssize_t |
| 2760 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2761 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2762 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2763 | } |
| 2764 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2765 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2766 | { |
| 2767 | /* check if two start/length pairs overlap */ |
| 2768 | if (s1+l1 <= s2) |
| 2769 | return 0; |
| 2770 | if (s2+l2 <= s1) |
| 2771 | return 0; |
| 2772 | return 1; |
| 2773 | } |
| 2774 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2775 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2776 | { |
| 2777 | unsigned long long blocks; |
| 2778 | sector_t new; |
| 2779 | |
| 2780 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2781 | return -EINVAL; |
| 2782 | |
| 2783 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2784 | return -EINVAL; /* sector conversion overflow */ |
| 2785 | |
| 2786 | new = blocks * 2; |
| 2787 | if (new != blocks * 2) |
| 2788 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2789 | |
| 2790 | *sectors = new; |
| 2791 | return 0; |
| 2792 | } |
| 2793 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2794 | static ssize_t |
| 2795 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2796 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2797 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2798 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2799 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2800 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2801 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2802 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2803 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2804 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2805 | sectors = super_types[my_mddev->major_version]. |
| 2806 | rdev_size_change(rdev, sectors); |
| 2807 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2808 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2809 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2810 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2811 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2812 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2813 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2814 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2815 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2816 | rdev->sectors = sectors; |
| 2817 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2818 | /* need to check that all other rdevs with the same ->bdev |
| 2819 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2820 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2821 | * we have to change it back, we will have the lock again. |
| 2822 | */ |
| 2823 | mddev_t *mddev; |
| 2824 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2825 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2826 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2827 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2828 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2829 | mdk_rdev_t *rdev2; |
| 2830 | |
| 2831 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2832 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2833 | if (rdev->bdev == rdev2->bdev && |
| 2834 | rdev != rdev2 && |
| 2835 | overlaps(rdev->data_offset, rdev->sectors, |
| 2836 | rdev2->data_offset, |
| 2837 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2838 | overlap = 1; |
| 2839 | break; |
| 2840 | } |
| 2841 | mddev_unlock(mddev); |
| 2842 | if (overlap) { |
| 2843 | mddev_put(mddev); |
| 2844 | break; |
| 2845 | } |
| 2846 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2847 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2848 | if (overlap) { |
| 2849 | /* Someone else could have slipped in a size |
| 2850 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2851 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2852 | * safe, and trust userspace not to race with |
| 2853 | * itself |
| 2854 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2855 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2856 | return -EBUSY; |
| 2857 | } |
| 2858 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2859 | return len; |
| 2860 | } |
| 2861 | |
| 2862 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2863 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2864 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2865 | |
| 2866 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2867 | { |
| 2868 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2869 | |
| 2870 | if (test_bit(In_sync, &rdev->flags) || |
| 2871 | recovery_start == MaxSector) |
| 2872 | return sprintf(page, "none\n"); |
| 2873 | |
| 2874 | return sprintf(page, "%llu\n", recovery_start); |
| 2875 | } |
| 2876 | |
| 2877 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2878 | { |
| 2879 | unsigned long long recovery_start; |
| 2880 | |
| 2881 | if (cmd_match(buf, "none")) |
| 2882 | recovery_start = MaxSector; |
| 2883 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2884 | return -EINVAL; |
| 2885 | |
| 2886 | if (rdev->mddev->pers && |
| 2887 | rdev->raid_disk >= 0) |
| 2888 | return -EBUSY; |
| 2889 | |
| 2890 | rdev->recovery_offset = recovery_start; |
| 2891 | if (recovery_start == MaxSector) |
| 2892 | set_bit(In_sync, &rdev->flags); |
| 2893 | else |
| 2894 | clear_bit(In_sync, &rdev->flags); |
| 2895 | return len; |
| 2896 | } |
| 2897 | |
| 2898 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2899 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2900 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2901 | |
| 2902 | static ssize_t |
| 2903 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2904 | static ssize_t |
| 2905 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2906 | |
| 2907 | static ssize_t bb_show(mdk_rdev_t *rdev, char *page) |
| 2908 | { |
| 2909 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2910 | } |
| 2911 | static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2912 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2913 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2914 | /* Maybe that ack was all we needed */ |
| 2915 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2916 | wake_up(&rdev->blocked_wait); |
| 2917 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2918 | } |
| 2919 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2920 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2921 | |
| 2922 | |
| 2923 | static ssize_t ubb_show(mdk_rdev_t *rdev, char *page) |
| 2924 | { |
| 2925 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2926 | } |
| 2927 | static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2928 | { |
| 2929 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2930 | } |
| 2931 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2932 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2933 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2934 | static struct attribute *rdev_default_attrs[] = { |
| 2935 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2936 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2937 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2938 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2939 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2940 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2941 | &rdev_bad_blocks.attr, |
| 2942 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2943 | NULL, |
| 2944 | }; |
| 2945 | static ssize_t |
| 2946 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2947 | { |
| 2948 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2949 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2950 | mddev_t *mddev = rdev->mddev; |
| 2951 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2952 | |
| 2953 | if (!entry->show) |
| 2954 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2955 | |
| 2956 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2957 | if (!rv) { |
| 2958 | if (rdev->mddev == NULL) |
| 2959 | rv = -EBUSY; |
| 2960 | else |
| 2961 | rv = entry->show(rdev, page); |
| 2962 | mddev_unlock(mddev); |
| 2963 | } |
| 2964 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | static ssize_t |
| 2968 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2969 | const char *page, size_t length) |
| 2970 | { |
| 2971 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2972 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2973 | ssize_t rv; |
| 2974 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2975 | |
| 2976 | if (!entry->store) |
| 2977 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2978 | if (!capable(CAP_SYS_ADMIN)) |
| 2979 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2980 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2981 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2982 | if (rdev->mddev == NULL) |
| 2983 | rv = -EBUSY; |
| 2984 | else |
| 2985 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2986 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2987 | } |
| 2988 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | static void rdev_free(struct kobject *ko) |
| 2992 | { |
| 2993 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2994 | kfree(rdev); |
| 2995 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2996 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2997 | .show = rdev_attr_show, |
| 2998 | .store = rdev_attr_store, |
| 2999 | }; |
| 3000 | static struct kobj_type rdev_ktype = { |
| 3001 | .release = rdev_free, |
| 3002 | .sysfs_ops = &rdev_sysfs_ops, |
| 3003 | .default_attrs = rdev_default_attrs, |
| 3004 | }; |
| 3005 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3006 | int md_rdev_init(mdk_rdev_t *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3007 | { |
| 3008 | rdev->desc_nr = -1; |
| 3009 | rdev->saved_raid_disk = -1; |
| 3010 | rdev->raid_disk = -1; |
| 3011 | rdev->flags = 0; |
| 3012 | rdev->data_offset = 0; |
| 3013 | rdev->sb_events = 0; |
| 3014 | rdev->last_read_error.tv_sec = 0; |
| 3015 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3016 | rdev->sb_loaded = 0; |
| 3017 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3018 | atomic_set(&rdev->nr_pending, 0); |
| 3019 | atomic_set(&rdev->read_errors, 0); |
| 3020 | atomic_set(&rdev->corrected_errors, 0); |
| 3021 | |
| 3022 | INIT_LIST_HEAD(&rdev->same_set); |
| 3023 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3024 | |
| 3025 | /* Add space to store bad block list. |
| 3026 | * This reserves the space even on arrays where it cannot |
| 3027 | * be used - I wonder if that matters |
| 3028 | */ |
| 3029 | rdev->badblocks.count = 0; |
| 3030 | rdev->badblocks.shift = 0; |
| 3031 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3032 | seqlock_init(&rdev->badblocks.lock); |
| 3033 | if (rdev->badblocks.page == NULL) |
| 3034 | return -ENOMEM; |
| 3035 | |
| 3036 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3037 | } |
| 3038 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | /* |
| 3040 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3041 | * |
| 3042 | * mark the device faulty if: |
| 3043 | * |
| 3044 | * - the device is nonexistent (zero size) |
| 3045 | * - the device has no valid superblock |
| 3046 | * |
| 3047 | * a faulty rdev _never_ has rdev->sb set. |
| 3048 | */ |
| 3049 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 3050 | { |
| 3051 | char b[BDEVNAME_SIZE]; |
| 3052 | int err; |
| 3053 | mdk_rdev_t *rdev; |
| 3054 | sector_t size; |
| 3055 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3056 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | if (!rdev) { |
| 3058 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3059 | return ERR_PTR(-ENOMEM); |
| 3060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3062 | err = md_rdev_init(rdev); |
| 3063 | if (err) |
| 3064 | goto abort_free; |
| 3065 | err = alloc_disk_sb(rdev); |
| 3066 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | goto abort_free; |
| 3068 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3069 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | if (err) |
| 3071 | goto abort_free; |
| 3072 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3073 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3074 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3075 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | if (!size) { |
| 3077 | printk(KERN_WARNING |
| 3078 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3079 | bdevname(rdev->bdev,b)); |
| 3080 | err = -EINVAL; |
| 3081 | goto abort_free; |
| 3082 | } |
| 3083 | |
| 3084 | if (super_format >= 0) { |
| 3085 | err = super_types[super_format]. |
| 3086 | load_super(rdev, NULL, super_minor); |
| 3087 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3088 | printk(KERN_WARNING |
| 3089 | "md: %s does not have a valid v%d.%d " |
| 3090 | "superblock, not importing!\n", |
| 3091 | bdevname(rdev->bdev,b), |
| 3092 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | goto abort_free; |
| 3094 | } |
| 3095 | if (err < 0) { |
| 3096 | printk(KERN_WARNING |
| 3097 | "md: could not read %s's sb, not importing!\n", |
| 3098 | bdevname(rdev->bdev,b)); |
| 3099 | goto abort_free; |
| 3100 | } |
| 3101 | } |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3102 | if (super_format == -1) |
| 3103 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3104 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | return rdev; |
| 3107 | |
| 3108 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3109 | if (rdev->bdev) |
| 3110 | unlock_rdev(rdev); |
| 3111 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3112 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | kfree(rdev); |
| 3114 | return ERR_PTR(err); |
| 3115 | } |
| 3116 | |
| 3117 | /* |
| 3118 | * Check a full RAID array for plausibility |
| 3119 | */ |
| 3120 | |
| 3121 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3122 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | { |
| 3124 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3125 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | char b[BDEVNAME_SIZE]; |
| 3127 | |
| 3128 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3129 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | switch (super_types[mddev->major_version]. |
| 3131 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3132 | case 1: |
| 3133 | freshest = rdev; |
| 3134 | break; |
| 3135 | case 0: |
| 3136 | break; |
| 3137 | default: |
| 3138 | printk( KERN_ERR \ |
| 3139 | "md: fatal superblock inconsistency in %s" |
| 3140 | " -- removing from array\n", |
| 3141 | bdevname(rdev->bdev,b)); |
| 3142 | kick_rdev_from_array(rdev); |
| 3143 | } |
| 3144 | |
| 3145 | |
| 3146 | super_types[mddev->major_version]. |
| 3147 | validate_super(mddev, freshest); |
| 3148 | |
| 3149 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3150 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3151 | if (mddev->max_disks && |
| 3152 | (rdev->desc_nr >= mddev->max_disks || |
| 3153 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3154 | printk(KERN_WARNING |
| 3155 | "md: %s: %s: only %d devices permitted\n", |
| 3156 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3157 | mddev->max_disks); |
| 3158 | kick_rdev_from_array(rdev); |
| 3159 | continue; |
| 3160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | if (rdev != freshest) |
| 3162 | if (super_types[mddev->major_version]. |
| 3163 | validate_super(mddev, rdev)) { |
| 3164 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3165 | " from array!\n", |
| 3166 | bdevname(rdev->bdev,b)); |
| 3167 | kick_rdev_from_array(rdev); |
| 3168 | continue; |
| 3169 | } |
| 3170 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3171 | rdev->desc_nr = i++; |
| 3172 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3173 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3174 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3175 | rdev->raid_disk = -1; |
| 3176 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3177 | } |
| 3178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3181 | /* Read a fixed-point number. |
| 3182 | * Numbers in sysfs attributes should be in "standard" units where |
| 3183 | * possible, so time should be in seconds. |
| 3184 | * However we internally use a a much smaller unit such as |
| 3185 | * milliseconds or jiffies. |
| 3186 | * This function takes a decimal number with a possible fractional |
| 3187 | * component, and produces an integer which is the result of |
| 3188 | * multiplying that number by 10^'scale'. |
| 3189 | * all without any floating-point arithmetic. |
| 3190 | */ |
| 3191 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3192 | { |
| 3193 | unsigned long result = 0; |
| 3194 | long decimals = -1; |
| 3195 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3196 | if (*cp == '.') |
| 3197 | decimals = 0; |
| 3198 | else if (decimals < scale) { |
| 3199 | unsigned int value; |
| 3200 | value = *cp - '0'; |
| 3201 | result = result * 10 + value; |
| 3202 | if (decimals >= 0) |
| 3203 | decimals++; |
| 3204 | } |
| 3205 | cp++; |
| 3206 | } |
| 3207 | if (*cp == '\n') |
| 3208 | cp++; |
| 3209 | if (*cp) |
| 3210 | return -EINVAL; |
| 3211 | if (decimals < 0) |
| 3212 | decimals = 0; |
| 3213 | while (decimals < scale) { |
| 3214 | result *= 10; |
| 3215 | decimals ++; |
| 3216 | } |
| 3217 | *res = result; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
| 3221 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3222 | static void md_safemode_timeout(unsigned long data); |
| 3223 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3224 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3225 | safe_delay_show(mddev_t *mddev, char *page) |
| 3226 | { |
| 3227 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3228 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3229 | } |
| 3230 | static ssize_t |
| 3231 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 3232 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3233 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3234 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3235 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3236 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3237 | if (msec == 0) |
| 3238 | mddev->safemode_delay = 0; |
| 3239 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3240 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3241 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3242 | if (mddev->safemode_delay == 0) |
| 3243 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3244 | if (mddev->safemode_delay < old_delay) |
| 3245 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3246 | } |
| 3247 | return len; |
| 3248 | } |
| 3249 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3250 | __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] | 3251 | |
| 3252 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3253 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3254 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3255 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3256 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3257 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3258 | else if (mddev->clevel[0]) |
| 3259 | return sprintf(page, "%s\n", mddev->clevel); |
| 3260 | else if (mddev->level != LEVEL_NONE) |
| 3261 | return sprintf(page, "%d\n", mddev->level); |
| 3262 | else |
| 3263 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3266 | static ssize_t |
| 3267 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3268 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3269 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3270 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3271 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3272 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3273 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3274 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3275 | |
| 3276 | if (mddev->pers == NULL) { |
| 3277 | if (len == 0) |
| 3278 | return 0; |
| 3279 | if (len >= sizeof(mddev->clevel)) |
| 3280 | return -ENOSPC; |
| 3281 | strncpy(mddev->clevel, buf, len); |
| 3282 | if (mddev->clevel[len-1] == '\n') |
| 3283 | len--; |
| 3284 | mddev->clevel[len] = 0; |
| 3285 | mddev->level = LEVEL_NONE; |
| 3286 | return rv; |
| 3287 | } |
| 3288 | |
| 3289 | /* request to change the personality. Need to ensure: |
| 3290 | * - array is not engaged in resync/recovery/reshape |
| 3291 | * - old personality can be suspended |
| 3292 | * - new personality will access other array. |
| 3293 | */ |
| 3294 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3295 | if (mddev->sync_thread || |
| 3296 | mddev->reshape_position != MaxSector || |
| 3297 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3298 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3299 | |
| 3300 | if (!mddev->pers->quiesce) { |
| 3301 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3302 | mdname(mddev), mddev->pers->name); |
| 3303 | return -EINVAL; |
| 3304 | } |
| 3305 | |
| 3306 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3307 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3308 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3309 | strncpy(clevel, buf, len); |
| 3310 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3311 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3312 | clevel[len] = 0; |
| 3313 | if (strict_strtol(clevel, 10, &level)) |
| 3314 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3315 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3316 | if (request_module("md-%s", clevel) != 0) |
| 3317 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3318 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3319 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3320 | if (!pers || !try_module_get(pers->owner)) { |
| 3321 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3322 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3323 | return -EINVAL; |
| 3324 | } |
| 3325 | spin_unlock(&pers_lock); |
| 3326 | |
| 3327 | if (pers == mddev->pers) { |
| 3328 | /* Nothing to do! */ |
| 3329 | module_put(pers->owner); |
| 3330 | return rv; |
| 3331 | } |
| 3332 | if (!pers->takeover) { |
| 3333 | module_put(pers->owner); |
| 3334 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3335 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3336 | return -EINVAL; |
| 3337 | } |
| 3338 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3339 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3340 | rdev->new_raid_disk = rdev->raid_disk; |
| 3341 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3342 | /* ->takeover must set new_* and/or delta_disks |
| 3343 | * if it succeeds, and may set them when it fails. |
| 3344 | */ |
| 3345 | priv = pers->takeover(mddev); |
| 3346 | if (IS_ERR(priv)) { |
| 3347 | mddev->new_level = mddev->level; |
| 3348 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3349 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3350 | mddev->raid_disks -= mddev->delta_disks; |
| 3351 | mddev->delta_disks = 0; |
| 3352 | module_put(pers->owner); |
| 3353 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3354 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3355 | return PTR_ERR(priv); |
| 3356 | } |
| 3357 | |
| 3358 | /* Looks like we have a winner */ |
| 3359 | mddev_suspend(mddev); |
| 3360 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3361 | |
| 3362 | if (mddev->pers->sync_request == NULL && |
| 3363 | pers->sync_request != NULL) { |
| 3364 | /* need to add the md_redundancy_group */ |
| 3365 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3366 | printk(KERN_WARNING |
| 3367 | "md: cannot register extra attributes for %s\n", |
| 3368 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3369 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3370 | } |
| 3371 | if (mddev->pers->sync_request != NULL && |
| 3372 | pers->sync_request == NULL) { |
| 3373 | /* need to remove the md_redundancy_group */ |
| 3374 | if (mddev->to_remove == NULL) |
| 3375 | mddev->to_remove = &md_redundancy_group; |
| 3376 | } |
| 3377 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3378 | if (mddev->pers->sync_request == NULL && |
| 3379 | mddev->external) { |
| 3380 | /* We are converting from a no-redundancy array |
| 3381 | * to a redundancy array and metadata is managed |
| 3382 | * externally so we need to be sure that writes |
| 3383 | * won't block due to a need to transition |
| 3384 | * clean->dirty |
| 3385 | * until external management is started. |
| 3386 | */ |
| 3387 | mddev->in_sync = 0; |
| 3388 | mddev->safemode_delay = 0; |
| 3389 | mddev->safemode = 0; |
| 3390 | } |
| 3391 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3392 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3393 | if (rdev->raid_disk < 0) |
| 3394 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3395 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3396 | rdev->new_raid_disk = -1; |
| 3397 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3398 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3399 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3400 | } |
| 3401 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3402 | if (rdev->raid_disk < 0) |
| 3403 | continue; |
| 3404 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3405 | continue; |
| 3406 | rdev->raid_disk = rdev->new_raid_disk; |
| 3407 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3408 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3409 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3410 | if (sysfs_link_rdev(mddev, rdev)) |
| 3411 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3412 | " for %s after level change\n", |
| 3413 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3414 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3418 | mddev->pers = pers; |
| 3419 | mddev->private = priv; |
| 3420 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3421 | mddev->level = mddev->new_level; |
| 3422 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3423 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3424 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3425 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3426 | if (mddev->pers->sync_request == NULL) { |
| 3427 | /* this is now an array without redundancy, so |
| 3428 | * it must always be in_sync |
| 3429 | */ |
| 3430 | mddev->in_sync = 1; |
| 3431 | del_timer_sync(&mddev->safemode_timer); |
| 3432 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3433 | pers->run(mddev); |
| 3434 | mddev_resume(mddev); |
| 3435 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3436 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3437 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3438 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3439 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3440 | return rv; |
| 3441 | } |
| 3442 | |
| 3443 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3444 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3445 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3446 | |
| 3447 | static ssize_t |
| 3448 | layout_show(mddev_t *mddev, char *page) |
| 3449 | { |
| 3450 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3451 | if (mddev->reshape_position != MaxSector && |
| 3452 | mddev->layout != mddev->new_layout) |
| 3453 | return sprintf(page, "%d (%d)\n", |
| 3454 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3455 | return sprintf(page, "%d\n", mddev->layout); |
| 3456 | } |
| 3457 | |
| 3458 | static ssize_t |
| 3459 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3460 | { |
| 3461 | char *e; |
| 3462 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3463 | |
| 3464 | if (!*buf || (*e && *e != '\n')) |
| 3465 | return -EINVAL; |
| 3466 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3467 | if (mddev->pers) { |
| 3468 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3469 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3470 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3471 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3472 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3473 | if (err) { |
| 3474 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3475 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3476 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3477 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3478 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3479 | if (mddev->reshape_position == MaxSector) |
| 3480 | mddev->layout = n; |
| 3481 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3482 | return len; |
| 3483 | } |
| 3484 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3485 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3486 | |
| 3487 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3488 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3489 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3490 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3491 | if (mddev->raid_disks == 0) |
| 3492 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3493 | if (mddev->reshape_position != MaxSector && |
| 3494 | mddev->delta_disks != 0) |
| 3495 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3496 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3497 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3498 | } |
| 3499 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3500 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3501 | |
| 3502 | static ssize_t |
| 3503 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3504 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3505 | char *e; |
| 3506 | int rv = 0; |
| 3507 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3508 | |
| 3509 | if (!*buf || (*e && *e != '\n')) |
| 3510 | return -EINVAL; |
| 3511 | |
| 3512 | if (mddev->pers) |
| 3513 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3514 | else if (mddev->reshape_position != MaxSector) { |
| 3515 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3516 | mddev->delta_disks = n - olddisks; |
| 3517 | mddev->raid_disks = n; |
| 3518 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3519 | mddev->raid_disks = n; |
| 3520 | return rv ? rv : len; |
| 3521 | } |
| 3522 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3523 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3524 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3525 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3526 | chunk_size_show(mddev_t *mddev, char *page) |
| 3527 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3528 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3529 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3530 | return sprintf(page, "%d (%d)\n", |
| 3531 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3532 | mddev->chunk_sectors << 9); |
| 3533 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | static ssize_t |
| 3537 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3538 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3539 | char *e; |
| 3540 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3541 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3542 | if (!*buf || (*e && *e != '\n')) |
| 3543 | return -EINVAL; |
| 3544 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3545 | if (mddev->pers) { |
| 3546 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3547 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3548 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3549 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3550 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3551 | if (err) { |
| 3552 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3553 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3554 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3555 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3556 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3557 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3558 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3559 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3560 | return len; |
| 3561 | } |
| 3562 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3563 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3564 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3565 | static ssize_t |
| 3566 | resync_start_show(mddev_t *mddev, char *page) |
| 3567 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3568 | if (mddev->recovery_cp == MaxSector) |
| 3569 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3570 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3571 | } |
| 3572 | |
| 3573 | static ssize_t |
| 3574 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3575 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3576 | char *e; |
| 3577 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3578 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3579 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3580 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3581 | if (cmd_match(buf, "none")) |
| 3582 | n = MaxSector; |
| 3583 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3584 | return -EINVAL; |
| 3585 | |
| 3586 | mddev->recovery_cp = n; |
| 3587 | return len; |
| 3588 | } |
| 3589 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3590 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3591 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3592 | /* |
| 3593 | * The array state can be: |
| 3594 | * |
| 3595 | * clear |
| 3596 | * No devices, no size, no level |
| 3597 | * Equivalent to STOP_ARRAY ioctl |
| 3598 | * inactive |
| 3599 | * May have some settings, but array is not active |
| 3600 | * all IO results in error |
| 3601 | * When written, doesn't tear down array, but just stops it |
| 3602 | * suspended (not supported yet) |
| 3603 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3604 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3605 | * readonly |
| 3606 | * no resync can happen. no superblocks get written. |
| 3607 | * write requests fail |
| 3608 | * read-auto |
| 3609 | * like readonly, but behaves like 'clean' on a write request. |
| 3610 | * |
| 3611 | * clean - no pending writes, but otherwise active. |
| 3612 | * When written to inactive array, starts without resync |
| 3613 | * If a write request arrives then |
| 3614 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3615 | * if not known, block and switch to write-pending |
| 3616 | * If written to an active array that has pending writes, then fails. |
| 3617 | * active |
| 3618 | * fully active: IO and resync can be happening. |
| 3619 | * When written to inactive array, starts with resync |
| 3620 | * |
| 3621 | * write-pending |
| 3622 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3623 | * |
| 3624 | * active-idle |
| 3625 | * like active, but no writes have been seen for a while (100msec). |
| 3626 | * |
| 3627 | */ |
| 3628 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3629 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3630 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3631 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3632 | "write-pending", "active-idle", NULL }; |
| 3633 | |
| 3634 | static int match_word(const char *word, char **list) |
| 3635 | { |
| 3636 | int n; |
| 3637 | for (n=0; list[n]; n++) |
| 3638 | if (cmd_match(word, list[n])) |
| 3639 | break; |
| 3640 | return n; |
| 3641 | } |
| 3642 | |
| 3643 | static ssize_t |
| 3644 | array_state_show(mddev_t *mddev, char *page) |
| 3645 | { |
| 3646 | enum array_state st = inactive; |
| 3647 | |
| 3648 | if (mddev->pers) |
| 3649 | switch(mddev->ro) { |
| 3650 | case 1: |
| 3651 | st = readonly; |
| 3652 | break; |
| 3653 | case 2: |
| 3654 | st = read_auto; |
| 3655 | break; |
| 3656 | case 0: |
| 3657 | if (mddev->in_sync) |
| 3658 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3659 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3660 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3661 | else if (mddev->safemode) |
| 3662 | st = active_idle; |
| 3663 | else |
| 3664 | st = active; |
| 3665 | } |
| 3666 | else { |
| 3667 | if (list_empty(&mddev->disks) && |
| 3668 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3669 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3670 | st = clear; |
| 3671 | else |
| 3672 | st = inactive; |
| 3673 | } |
| 3674 | return sprintf(page, "%s\n", array_states[st]); |
| 3675 | } |
| 3676 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3677 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3678 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3679 | static int do_md_run(mddev_t * mddev); |
| 3680 | static int restart_array(mddev_t *mddev); |
| 3681 | |
| 3682 | static ssize_t |
| 3683 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3684 | { |
| 3685 | int err = -EINVAL; |
| 3686 | enum array_state st = match_word(buf, array_states); |
| 3687 | switch(st) { |
| 3688 | case bad_word: |
| 3689 | break; |
| 3690 | case clear: |
| 3691 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3692 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3693 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3694 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3695 | break; |
| 3696 | case inactive: |
| 3697 | /* stopping an active array */ |
| 3698 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3699 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3700 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3701 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3702 | } else |
| 3703 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3704 | break; |
| 3705 | case suspended: |
| 3706 | break; /* not supported yet */ |
| 3707 | case readonly: |
| 3708 | if (mddev->pers) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3709 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3710 | else { |
| 3711 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3712 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3713 | err = do_md_run(mddev); |
| 3714 | } |
| 3715 | break; |
| 3716 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3717 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3718 | if (mddev->ro == 0) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3719 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3720 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3721 | err = restart_array(mddev); |
| 3722 | if (err == 0) { |
| 3723 | mddev->ro = 2; |
| 3724 | set_disk_ro(mddev->gendisk, 0); |
| 3725 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3726 | } else { |
| 3727 | mddev->ro = 2; |
| 3728 | err = do_md_run(mddev); |
| 3729 | } |
| 3730 | break; |
| 3731 | case clean: |
| 3732 | if (mddev->pers) { |
| 3733 | restart_array(mddev); |
| 3734 | spin_lock_irq(&mddev->write_lock); |
| 3735 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3736 | if (mddev->in_sync == 0) { |
| 3737 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3738 | if (mddev->safemode == 1) |
| 3739 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3740 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3741 | } |
| 3742 | err = 0; |
| 3743 | } else |
| 3744 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3745 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3746 | } else |
| 3747 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3748 | break; |
| 3749 | case active: |
| 3750 | if (mddev->pers) { |
| 3751 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3752 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3753 | wake_up(&mddev->sb_wait); |
| 3754 | err = 0; |
| 3755 | } else { |
| 3756 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3757 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3758 | err = do_md_run(mddev); |
| 3759 | } |
| 3760 | break; |
| 3761 | case write_pending: |
| 3762 | case active_idle: |
| 3763 | /* these cannot be set */ |
| 3764 | break; |
| 3765 | } |
| 3766 | if (err) |
| 3767 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3768 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3769 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3770 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3771 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3772 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3773 | static struct md_sysfs_entry md_array_state = |
| 3774 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3776 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3777 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3778 | return sprintf(page, "%d\n", |
| 3779 | atomic_read(&mddev->max_corr_read_errors)); |
| 3780 | } |
| 3781 | |
| 3782 | static ssize_t |
| 3783 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3784 | { |
| 3785 | char *e; |
| 3786 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3787 | |
| 3788 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3789 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3790 | return len; |
| 3791 | } |
| 3792 | return -EINVAL; |
| 3793 | } |
| 3794 | |
| 3795 | static struct md_sysfs_entry max_corr_read_errors = |
| 3796 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3797 | max_corrected_read_errors_store); |
| 3798 | |
| 3799 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3800 | null_show(mddev_t *mddev, char *page) |
| 3801 | { |
| 3802 | return -EINVAL; |
| 3803 | } |
| 3804 | |
| 3805 | static ssize_t |
| 3806 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3807 | { |
| 3808 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3809 | /* The new device is added to the array. |
| 3810 | * If the array has a persistent superblock, we read the |
| 3811 | * superblock to initialise info and check validity. |
| 3812 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3813 | * which mainly checks size. |
| 3814 | */ |
| 3815 | char *e; |
| 3816 | int major = simple_strtoul(buf, &e, 10); |
| 3817 | int minor; |
| 3818 | dev_t dev; |
| 3819 | mdk_rdev_t *rdev; |
| 3820 | int err; |
| 3821 | |
| 3822 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3823 | return -EINVAL; |
| 3824 | minor = simple_strtoul(e+1, &e, 10); |
| 3825 | if (*e && *e != '\n') |
| 3826 | return -EINVAL; |
| 3827 | dev = MKDEV(major, minor); |
| 3828 | if (major != MAJOR(dev) || |
| 3829 | minor != MINOR(dev)) |
| 3830 | return -EOVERFLOW; |
| 3831 | |
| 3832 | |
| 3833 | if (mddev->persistent) { |
| 3834 | rdev = md_import_device(dev, mddev->major_version, |
| 3835 | mddev->minor_version); |
| 3836 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3837 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3838 | mdk_rdev_t, same_set); |
| 3839 | err = super_types[mddev->major_version] |
| 3840 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3841 | if (err < 0) |
| 3842 | goto out; |
| 3843 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3844 | } else if (mddev->external) |
| 3845 | rdev = md_import_device(dev, -2, -1); |
| 3846 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3847 | rdev = md_import_device(dev, -1, -1); |
| 3848 | |
| 3849 | if (IS_ERR(rdev)) |
| 3850 | return PTR_ERR(rdev); |
| 3851 | err = bind_rdev_to_array(rdev, mddev); |
| 3852 | out: |
| 3853 | if (err) |
| 3854 | export_rdev(rdev); |
| 3855 | return err ? err : len; |
| 3856 | } |
| 3857 | |
| 3858 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3859 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3860 | |
| 3861 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3862 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3863 | { |
| 3864 | char *end; |
| 3865 | unsigned long chunk, end_chunk; |
| 3866 | |
| 3867 | if (!mddev->bitmap) |
| 3868 | goto out; |
| 3869 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3870 | while (*buf) { |
| 3871 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3872 | if (buf == end) break; |
| 3873 | if (*end == '-') { /* range */ |
| 3874 | buf = end + 1; |
| 3875 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3876 | if (buf == end) break; |
| 3877 | } |
| 3878 | if (*end && !isspace(*end)) break; |
| 3879 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3880 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3881 | } |
| 3882 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3883 | out: |
| 3884 | return len; |
| 3885 | } |
| 3886 | |
| 3887 | static struct md_sysfs_entry md_bitmap = |
| 3888 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3889 | |
| 3890 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3891 | size_show(mddev_t *mddev, char *page) |
| 3892 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3893 | return sprintf(page, "%llu\n", |
| 3894 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3895 | } |
| 3896 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3897 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3898 | |
| 3899 | static ssize_t |
| 3900 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3901 | { |
| 3902 | /* If array is inactive, we can reduce the component size, but |
| 3903 | * not increase it (except from 0). |
| 3904 | * If array is active, we can try an on-line resize |
| 3905 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3906 | sector_t sectors; |
| 3907 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3908 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3909 | if (err < 0) |
| 3910 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3911 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3912 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3913 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3914 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3915 | if (mddev->dev_sectors == 0 || |
| 3916 | mddev->dev_sectors > sectors) |
| 3917 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3918 | else |
| 3919 | err = -ENOSPC; |
| 3920 | } |
| 3921 | return err ? err : len; |
| 3922 | } |
| 3923 | |
| 3924 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3925 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3926 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3927 | |
| 3928 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3929 | * This is one of |
| 3930 | * 'none' for arrays with no metadata (good luck...) |
| 3931 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3932 | * or N.M for internally known formats |
| 3933 | */ |
| 3934 | static ssize_t |
| 3935 | metadata_show(mddev_t *mddev, char *page) |
| 3936 | { |
| 3937 | if (mddev->persistent) |
| 3938 | return sprintf(page, "%d.%d\n", |
| 3939 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3940 | else if (mddev->external) |
| 3941 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3942 | else |
| 3943 | return sprintf(page, "none\n"); |
| 3944 | } |
| 3945 | |
| 3946 | static ssize_t |
| 3947 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3948 | { |
| 3949 | int major, minor; |
| 3950 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3951 | /* Changing the details of 'external' metadata is |
| 3952 | * always permitted. Otherwise there must be |
| 3953 | * no devices attached to the array. |
| 3954 | */ |
| 3955 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3956 | ; |
| 3957 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3958 | return -EBUSY; |
| 3959 | |
| 3960 | if (cmd_match(buf, "none")) { |
| 3961 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3962 | mddev->external = 0; |
| 3963 | mddev->major_version = 0; |
| 3964 | mddev->minor_version = 90; |
| 3965 | return len; |
| 3966 | } |
| 3967 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3968 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3969 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3970 | namelen = sizeof(mddev->metadata_type)-1; |
| 3971 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3972 | mddev->metadata_type[namelen] = 0; |
| 3973 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3974 | mddev->metadata_type[--namelen] = 0; |
| 3975 | mddev->persistent = 0; |
| 3976 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3977 | mddev->major_version = 0; |
| 3978 | mddev->minor_version = 90; |
| 3979 | return len; |
| 3980 | } |
| 3981 | major = simple_strtoul(buf, &e, 10); |
| 3982 | if (e==buf || *e != '.') |
| 3983 | return -EINVAL; |
| 3984 | buf = e+1; |
| 3985 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3986 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3987 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3988 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3989 | return -ENOENT; |
| 3990 | mddev->major_version = major; |
| 3991 | mddev->minor_version = minor; |
| 3992 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3993 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3994 | return len; |
| 3995 | } |
| 3996 | |
| 3997 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3998 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3999 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4000 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4001 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4002 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4003 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4004 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4005 | type = "frozen"; |
| 4006 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4007 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4008 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4009 | type = "reshape"; |
| 4010 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4011 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4012 | type = "resync"; |
| 4013 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4014 | type = "check"; |
| 4015 | else |
| 4016 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4017 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4018 | type = "recover"; |
| 4019 | } |
| 4020 | return sprintf(page, "%s\n", type); |
| 4021 | } |
| 4022 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4023 | static void reap_sync_thread(mddev_t *mddev); |
| 4024 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4025 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4026 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4027 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4028 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4029 | return -EINVAL; |
| 4030 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4031 | if (cmd_match(page, "frozen")) |
| 4032 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4033 | else |
| 4034 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4035 | |
| 4036 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4037 | if (mddev->sync_thread) { |
| 4038 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4039 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4040 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4041 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4042 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4043 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4044 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4045 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4046 | else if (cmd_match(page, "recover")) { |
| 4047 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4048 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4049 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4050 | int err; |
| 4051 | if (mddev->pers->start_reshape == NULL) |
| 4052 | return -EINVAL; |
| 4053 | err = mddev->pers->start_reshape(mddev); |
| 4054 | if (err) |
| 4055 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4056 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4057 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4058 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4059 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4060 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4061 | return -EINVAL; |
| 4062 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4063 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4064 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4065 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4066 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4067 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4068 | return len; |
| 4069 | } |
| 4070 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4071 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4072 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4073 | { |
| 4074 | return sprintf(page, "%llu\n", |
| 4075 | (unsigned long long) mddev->resync_mismatches); |
| 4076 | } |
| 4077 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4078 | static struct md_sysfs_entry md_scan_mode = |
| 4079 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4080 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4081 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4082 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4083 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4084 | static ssize_t |
| 4085 | sync_min_show(mddev_t *mddev, char *page) |
| 4086 | { |
| 4087 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4088 | mddev->sync_speed_min ? "local": "system"); |
| 4089 | } |
| 4090 | |
| 4091 | static ssize_t |
| 4092 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 4093 | { |
| 4094 | int min; |
| 4095 | char *e; |
| 4096 | if (strncmp(buf, "system", 6)==0) { |
| 4097 | mddev->sync_speed_min = 0; |
| 4098 | return len; |
| 4099 | } |
| 4100 | min = simple_strtoul(buf, &e, 10); |
| 4101 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4102 | return -EINVAL; |
| 4103 | mddev->sync_speed_min = min; |
| 4104 | return len; |
| 4105 | } |
| 4106 | |
| 4107 | static struct md_sysfs_entry md_sync_min = |
| 4108 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4109 | |
| 4110 | static ssize_t |
| 4111 | sync_max_show(mddev_t *mddev, char *page) |
| 4112 | { |
| 4113 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4114 | mddev->sync_speed_max ? "local": "system"); |
| 4115 | } |
| 4116 | |
| 4117 | static ssize_t |
| 4118 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 4119 | { |
| 4120 | int max; |
| 4121 | char *e; |
| 4122 | if (strncmp(buf, "system", 6)==0) { |
| 4123 | mddev->sync_speed_max = 0; |
| 4124 | return len; |
| 4125 | } |
| 4126 | max = simple_strtoul(buf, &e, 10); |
| 4127 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4128 | return -EINVAL; |
| 4129 | mddev->sync_speed_max = max; |
| 4130 | return len; |
| 4131 | } |
| 4132 | |
| 4133 | static struct md_sysfs_entry md_sync_max = |
| 4134 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4135 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4136 | static ssize_t |
| 4137 | degraded_show(mddev_t *mddev, char *page) |
| 4138 | { |
| 4139 | return sprintf(page, "%d\n", mddev->degraded); |
| 4140 | } |
| 4141 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4142 | |
| 4143 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4144 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 4145 | { |
| 4146 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4147 | } |
| 4148 | |
| 4149 | static ssize_t |
| 4150 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 4151 | { |
| 4152 | long n; |
| 4153 | |
| 4154 | if (strict_strtol(buf, 10, &n)) |
| 4155 | return -EINVAL; |
| 4156 | |
| 4157 | if (n != 0 && n != 1) |
| 4158 | return -EINVAL; |
| 4159 | |
| 4160 | mddev->parallel_resync = n; |
| 4161 | |
| 4162 | if (mddev->sync_thread) |
| 4163 | wake_up(&resync_wait); |
| 4164 | |
| 4165 | return len; |
| 4166 | } |
| 4167 | |
| 4168 | /* force parallel resync, even with shared block devices */ |
| 4169 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4170 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4171 | sync_force_parallel_show, sync_force_parallel_store); |
| 4172 | |
| 4173 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4174 | sync_speed_show(mddev_t *mddev, char *page) |
| 4175 | { |
| 4176 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4177 | if (mddev->curr_resync == 0) |
| 4178 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4179 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4180 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4181 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4182 | db = resync - mddev->resync_mark_cnt; |
| 4183 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4184 | } |
| 4185 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4186 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4187 | |
| 4188 | static ssize_t |
| 4189 | sync_completed_show(mddev_t *mddev, char *page) |
| 4190 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4191 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4192 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4193 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4194 | return sprintf(page, "none\n"); |
| 4195 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4196 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4197 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4198 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4199 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4200 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4201 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4202 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4203 | } |
| 4204 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4205 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4206 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4207 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4208 | min_sync_show(mddev_t *mddev, char *page) |
| 4209 | { |
| 4210 | return sprintf(page, "%llu\n", |
| 4211 | (unsigned long long)mddev->resync_min); |
| 4212 | } |
| 4213 | static ssize_t |
| 4214 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4215 | { |
| 4216 | unsigned long long min; |
| 4217 | if (strict_strtoull(buf, 10, &min)) |
| 4218 | return -EINVAL; |
| 4219 | if (min > mddev->resync_max) |
| 4220 | return -EINVAL; |
| 4221 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4222 | return -EBUSY; |
| 4223 | |
| 4224 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4225 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4226 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4227 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4228 | return -EINVAL; |
| 4229 | } |
| 4230 | mddev->resync_min = min; |
| 4231 | |
| 4232 | return len; |
| 4233 | } |
| 4234 | |
| 4235 | static struct md_sysfs_entry md_min_sync = |
| 4236 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4237 | |
| 4238 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4239 | max_sync_show(mddev_t *mddev, char *page) |
| 4240 | { |
| 4241 | if (mddev->resync_max == MaxSector) |
| 4242 | return sprintf(page, "max\n"); |
| 4243 | else |
| 4244 | return sprintf(page, "%llu\n", |
| 4245 | (unsigned long long)mddev->resync_max); |
| 4246 | } |
| 4247 | static ssize_t |
| 4248 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4249 | { |
| 4250 | if (strncmp(buf, "max", 3) == 0) |
| 4251 | mddev->resync_max = MaxSector; |
| 4252 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4253 | unsigned long long max; |
| 4254 | if (strict_strtoull(buf, 10, &max)) |
| 4255 | return -EINVAL; |
| 4256 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4257 | return -EINVAL; |
| 4258 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4259 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4260 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4261 | return -EBUSY; |
| 4262 | |
| 4263 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4264 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4265 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4266 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4267 | return -EINVAL; |
| 4268 | } |
| 4269 | mddev->resync_max = max; |
| 4270 | } |
| 4271 | wake_up(&mddev->recovery_wait); |
| 4272 | return len; |
| 4273 | } |
| 4274 | |
| 4275 | static struct md_sysfs_entry md_max_sync = |
| 4276 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4277 | |
| 4278 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4279 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4280 | { |
| 4281 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4282 | } |
| 4283 | |
| 4284 | static ssize_t |
| 4285 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4286 | { |
| 4287 | char *e; |
| 4288 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4289 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4290 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4291 | if (mddev->pers == NULL || |
| 4292 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4293 | return -EINVAL; |
| 4294 | if (buf == e || (*e && *e != '\n')) |
| 4295 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4296 | |
| 4297 | mddev->suspend_lo = new; |
| 4298 | if (new >= old) |
| 4299 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4300 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4301 | else { |
| 4302 | /* Expanding suspended region - need to wait */ |
| 4303 | mddev->pers->quiesce(mddev, 1); |
| 4304 | mddev->pers->quiesce(mddev, 0); |
| 4305 | } |
| 4306 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4307 | } |
| 4308 | static struct md_sysfs_entry md_suspend_lo = |
| 4309 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4310 | |
| 4311 | |
| 4312 | static ssize_t |
| 4313 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4314 | { |
| 4315 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4316 | } |
| 4317 | |
| 4318 | static ssize_t |
| 4319 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4320 | { |
| 4321 | char *e; |
| 4322 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4323 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4324 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4325 | if (mddev->pers == NULL || |
| 4326 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4327 | return -EINVAL; |
| 4328 | if (buf == e || (*e && *e != '\n')) |
| 4329 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4330 | |
| 4331 | mddev->suspend_hi = new; |
| 4332 | if (new <= old) |
| 4333 | /* Shrinking suspended region */ |
| 4334 | mddev->pers->quiesce(mddev, 2); |
| 4335 | else { |
| 4336 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4337 | mddev->pers->quiesce(mddev, 1); |
| 4338 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4339 | } |
| 4340 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4341 | } |
| 4342 | static struct md_sysfs_entry md_suspend_hi = |
| 4343 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4344 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4345 | static ssize_t |
| 4346 | reshape_position_show(mddev_t *mddev, char *page) |
| 4347 | { |
| 4348 | if (mddev->reshape_position != MaxSector) |
| 4349 | return sprintf(page, "%llu\n", |
| 4350 | (unsigned long long)mddev->reshape_position); |
| 4351 | strcpy(page, "none\n"); |
| 4352 | return 5; |
| 4353 | } |
| 4354 | |
| 4355 | static ssize_t |
| 4356 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4357 | { |
| 4358 | char *e; |
| 4359 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4360 | if (mddev->pers) |
| 4361 | return -EBUSY; |
| 4362 | if (buf == e || (*e && *e != '\n')) |
| 4363 | return -EINVAL; |
| 4364 | mddev->reshape_position = new; |
| 4365 | mddev->delta_disks = 0; |
| 4366 | mddev->new_level = mddev->level; |
| 4367 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4368 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4369 | return len; |
| 4370 | } |
| 4371 | |
| 4372 | static struct md_sysfs_entry md_reshape_position = |
| 4373 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4374 | reshape_position_store); |
| 4375 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4376 | static ssize_t |
| 4377 | array_size_show(mddev_t *mddev, char *page) |
| 4378 | { |
| 4379 | if (mddev->external_size) |
| 4380 | return sprintf(page, "%llu\n", |
| 4381 | (unsigned long long)mddev->array_sectors/2); |
| 4382 | else |
| 4383 | return sprintf(page, "default\n"); |
| 4384 | } |
| 4385 | |
| 4386 | static ssize_t |
| 4387 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4388 | { |
| 4389 | sector_t sectors; |
| 4390 | |
| 4391 | if (strncmp(buf, "default", 7) == 0) { |
| 4392 | if (mddev->pers) |
| 4393 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4394 | else |
| 4395 | sectors = mddev->array_sectors; |
| 4396 | |
| 4397 | mddev->external_size = 0; |
| 4398 | } else { |
| 4399 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4400 | return -EINVAL; |
| 4401 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4402 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4403 | |
| 4404 | mddev->external_size = 1; |
| 4405 | } |
| 4406 | |
| 4407 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4408 | if (mddev->pers) { |
| 4409 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4410 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4411 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4412 | return len; |
| 4413 | } |
| 4414 | |
| 4415 | static struct md_sysfs_entry md_array_size = |
| 4416 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4417 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4418 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4419 | static struct attribute *md_default_attrs[] = { |
| 4420 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4421 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4422 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4423 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4424 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4425 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4426 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4427 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4428 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4429 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4430 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4431 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4432 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4433 | NULL, |
| 4434 | }; |
| 4435 | |
| 4436 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4437 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4438 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4439 | &md_sync_min.attr, |
| 4440 | &md_sync_max.attr, |
| 4441 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4442 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4443 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4444 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4445 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4446 | &md_suspend_lo.attr, |
| 4447 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4448 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4449 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4450 | NULL, |
| 4451 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4452 | static struct attribute_group md_redundancy_group = { |
| 4453 | .name = NULL, |
| 4454 | .attrs = md_redundancy_attrs, |
| 4455 | }; |
| 4456 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4457 | |
| 4458 | static ssize_t |
| 4459 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4460 | { |
| 4461 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4462 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4463 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4464 | |
| 4465 | if (!entry->show) |
| 4466 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4467 | rv = mddev_lock(mddev); |
| 4468 | if (!rv) { |
| 4469 | rv = entry->show(mddev, page); |
| 4470 | mddev_unlock(mddev); |
| 4471 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4472 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | static ssize_t |
| 4476 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4477 | const char *page, size_t length) |
| 4478 | { |
| 4479 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4480 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4481 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4482 | |
| 4483 | if (!entry->store) |
| 4484 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4485 | if (!capable(CAP_SYS_ADMIN)) |
| 4486 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4487 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4488 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4489 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4490 | if (!rv) { |
| 4491 | rv = entry->store(mddev, page, length); |
| 4492 | mddev_unlock(mddev); |
| 4493 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4494 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | static void md_free(struct kobject *ko) |
| 4498 | { |
| 4499 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4500 | |
| 4501 | if (mddev->sysfs_state) |
| 4502 | sysfs_put(mddev->sysfs_state); |
| 4503 | |
| 4504 | if (mddev->gendisk) { |
| 4505 | del_gendisk(mddev->gendisk); |
| 4506 | put_disk(mddev->gendisk); |
| 4507 | } |
| 4508 | if (mddev->queue) |
| 4509 | blk_cleanup_queue(mddev->queue); |
| 4510 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4511 | kfree(mddev); |
| 4512 | } |
| 4513 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4514 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4515 | .show = md_attr_show, |
| 4516 | .store = md_attr_store, |
| 4517 | }; |
| 4518 | static struct kobj_type md_ktype = { |
| 4519 | .release = md_free, |
| 4520 | .sysfs_ops = &md_sysfs_ops, |
| 4521 | .default_attrs = md_default_attrs, |
| 4522 | }; |
| 4523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | int mdp_major = 0; |
| 4525 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4526 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4527 | { |
| 4528 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4529 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4530 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4531 | kobject_del(&mddev->kobj); |
| 4532 | kobject_put(&mddev->kobj); |
| 4533 | } |
| 4534 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4535 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4537 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | mddev_t *mddev = mddev_find(dev); |
| 4539 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4540 | int partitioned; |
| 4541 | int shift; |
| 4542 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4543 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
| 4545 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4546 | return -ENODEV; |
| 4547 | |
| 4548 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4549 | shift = partitioned ? MdpMinorShift : 0; |
| 4550 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4552 | /* wait for any previous instance of this device to be |
| 4553 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4554 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4555 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4556 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4557 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4558 | error = -EEXIST; |
| 4559 | if (mddev->gendisk) |
| 4560 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4561 | |
| 4562 | if (name) { |
| 4563 | /* Need to ensure that 'name' is not a duplicate. |
| 4564 | */ |
| 4565 | mddev_t *mddev2; |
| 4566 | spin_lock(&all_mddevs_lock); |
| 4567 | |
| 4568 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4569 | if (mddev2->gendisk && |
| 4570 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4571 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4572 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4573 | } |
| 4574 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4576 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4577 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4578 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4579 | if (!mddev->queue) |
| 4580 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4581 | mddev->queue->queuedata = mddev; |
| 4582 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4583 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | disk = alloc_disk(1 << shift); |
| 4586 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4587 | blk_cleanup_queue(mddev->queue); |
| 4588 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4589 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4591 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4593 | if (name) |
| 4594 | strcpy(disk->disk_name, name); |
| 4595 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4597 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | disk->fops = &md_fops; |
| 4600 | disk->private_data = mddev; |
| 4601 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4602 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4603 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4604 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4605 | * remove it now. |
| 4606 | */ |
| 4607 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4609 | /* As soon as we call add_disk(), another thread could get |
| 4610 | * through to md_open, so make sure it doesn't get too far |
| 4611 | */ |
| 4612 | mutex_lock(&mddev->open_mutex); |
| 4613 | add_disk(disk); |
| 4614 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4615 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4616 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4617 | if (error) { |
| 4618 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4619 | * __must_check, we must do something with the result |
| 4620 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4621 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4622 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4623 | error = 0; |
| 4624 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4625 | if (mddev->kobj.sd && |
| 4626 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4627 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4628 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4629 | abort: |
| 4630 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4631 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4632 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4633 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4634 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4635 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4636 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4640 | { |
| 4641 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | return NULL; |
| 4643 | } |
| 4644 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4645 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4646 | { |
| 4647 | /* val must be "md_*" where * is not all digits. |
| 4648 | * We allocate an array with a large free minor number, and |
| 4649 | * set the name to val. val must not already be an active name. |
| 4650 | */ |
| 4651 | int len = strlen(val); |
| 4652 | char buf[DISK_NAME_LEN]; |
| 4653 | |
| 4654 | while (len && val[len-1] == '\n') |
| 4655 | len--; |
| 4656 | if (len >= DISK_NAME_LEN) |
| 4657 | return -E2BIG; |
| 4658 | strlcpy(buf, val, len+1); |
| 4659 | if (strncmp(buf, "md_", 3) != 0) |
| 4660 | return -EINVAL; |
| 4661 | return md_alloc(0, buf); |
| 4662 | } |
| 4663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | static void md_safemode_timeout(unsigned long data) |
| 4665 | { |
| 4666 | mddev_t *mddev = (mddev_t *) data; |
| 4667 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4668 | if (!atomic_read(&mddev->writes_pending)) { |
| 4669 | mddev->safemode = 1; |
| 4670 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4671 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | md_wakeup_thread(mddev->thread); |
| 4674 | } |
| 4675 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4676 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4678 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4680 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4682 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4684 | if (list_empty(&mddev->disks)) |
| 4685 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | |
| 4688 | if (mddev->pers) |
| 4689 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4690 | /* Cannot run until previous stop completes properly */ |
| 4691 | if (mddev->sysfs_active) |
| 4692 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | /* |
| 4695 | * Analyze all RAID superblock(s) |
| 4696 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4697 | if (!mddev->raid_disks) { |
| 4698 | if (!mddev->persistent) |
| 4699 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4700 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4703 | if (mddev->level != LEVEL_NONE) |
| 4704 | request_module("md-level-%d", mddev->level); |
| 4705 | else if (mddev->clevel[0]) |
| 4706 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | |
| 4708 | /* |
| 4709 | * Drop all container device buffers, from now on |
| 4710 | * the only valid external interface is through the md |
| 4711 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4713 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4714 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | continue; |
| 4716 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4717 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4718 | |
| 4719 | /* perform some consistency tests on the device. |
| 4720 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4721 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4722 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4723 | if (rdev->meta_bdev) { |
| 4724 | /* Nothing to check */; |
| 4725 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4726 | if (mddev->dev_sectors && |
| 4727 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4728 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4729 | printk("md: %s: data overlaps metadata\n", |
| 4730 | mdname(mddev)); |
| 4731 | return -EINVAL; |
| 4732 | } |
| 4733 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4734 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4735 | > rdev->data_offset) { |
| 4736 | printk("md: %s: metadata overlaps data\n", |
| 4737 | mdname(mddev)); |
| 4738 | return -EINVAL; |
| 4739 | } |
| 4740 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4741 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | } |
| 4743 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4744 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4745 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
| 4746 | sizeof(mddev_t *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4749 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4750 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4752 | if (mddev->level != LEVEL_NONE) |
| 4753 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4754 | mddev->level); |
| 4755 | else |
| 4756 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4757 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | return -EINVAL; |
| 4759 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4760 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4762 | if (mddev->level != pers->level) { |
| 4763 | mddev->level = pers->level; |
| 4764 | mddev->new_level = pers->level; |
| 4765 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4766 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4768 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4769 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4770 | /* This personality cannot handle reshaping... */ |
| 4771 | mddev->pers = NULL; |
| 4772 | module_put(pers->owner); |
| 4773 | return -EINVAL; |
| 4774 | } |
| 4775 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4776 | if (pers->sync_request) { |
| 4777 | /* Warn if this is a potentially silly |
| 4778 | * configuration. |
| 4779 | */ |
| 4780 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4781 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4782 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4783 | |
| 4784 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4785 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4786 | if (rdev < rdev2 && |
| 4787 | rdev->bdev->bd_contains == |
| 4788 | rdev2->bdev->bd_contains) { |
| 4789 | printk(KERN_WARNING |
| 4790 | "%s: WARNING: %s appears to be" |
| 4791 | " on the same physical disk as" |
| 4792 | " %s.\n", |
| 4793 | mdname(mddev), |
| 4794 | bdevname(rdev->bdev,b), |
| 4795 | bdevname(rdev2->bdev,b2)); |
| 4796 | warned = 1; |
| 4797 | } |
| 4798 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4799 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4800 | if (warned) |
| 4801 | printk(KERN_WARNING |
| 4802 | "True protection against single-disk" |
| 4803 | " failure might be compromised.\n"); |
| 4804 | } |
| 4805 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4806 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4807 | /* may be over-ridden by personality */ |
| 4808 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4809 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4810 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4812 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4813 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4814 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4815 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4816 | if (err) |
| 4817 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4818 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4819 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4820 | " but 'external_size' not in effect?\n", __func__); |
| 4821 | printk(KERN_ERR |
| 4822 | "md: invalid array_size %llu > default size %llu\n", |
| 4823 | (unsigned long long)mddev->array_sectors / 2, |
| 4824 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4825 | err = -EINVAL; |
| 4826 | mddev->pers->stop(mddev); |
| 4827 | } |
| 4828 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4829 | err = bitmap_create(mddev); |
| 4830 | if (err) { |
| 4831 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4832 | mdname(mddev), err); |
| 4833 | mddev->pers->stop(mddev); |
| 4834 | } |
| 4835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | module_put(mddev->pers->owner); |
| 4838 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4839 | bitmap_destroy(mddev); |
| 4840 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4842 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4843 | if (mddev->kobj.sd && |
| 4844 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4845 | printk(KERN_WARNING |
| 4846 | "md: cannot register extra attributes for %s\n", |
| 4847 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4848 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4849 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4850 | mddev->ro = 0; |
| 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4853 | atomic_set(&mddev->max_corr_read_errors, |
| 4854 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | mddev->safemode = 0; |
| 4856 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4857 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4858 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4860 | smp_wmb(); |
| 4861 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4862 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4863 | if (rdev->raid_disk >= 0) |
| 4864 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4865 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | |
| 4867 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4868 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4869 | if (mddev->flags) |
| 4870 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4872 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4873 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4874 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4875 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | return 0; |
| 4877 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4878 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4880 | static int do_md_run(mddev_t *mddev) |
| 4881 | { |
| 4882 | int err; |
| 4883 | |
| 4884 | err = md_run(mddev); |
| 4885 | if (err) |
| 4886 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4887 | err = bitmap_load(mddev); |
| 4888 | if (err) { |
| 4889 | bitmap_destroy(mddev); |
| 4890 | goto out; |
| 4891 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4892 | |
| 4893 | md_wakeup_thread(mddev->thread); |
| 4894 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4895 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4896 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4897 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4898 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4899 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4900 | out: |
| 4901 | return err; |
| 4902 | } |
| 4903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | static int restart_array(mddev_t *mddev) |
| 4905 | { |
| 4906 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4908 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4910 | return -ENXIO; |
| 4911 | if (!mddev->pers) |
| 4912 | return -EINVAL; |
| 4913 | if (!mddev->ro) |
| 4914 | return -EBUSY; |
| 4915 | mddev->safemode = 0; |
| 4916 | mddev->ro = 0; |
| 4917 | set_disk_ro(disk, 0); |
| 4918 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4919 | mdname(mddev)); |
| 4920 | /* Kick recovery or resync if necessary */ |
| 4921 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4922 | md_wakeup_thread(mddev->thread); |
| 4923 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4924 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4925 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | } |
| 4927 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4928 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4929 | * to the file ourselves */ |
| 4930 | static int deny_bitmap_write_access(struct file * file) |
| 4931 | { |
| 4932 | struct inode *inode = file->f_mapping->host; |
| 4933 | |
| 4934 | spin_lock(&inode->i_lock); |
| 4935 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4936 | spin_unlock(&inode->i_lock); |
| 4937 | return -ETXTBSY; |
| 4938 | } |
| 4939 | atomic_set(&inode->i_writecount, -1); |
| 4940 | spin_unlock(&inode->i_lock); |
| 4941 | |
| 4942 | return 0; |
| 4943 | } |
| 4944 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4945 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4946 | { |
| 4947 | struct inode *inode = file->f_mapping->host; |
| 4948 | |
| 4949 | spin_lock(&inode->i_lock); |
| 4950 | atomic_set(&inode->i_writecount, 1); |
| 4951 | spin_unlock(&inode->i_lock); |
| 4952 | } |
| 4953 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4954 | static void md_clean(mddev_t *mddev) |
| 4955 | { |
| 4956 | mddev->array_sectors = 0; |
| 4957 | mddev->external_size = 0; |
| 4958 | mddev->dev_sectors = 0; |
| 4959 | mddev->raid_disks = 0; |
| 4960 | mddev->recovery_cp = 0; |
| 4961 | mddev->resync_min = 0; |
| 4962 | mddev->resync_max = MaxSector; |
| 4963 | mddev->reshape_position = MaxSector; |
| 4964 | mddev->external = 0; |
| 4965 | mddev->persistent = 0; |
| 4966 | mddev->level = LEVEL_NONE; |
| 4967 | mddev->clevel[0] = 0; |
| 4968 | mddev->flags = 0; |
| 4969 | mddev->ro = 0; |
| 4970 | mddev->metadata_type[0] = 0; |
| 4971 | mddev->chunk_sectors = 0; |
| 4972 | mddev->ctime = mddev->utime = 0; |
| 4973 | mddev->layout = 0; |
| 4974 | mddev->max_disks = 0; |
| 4975 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4976 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4977 | mddev->delta_disks = 0; |
| 4978 | mddev->new_level = LEVEL_NONE; |
| 4979 | mddev->new_layout = 0; |
| 4980 | mddev->new_chunk_sectors = 0; |
| 4981 | mddev->curr_resync = 0; |
| 4982 | mddev->resync_mismatches = 0; |
| 4983 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4984 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4985 | mddev->recovery = 0; |
| 4986 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4987 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4988 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4989 | mddev->safemode = 0; |
| 4990 | mddev->bitmap_info.offset = 0; |
| 4991 | mddev->bitmap_info.default_offset = 0; |
| 4992 | mddev->bitmap_info.chunksize = 0; |
| 4993 | mddev->bitmap_info.daemon_sleep = 0; |
| 4994 | mddev->bitmap_info.max_write_behind = 0; |
| 4995 | } |
| 4996 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4997 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4998 | { |
| 4999 | if (mddev->sync_thread) { |
| 5000 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5001 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5002 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | del_timer_sync(&mddev->safemode_timer); |
| 5006 | |
| 5007 | bitmap_flush(mddev); |
| 5008 | md_super_wait(mddev); |
| 5009 | |
| 5010 | if (!mddev->in_sync || mddev->flags) { |
| 5011 | /* mark array as shutdown cleanly */ |
| 5012 | mddev->in_sync = 1; |
| 5013 | md_update_sb(mddev, 1); |
| 5014 | } |
| 5015 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5016 | |
| 5017 | void md_stop_writes(mddev_t *mddev) |
| 5018 | { |
| 5019 | mddev_lock(mddev); |
| 5020 | __md_stop_writes(mddev); |
| 5021 | mddev_unlock(mddev); |
| 5022 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5023 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5024 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5025 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5026 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5027 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5028 | mddev->pers->stop(mddev); |
| 5029 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5030 | mddev->to_remove = &md_redundancy_group; |
| 5031 | module_put(mddev->pers->owner); |
| 5032 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5033 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5034 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5035 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5036 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5037 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 5038 | { |
| 5039 | int err = 0; |
| 5040 | mutex_lock(&mddev->open_mutex); |
| 5041 | if (atomic_read(&mddev->openers) > is_open) { |
| 5042 | printk("md: %s still in use.\n",mdname(mddev)); |
| 5043 | err = -EBUSY; |
| 5044 | goto out; |
| 5045 | } |
| 5046 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5047 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5048 | |
| 5049 | err = -ENXIO; |
| 5050 | if (mddev->ro==1) |
| 5051 | goto out; |
| 5052 | mddev->ro = 1; |
| 5053 | set_disk_ro(mddev->gendisk, 1); |
| 5054 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5055 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5056 | err = 0; |
| 5057 | } |
| 5058 | out: |
| 5059 | mutex_unlock(&mddev->open_mutex); |
| 5060 | return err; |
| 5061 | } |
| 5062 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5063 | /* mode: |
| 5064 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5065 | * 2 - stop but do not disassemble array |
| 5066 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5067 | 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] | 5068 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5070 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5072 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5073 | if (atomic_read(&mddev->openers) > is_open || |
| 5074 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5075 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5076 | mutex_unlock(&mddev->open_mutex); |
| 5077 | return -EBUSY; |
| 5078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5080 | if (mddev->pers) { |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5081 | if (mddev->ro) |
| 5082 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5083 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5084 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5085 | md_stop(mddev); |
| 5086 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5087 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5088 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5089 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5090 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5091 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5092 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5093 | if (rdev->raid_disk >= 0) |
| 5094 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5095 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5096 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5097 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5098 | mddev->changed = 1; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5099 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5100 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5101 | if (mddev->ro) |
| 5102 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5103 | } else |
| 5104 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | /* |
| 5106 | * Free resources if final stop |
| 5107 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5108 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5110 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5111 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5112 | if (mddev->bitmap_info.file) { |
| 5113 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5114 | fput(mddev->bitmap_info.file); |
| 5115 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5116 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5117 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | export_array(mddev); |
| 5120 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5121 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5122 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5123 | if (mddev->hold_active == UNTIL_STOP) |
| 5124 | mddev->hold_active = 0; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5125 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5126 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5127 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5128 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5132 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | static void autorun_array(mddev_t *mddev) |
| 5134 | { |
| 5135 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | int err; |
| 5137 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5138 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
| 5141 | printk(KERN_INFO "md: running: "); |
| 5142 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5143 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | char b[BDEVNAME_SIZE]; |
| 5145 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5146 | } |
| 5147 | printk("\n"); |
| 5148 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5149 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | if (err) { |
| 5151 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5152 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | /* |
| 5157 | * lets try to run arrays based on all disks that have arrived |
| 5158 | * until now. (those are in pending_raid_disks) |
| 5159 | * |
| 5160 | * the method: pick the first pending disk, collect all disks with |
| 5161 | * the same UUID, remove all from the pending list and put them into |
| 5162 | * the 'same_array' list. Then order this list based on superblock |
| 5163 | * update time (freshest comes first), kick out 'old' disks and |
| 5164 | * compare superblocks. If everything's fine then run it. |
| 5165 | * |
| 5166 | * If "unit" is allocated, then bump its reference count |
| 5167 | */ |
| 5168 | static void autorun_devices(int part) |
| 5169 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5170 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | mddev_t *mddev; |
| 5172 | char b[BDEVNAME_SIZE]; |
| 5173 | |
| 5174 | printk(KERN_INFO "md: autorun ...\n"); |
| 5175 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5176 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5178 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | rdev0 = list_entry(pending_raid_disks.next, |
| 5180 | mdk_rdev_t, same_set); |
| 5181 | |
| 5182 | printk(KERN_INFO "md: considering %s ...\n", |
| 5183 | bdevname(rdev0->bdev,b)); |
| 5184 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5185 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5187 | printk(KERN_INFO "md: adding %s ...\n", |
| 5188 | bdevname(rdev->bdev,b)); |
| 5189 | list_move(&rdev->same_set, &candidates); |
| 5190 | } |
| 5191 | /* |
| 5192 | * now we have a set of devices, with all of them having |
| 5193 | * mostly sane superblocks. It's time to allocate the |
| 5194 | * mddev. |
| 5195 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5196 | if (part) { |
| 5197 | dev = MKDEV(mdp_major, |
| 5198 | rdev0->preferred_minor << MdpMinorShift); |
| 5199 | unit = MINOR(dev) >> MdpMinorShift; |
| 5200 | } else { |
| 5201 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5202 | unit = MINOR(dev); |
| 5203 | } |
| 5204 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5206 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5207 | break; |
| 5208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | |
| 5210 | md_probe(dev, NULL, NULL); |
| 5211 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5212 | if (!mddev || !mddev->gendisk) { |
| 5213 | if (mddev) |
| 5214 | mddev_put(mddev); |
| 5215 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | "md: cannot allocate memory for md drive.\n"); |
| 5217 | break; |
| 5218 | } |
| 5219 | if (mddev_lock(mddev)) |
| 5220 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5221 | mdname(mddev)); |
| 5222 | else if (mddev->raid_disks || mddev->major_version |
| 5223 | || !list_empty(&mddev->disks)) { |
| 5224 | printk(KERN_WARNING |
| 5225 | "md: %s already running, cannot run %s\n", |
| 5226 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5227 | mddev_unlock(mddev); |
| 5228 | } else { |
| 5229 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5230 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5231 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | list_del_init(&rdev->same_set); |
| 5233 | if (bind_rdev_to_array(rdev, mddev)) |
| 5234 | export_rdev(rdev); |
| 5235 | } |
| 5236 | autorun_array(mddev); |
| 5237 | mddev_unlock(mddev); |
| 5238 | } |
| 5239 | /* on success, candidates will be empty, on error |
| 5240 | * it won't... |
| 5241 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5242 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5243 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | mddev_put(mddev); |
| 5247 | } |
| 5248 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5249 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5250 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | static int get_version(void __user * arg) |
| 5253 | { |
| 5254 | mdu_version_t ver; |
| 5255 | |
| 5256 | ver.major = MD_MAJOR_VERSION; |
| 5257 | ver.minor = MD_MINOR_VERSION; |
| 5258 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5259 | |
| 5260 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5261 | return -EFAULT; |
| 5262 | |
| 5263 | return 0; |
| 5264 | } |
| 5265 | |
| 5266 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5267 | { |
| 5268 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5269 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5272 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5273 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5275 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | failed++; |
| 5277 | else { |
| 5278 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5279 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5280 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | else |
| 5282 | spare++; |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | info.major_version = mddev->major_version; |
| 5287 | info.minor_version = mddev->minor_version; |
| 5288 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5289 | info.ctime = mddev->ctime; |
| 5290 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5291 | info.size = mddev->dev_sectors / 2; |
| 5292 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5293 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | info.nr_disks = nr; |
| 5295 | info.raid_disks = mddev->raid_disks; |
| 5296 | info.md_minor = mddev->md_minor; |
| 5297 | info.not_persistent= !mddev->persistent; |
| 5298 | |
| 5299 | info.utime = mddev->utime; |
| 5300 | info.state = 0; |
| 5301 | if (mddev->in_sync) |
| 5302 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5303 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5304 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5305 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | info.working_disks = working; |
| 5307 | info.failed_disks = failed; |
| 5308 | info.spare_disks = spare; |
| 5309 | |
| 5310 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5311 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | |
| 5313 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5314 | return -EFAULT; |
| 5315 | |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5319 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5320 | { |
| 5321 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5322 | char *ptr, *buf = NULL; |
| 5323 | int err = -ENOMEM; |
| 5324 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5325 | if (md_allow_write(mddev)) |
| 5326 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5327 | else |
| 5328 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5329 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5330 | if (!file) |
| 5331 | goto out; |
| 5332 | |
| 5333 | /* bitmap disabled, zero the first byte and copy out */ |
| 5334 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5335 | file->pathname[0] = '\0'; |
| 5336 | goto copy_out; |
| 5337 | } |
| 5338 | |
| 5339 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5340 | if (!buf) |
| 5341 | goto out; |
| 5342 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5343 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5344 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5345 | goto out; |
| 5346 | |
| 5347 | strcpy(file->pathname, ptr); |
| 5348 | |
| 5349 | copy_out: |
| 5350 | err = 0; |
| 5351 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5352 | err = -EFAULT; |
| 5353 | out: |
| 5354 | kfree(buf); |
| 5355 | kfree(file); |
| 5356 | return err; |
| 5357 | } |
| 5358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5360 | { |
| 5361 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | mdk_rdev_t *rdev; |
| 5363 | |
| 5364 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5365 | return -EFAULT; |
| 5366 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5367 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | if (rdev) { |
| 5369 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5370 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5371 | info.raid_disk = rdev->raid_disk; |
| 5372 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5373 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5375 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5377 | info.state |= (1<<MD_DISK_SYNC); |
| 5378 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5379 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5380 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | } else { |
| 5382 | info.major = info.minor = 0; |
| 5383 | info.raid_disk = -1; |
| 5384 | info.state = (1<<MD_DISK_REMOVED); |
| 5385 | } |
| 5386 | |
| 5387 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5388 | return -EFAULT; |
| 5389 | |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5394 | { |
| 5395 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5396 | mdk_rdev_t *rdev; |
| 5397 | dev_t dev = MKDEV(info->major,info->minor); |
| 5398 | |
| 5399 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5400 | return -EOVERFLOW; |
| 5401 | |
| 5402 | if (!mddev->raid_disks) { |
| 5403 | int err; |
| 5404 | /* expecting a device which has a superblock */ |
| 5405 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5406 | if (IS_ERR(rdev)) { |
| 5407 | printk(KERN_WARNING |
| 5408 | "md: md_import_device returned %ld\n", |
| 5409 | PTR_ERR(rdev)); |
| 5410 | return PTR_ERR(rdev); |
| 5411 | } |
| 5412 | if (!list_empty(&mddev->disks)) { |
| 5413 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5414 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5415 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5417 | if (err < 0) { |
| 5418 | printk(KERN_WARNING |
| 5419 | "md: %s has different UUID to %s\n", |
| 5420 | bdevname(rdev->bdev,b), |
| 5421 | bdevname(rdev0->bdev,b2)); |
| 5422 | export_rdev(rdev); |
| 5423 | return -EINVAL; |
| 5424 | } |
| 5425 | } |
| 5426 | err = bind_rdev_to_array(rdev, mddev); |
| 5427 | if (err) |
| 5428 | export_rdev(rdev); |
| 5429 | return err; |
| 5430 | } |
| 5431 | |
| 5432 | /* |
| 5433 | * add_new_disk can be used once the array is assembled |
| 5434 | * to add "hot spares". They must already have a superblock |
| 5435 | * written |
| 5436 | */ |
| 5437 | if (mddev->pers) { |
| 5438 | int err; |
| 5439 | if (!mddev->pers->hot_add_disk) { |
| 5440 | printk(KERN_WARNING |
| 5441 | "%s: personality does not support diskops!\n", |
| 5442 | mdname(mddev)); |
| 5443 | return -EINVAL; |
| 5444 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5445 | if (mddev->persistent) |
| 5446 | rdev = md_import_device(dev, mddev->major_version, |
| 5447 | mddev->minor_version); |
| 5448 | else |
| 5449 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | if (IS_ERR(rdev)) { |
| 5451 | printk(KERN_WARNING |
| 5452 | "md: md_import_device returned %ld\n", |
| 5453 | PTR_ERR(rdev)); |
| 5454 | return PTR_ERR(rdev); |
| 5455 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5456 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5457 | if (!mddev->persistent) { |
| 5458 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5459 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5460 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5461 | set_bit(In_sync, &rdev->flags); |
| 5462 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5463 | rdev->raid_disk = -1; |
| 5464 | } else |
| 5465 | super_types[mddev->major_version]. |
| 5466 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5467 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5468 | (!test_bit(In_sync, &rdev->flags) || |
| 5469 | rdev->raid_disk != info->raid_disk)) { |
| 5470 | /* This was a hot-add request, but events doesn't |
| 5471 | * match, so reject it. |
| 5472 | */ |
| 5473 | export_rdev(rdev); |
| 5474 | return -EINVAL; |
| 5475 | } |
| 5476 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5477 | if (test_bit(In_sync, &rdev->flags)) |
| 5478 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5479 | else |
| 5480 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5481 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5482 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5483 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5484 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5485 | else |
| 5486 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | rdev->raid_disk = -1; |
| 5489 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5490 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5491 | /* If there is hot_add_disk but no hot_remove_disk |
| 5492 | * then added disks for geometry changes, |
| 5493 | * and should be added immediately. |
| 5494 | */ |
| 5495 | super_types[mddev->major_version]. |
| 5496 | validate_super(mddev, rdev); |
| 5497 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5498 | if (err) |
| 5499 | unbind_rdev_from_array(rdev); |
| 5500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | if (err) |
| 5502 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5503 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5504 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5505 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5506 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5507 | if (mddev->degraded) |
| 5508 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5509 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5510 | if (!err) |
| 5511 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5512 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | return err; |
| 5514 | } |
| 5515 | |
| 5516 | /* otherwise, add_new_disk is only allowed |
| 5517 | * for major_version==0 superblocks |
| 5518 | */ |
| 5519 | if (mddev->major_version != 0) { |
| 5520 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5521 | mdname(mddev)); |
| 5522 | return -EINVAL; |
| 5523 | } |
| 5524 | |
| 5525 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5526 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5527 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | if (IS_ERR(rdev)) { |
| 5529 | printk(KERN_WARNING |
| 5530 | "md: error, md_import_device() returned %ld\n", |
| 5531 | PTR_ERR(rdev)); |
| 5532 | return PTR_ERR(rdev); |
| 5533 | } |
| 5534 | rdev->desc_nr = info->number; |
| 5535 | if (info->raid_disk < mddev->raid_disks) |
| 5536 | rdev->raid_disk = info->raid_disk; |
| 5537 | else |
| 5538 | rdev->raid_disk = -1; |
| 5539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5541 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5542 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5544 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5545 | set_bit(WriteMostly, &rdev->flags); |
| 5546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | if (!mddev->persistent) { |
| 5548 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5549 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5550 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5551 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5552 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5554 | err = bind_rdev_to_array(rdev, mddev); |
| 5555 | if (err) { |
| 5556 | export_rdev(rdev); |
| 5557 | return err; |
| 5558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
| 5564 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5565 | { |
| 5566 | char b[BDEVNAME_SIZE]; |
| 5567 | mdk_rdev_t *rdev; |
| 5568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | rdev = find_rdev(mddev, dev); |
| 5570 | if (!rdev) |
| 5571 | return -ENXIO; |
| 5572 | |
| 5573 | if (rdev->raid_disk >= 0) |
| 5574 | goto busy; |
| 5575 | |
| 5576 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5577 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5578 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | |
| 5580 | return 0; |
| 5581 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5582 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5584 | return -EBUSY; |
| 5585 | } |
| 5586 | |
| 5587 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5588 | { |
| 5589 | char b[BDEVNAME_SIZE]; |
| 5590 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | mdk_rdev_t *rdev; |
| 5592 | |
| 5593 | if (!mddev->pers) |
| 5594 | return -ENODEV; |
| 5595 | |
| 5596 | if (mddev->major_version != 0) { |
| 5597 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5598 | " version-0 superblocks.\n", |
| 5599 | mdname(mddev)); |
| 5600 | return -EINVAL; |
| 5601 | } |
| 5602 | if (!mddev->pers->hot_add_disk) { |
| 5603 | printk(KERN_WARNING |
| 5604 | "%s: personality does not support diskops!\n", |
| 5605 | mdname(mddev)); |
| 5606 | return -EINVAL; |
| 5607 | } |
| 5608 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5609 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | if (IS_ERR(rdev)) { |
| 5611 | printk(KERN_WARNING |
| 5612 | "md: error, md_import_device() returned %ld\n", |
| 5613 | PTR_ERR(rdev)); |
| 5614 | return -EINVAL; |
| 5615 | } |
| 5616 | |
| 5617 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5618 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5620 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5622 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5624 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | printk(KERN_WARNING |
| 5626 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5627 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5628 | err = -EINVAL; |
| 5629 | goto abort_export; |
| 5630 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5631 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5633 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5634 | err = bind_rdev_to_array(rdev, mddev); |
| 5635 | if (err) |
| 5636 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | |
| 5638 | /* |
| 5639 | * The rest should better be atomic, we can have disk failures |
| 5640 | * noticed in interrupt contexts ... |
| 5641 | */ |
| 5642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | rdev->raid_disk = -1; |
| 5644 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5645 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | |
| 5647 | /* |
| 5648 | * Kick recovery, maybe this spare has to be added to the |
| 5649 | * array immediately. |
| 5650 | */ |
| 5651 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5652 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5653 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | return 0; |
| 5655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | abort_export: |
| 5657 | export_rdev(rdev); |
| 5658 | return err; |
| 5659 | } |
| 5660 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5661 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5662 | { |
| 5663 | int err; |
| 5664 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5665 | if (mddev->pers) { |
| 5666 | if (!mddev->pers->quiesce) |
| 5667 | return -EBUSY; |
| 5668 | if (mddev->recovery || mddev->sync_thread) |
| 5669 | return -EBUSY; |
| 5670 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5671 | } |
| 5672 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5673 | |
| 5674 | if (fd >= 0) { |
| 5675 | if (mddev->bitmap) |
| 5676 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5677 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5678 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5679 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5680 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5681 | mdname(mddev)); |
| 5682 | return -EBADF; |
| 5683 | } |
| 5684 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5685 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5686 | if (err) { |
| 5687 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5688 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5689 | fput(mddev->bitmap_info.file); |
| 5690 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5691 | return err; |
| 5692 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5693 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5694 | } else if (mddev->bitmap == NULL) |
| 5695 | return -ENOENT; /* cannot remove what isn't there */ |
| 5696 | err = 0; |
| 5697 | if (mddev->pers) { |
| 5698 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5699 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5700 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5701 | if (!err) |
| 5702 | err = bitmap_load(mddev); |
| 5703 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5704 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5705 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5706 | fd = -1; /* make sure to put the file */ |
| 5707 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5708 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5709 | } |
| 5710 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5711 | if (mddev->bitmap_info.file) { |
| 5712 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5713 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5714 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5715 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5716 | } |
| 5717 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5718 | return err; |
| 5719 | } |
| 5720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | /* |
| 5722 | * set_array_info is used two different ways |
| 5723 | * The original usage is when creating a new array. |
| 5724 | * In this usage, raid_disks is > 0 and it together with |
| 5725 | * level, size, not_persistent,layout,chunksize determine the |
| 5726 | * shape of the array. |
| 5727 | * This will always create an array with a type-0.90.0 superblock. |
| 5728 | * The newer usage is when assembling an array. |
| 5729 | * In this case raid_disks will be 0, and the major_version field is |
| 5730 | * use to determine which style super-blocks are to be found on the devices. |
| 5731 | * The minor and patch _version numbers are also kept incase the |
| 5732 | * super_block handler wishes to interpret them. |
| 5733 | */ |
| 5734 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5735 | { |
| 5736 | |
| 5737 | if (info->raid_disks == 0) { |
| 5738 | /* just setting version number for superblock loading */ |
| 5739 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5740 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | super_types[info->major_version].name == NULL) { |
| 5742 | /* maybe try to auto-load a module? */ |
| 5743 | printk(KERN_INFO |
| 5744 | "md: superblock version %d not known\n", |
| 5745 | info->major_version); |
| 5746 | return -EINVAL; |
| 5747 | } |
| 5748 | mddev->major_version = info->major_version; |
| 5749 | mddev->minor_version = info->minor_version; |
| 5750 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5751 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5752 | /* ensure mddev_put doesn't delete this now that there |
| 5753 | * is some minimal configuration. |
| 5754 | */ |
| 5755 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | return 0; |
| 5757 | } |
| 5758 | mddev->major_version = MD_MAJOR_VERSION; |
| 5759 | mddev->minor_version = MD_MINOR_VERSION; |
| 5760 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5761 | mddev->ctime = get_seconds(); |
| 5762 | |
| 5763 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5764 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5765 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | mddev->raid_disks = info->raid_disks; |
| 5767 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5768 | * openned |
| 5769 | */ |
| 5770 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5771 | mddev->recovery_cp = MaxSector; |
| 5772 | else |
| 5773 | mddev->recovery_cp = 0; |
| 5774 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5775 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | |
| 5777 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5778 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | |
| 5780 | mddev->max_disks = MD_SB_DISKS; |
| 5781 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5782 | if (mddev->persistent) |
| 5783 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5784 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5786 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5787 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5788 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5789 | mddev->reshape_position = MaxSector; |
| 5790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | /* |
| 5792 | * Generate a 128 bit UUID |
| 5793 | */ |
| 5794 | get_random_bytes(mddev->uuid, 16); |
| 5795 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5796 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5797 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5798 | mddev->new_layout = mddev->layout; |
| 5799 | mddev->delta_disks = 0; |
| 5800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | return 0; |
| 5802 | } |
| 5803 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5804 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5805 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5806 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5807 | |
| 5808 | if (mddev->external_size) |
| 5809 | return; |
| 5810 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5811 | mddev->array_sectors = array_sectors; |
| 5812 | } |
| 5813 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5814 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5815 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5816 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5817 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5818 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5819 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5820 | |
| 5821 | if (mddev->pers->resize == NULL) |
| 5822 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5823 | /* The "num_sectors" is the number of sectors of each device that |
| 5824 | * is used. This can only make sense for arrays with redundancy. |
| 5825 | * linear and raid0 always use whatever space is available. We can only |
| 5826 | * consider changing this number if no resync or reconstruction is |
| 5827 | * 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] | 5828 | * 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] | 5829 | * of each device. If num_sectors is zero, we find the largest size |
| 5830 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5831 | */ |
| 5832 | if (mddev->sync_thread) |
| 5833 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5834 | if (mddev->bitmap) |
| 5835 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5836 | * grow, and re-add. |
| 5837 | */ |
| 5838 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5839 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5840 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5841 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5842 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5843 | num_sectors = avail; |
| 5844 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5845 | return -ENOSPC; |
| 5846 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5847 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5848 | if (!rv) |
| 5849 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5850 | return rv; |
| 5851 | } |
| 5852 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5853 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5854 | { |
| 5855 | int rv; |
| 5856 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5857 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5858 | return -EINVAL; |
| 5859 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5860 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5861 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5862 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5863 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5864 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5865 | |
| 5866 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5867 | if (rv < 0) |
| 5868 | mddev->delta_disks = 0; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5869 | return rv; |
| 5870 | } |
| 5871 | |
| 5872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | /* |
| 5874 | * update_array_info is used to change the configuration of an |
| 5875 | * on-line array. |
| 5876 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5877 | * fields in the info are checked against the array. |
| 5878 | * Any differences that cannot be handled will cause an error. |
| 5879 | * Normally, only one change can be managed at a time. |
| 5880 | */ |
| 5881 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5882 | { |
| 5883 | int rv = 0; |
| 5884 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5885 | int state = 0; |
| 5886 | |
| 5887 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5888 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5889 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | |
| 5891 | if (mddev->major_version != info->major_version || |
| 5892 | mddev->minor_version != info->minor_version || |
| 5893 | /* mddev->patch_version != info->patch_version || */ |
| 5894 | mddev->ctime != info->ctime || |
| 5895 | mddev->level != info->level || |
| 5896 | /* mddev->layout != info->layout || */ |
| 5897 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5898 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5899 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5900 | ((state^info->state) & 0xfffffe00) |
| 5901 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | return -EINVAL; |
| 5903 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5904 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5905 | cnt++; |
| 5906 | if (mddev->raid_disks != info->raid_disks) |
| 5907 | cnt++; |
| 5908 | if (mddev->layout != info->layout) |
| 5909 | cnt++; |
| 5910 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5911 | cnt++; |
| 5912 | if (cnt == 0) |
| 5913 | return 0; |
| 5914 | if (cnt > 1) |
| 5915 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | |
| 5917 | if (mddev->layout != info->layout) { |
| 5918 | /* Change layout |
| 5919 | * we don't need to do anything at the md level, the |
| 5920 | * personality will take care of it all. |
| 5921 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5922 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5924 | else { |
| 5925 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5926 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5927 | if (rv) |
| 5928 | mddev->new_layout = mddev->layout; |
| 5929 | return rv; |
| 5930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5932 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5933 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5935 | if (mddev->raid_disks != info->raid_disks) |
| 5936 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5938 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5939 | if (mddev->pers->quiesce == NULL) |
| 5940 | return -EINVAL; |
| 5941 | if (mddev->recovery || mddev->sync_thread) |
| 5942 | return -EBUSY; |
| 5943 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5944 | /* add the bitmap */ |
| 5945 | if (mddev->bitmap) |
| 5946 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5947 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5948 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5949 | mddev->bitmap_info.offset = |
| 5950 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5951 | mddev->pers->quiesce(mddev, 1); |
| 5952 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5953 | if (!rv) |
| 5954 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5955 | if (rv) |
| 5956 | bitmap_destroy(mddev); |
| 5957 | mddev->pers->quiesce(mddev, 0); |
| 5958 | } else { |
| 5959 | /* remove the bitmap */ |
| 5960 | if (!mddev->bitmap) |
| 5961 | return -ENOENT; |
| 5962 | if (mddev->bitmap->file) |
| 5963 | return -EINVAL; |
| 5964 | mddev->pers->quiesce(mddev, 1); |
| 5965 | bitmap_destroy(mddev); |
| 5966 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5967 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5968 | } |
| 5969 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5970 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | return rv; |
| 5972 | } |
| 5973 | |
| 5974 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5975 | { |
| 5976 | mdk_rdev_t *rdev; |
| 5977 | |
| 5978 | if (mddev->pers == NULL) |
| 5979 | return -ENODEV; |
| 5980 | |
| 5981 | rdev = find_rdev(mddev, dev); |
| 5982 | if (!rdev) |
| 5983 | return -ENODEV; |
| 5984 | |
| 5985 | md_error(mddev, rdev); |
| 5986 | return 0; |
| 5987 | } |
| 5988 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5989 | /* |
| 5990 | * We have a problem here : there is no easy way to give a CHS |
| 5991 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5992 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5993 | * dosfs just mad... ;-) |
| 5994 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5995 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5996 | { |
| 5997 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5998 | |
| 5999 | geo->heads = 2; |
| 6000 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6001 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
| 6004 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6005 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | unsigned int cmd, unsigned long arg) |
| 6007 | { |
| 6008 | int err = 0; |
| 6009 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6011 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | |
| 6013 | if (!capable(CAP_SYS_ADMIN)) |
| 6014 | return -EACCES; |
| 6015 | |
| 6016 | /* |
| 6017 | * Commands dealing with the RAID driver but not any |
| 6018 | * particular array: |
| 6019 | */ |
| 6020 | switch (cmd) |
| 6021 | { |
| 6022 | case RAID_VERSION: |
| 6023 | err = get_version(argp); |
| 6024 | goto done; |
| 6025 | |
| 6026 | case PRINT_RAID_DEBUG: |
| 6027 | err = 0; |
| 6028 | md_print_devices(); |
| 6029 | goto done; |
| 6030 | |
| 6031 | #ifndef MODULE |
| 6032 | case RAID_AUTORUN: |
| 6033 | err = 0; |
| 6034 | autostart_arrays(arg); |
| 6035 | goto done; |
| 6036 | #endif |
| 6037 | default:; |
| 6038 | } |
| 6039 | |
| 6040 | /* |
| 6041 | * Commands creating/starting a new array: |
| 6042 | */ |
| 6043 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6044 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | |
| 6046 | if (!mddev) { |
| 6047 | BUG(); |
| 6048 | goto abort; |
| 6049 | } |
| 6050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | err = mddev_lock(mddev); |
| 6052 | if (err) { |
| 6053 | printk(KERN_INFO |
| 6054 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6055 | err, cmd); |
| 6056 | goto abort; |
| 6057 | } |
| 6058 | |
| 6059 | switch (cmd) |
| 6060 | { |
| 6061 | case SET_ARRAY_INFO: |
| 6062 | { |
| 6063 | mdu_array_info_t info; |
| 6064 | if (!arg) |
| 6065 | memset(&info, 0, sizeof(info)); |
| 6066 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6067 | err = -EFAULT; |
| 6068 | goto abort_unlock; |
| 6069 | } |
| 6070 | if (mddev->pers) { |
| 6071 | err = update_array_info(mddev, &info); |
| 6072 | if (err) { |
| 6073 | printk(KERN_WARNING "md: couldn't update" |
| 6074 | " array info. %d\n", err); |
| 6075 | goto abort_unlock; |
| 6076 | } |
| 6077 | goto done_unlock; |
| 6078 | } |
| 6079 | if (!list_empty(&mddev->disks)) { |
| 6080 | printk(KERN_WARNING |
| 6081 | "md: array %s already has disks!\n", |
| 6082 | mdname(mddev)); |
| 6083 | err = -EBUSY; |
| 6084 | goto abort_unlock; |
| 6085 | } |
| 6086 | if (mddev->raid_disks) { |
| 6087 | printk(KERN_WARNING |
| 6088 | "md: array %s already initialised!\n", |
| 6089 | mdname(mddev)); |
| 6090 | err = -EBUSY; |
| 6091 | goto abort_unlock; |
| 6092 | } |
| 6093 | err = set_array_info(mddev, &info); |
| 6094 | if (err) { |
| 6095 | printk(KERN_WARNING "md: couldn't set" |
| 6096 | " array info. %d\n", err); |
| 6097 | goto abort_unlock; |
| 6098 | } |
| 6099 | } |
| 6100 | goto done_unlock; |
| 6101 | |
| 6102 | default:; |
| 6103 | } |
| 6104 | |
| 6105 | /* |
| 6106 | * Commands querying/configuring an existing array: |
| 6107 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6108 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6109 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6110 | if ((!mddev->raid_disks && !mddev->external) |
| 6111 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6112 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6113 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | err = -ENODEV; |
| 6115 | goto abort_unlock; |
| 6116 | } |
| 6117 | |
| 6118 | /* |
| 6119 | * Commands even a read-only array can execute: |
| 6120 | */ |
| 6121 | switch (cmd) |
| 6122 | { |
| 6123 | case GET_ARRAY_INFO: |
| 6124 | err = get_array_info(mddev, argp); |
| 6125 | goto done_unlock; |
| 6126 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6127 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6128 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6129 | goto done_unlock; |
| 6130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | case GET_DISK_INFO: |
| 6132 | err = get_disk_info(mddev, argp); |
| 6133 | goto done_unlock; |
| 6134 | |
| 6135 | case RESTART_ARRAY_RW: |
| 6136 | err = restart_array(mddev); |
| 6137 | goto done_unlock; |
| 6138 | |
| 6139 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6140 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | goto done_unlock; |
| 6142 | |
| 6143 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6144 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | goto done_unlock; |
| 6146 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6147 | case BLKROSET: |
| 6148 | if (get_user(ro, (int __user *)(arg))) { |
| 6149 | err = -EFAULT; |
| 6150 | goto done_unlock; |
| 6151 | } |
| 6152 | err = -EINVAL; |
| 6153 | |
| 6154 | /* if the bdev is going readonly the value of mddev->ro |
| 6155 | * does not matter, no writes are coming |
| 6156 | */ |
| 6157 | if (ro) |
| 6158 | goto done_unlock; |
| 6159 | |
| 6160 | /* are we are already prepared for writes? */ |
| 6161 | if (mddev->ro != 1) |
| 6162 | goto done_unlock; |
| 6163 | |
| 6164 | /* transitioning to readauto need only happen for |
| 6165 | * arrays that call md_write_start |
| 6166 | */ |
| 6167 | if (mddev->pers) { |
| 6168 | err = restart_array(mddev); |
| 6169 | if (err == 0) { |
| 6170 | mddev->ro = 2; |
| 6171 | set_disk_ro(mddev->gendisk, 0); |
| 6172 | } |
| 6173 | } |
| 6174 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | /* |
| 6178 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6179 | * superblock, so we do not allow them on read-only arrays. |
| 6180 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6181 | * here and hit the 'default' below, so only disallow |
| 6182 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6184 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6185 | if (mddev->ro == 2) { |
| 6186 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6187 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6188 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6189 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6190 | } else { |
| 6191 | err = -EROFS; |
| 6192 | goto abort_unlock; |
| 6193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | switch (cmd) |
| 6197 | { |
| 6198 | case ADD_NEW_DISK: |
| 6199 | { |
| 6200 | mdu_disk_info_t info; |
| 6201 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6202 | err = -EFAULT; |
| 6203 | else |
| 6204 | err = add_new_disk(mddev, &info); |
| 6205 | goto done_unlock; |
| 6206 | } |
| 6207 | |
| 6208 | case HOT_REMOVE_DISK: |
| 6209 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6210 | goto done_unlock; |
| 6211 | |
| 6212 | case HOT_ADD_DISK: |
| 6213 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6214 | goto done_unlock; |
| 6215 | |
| 6216 | case SET_DISK_FAULTY: |
| 6217 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6218 | goto done_unlock; |
| 6219 | |
| 6220 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6221 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | goto done_unlock; |
| 6223 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6224 | case SET_BITMAP_FILE: |
| 6225 | err = set_bitmap_file(mddev, (int)arg); |
| 6226 | goto done_unlock; |
| 6227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | err = -EINVAL; |
| 6230 | goto abort_unlock; |
| 6231 | } |
| 6232 | |
| 6233 | done_unlock: |
| 6234 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6235 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6236 | err != -EINVAL) |
| 6237 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | mddev_unlock(mddev); |
| 6239 | |
| 6240 | return err; |
| 6241 | done: |
| 6242 | if (err) |
| 6243 | MD_BUG(); |
| 6244 | abort: |
| 6245 | return err; |
| 6246 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6247 | #ifdef CONFIG_COMPAT |
| 6248 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6249 | unsigned int cmd, unsigned long arg) |
| 6250 | { |
| 6251 | switch (cmd) { |
| 6252 | case HOT_REMOVE_DISK: |
| 6253 | case HOT_ADD_DISK: |
| 6254 | case SET_DISK_FAULTY: |
| 6255 | case SET_BITMAP_FILE: |
| 6256 | /* These take in integer arg, do not convert */ |
| 6257 | break; |
| 6258 | default: |
| 6259 | arg = (unsigned long)compat_ptr(arg); |
| 6260 | break; |
| 6261 | } |
| 6262 | |
| 6263 | return md_ioctl(bdev, mode, cmd, arg); |
| 6264 | } |
| 6265 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6267 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | { |
| 6269 | /* |
| 6270 | * Succeed if we can lock the mddev, which confirms that |
| 6271 | * it isn't being stopped right now. |
| 6272 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6273 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | int err; |
| 6275 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6276 | if (mddev->gendisk != bdev->bd_disk) { |
| 6277 | /* we are racing with mddev_put which is discarding this |
| 6278 | * bd_disk. |
| 6279 | */ |
| 6280 | mddev_put(mddev); |
| 6281 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6282 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6283 | /* Then retry the open from the top */ |
| 6284 | return -ERESTARTSYS; |
| 6285 | } |
| 6286 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6287 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6288 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | goto out; |
| 6290 | |
| 6291 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6292 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6293 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6295 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6296 | out: |
| 6297 | return err; |
| 6298 | } |
| 6299 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6300 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6302 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6303 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6304 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6305 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | mddev_put(mddev); |
| 6307 | |
| 6308 | return 0; |
| 6309 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6310 | |
| 6311 | static int md_media_changed(struct gendisk *disk) |
| 6312 | { |
| 6313 | mddev_t *mddev = disk->private_data; |
| 6314 | |
| 6315 | return mddev->changed; |
| 6316 | } |
| 6317 | |
| 6318 | static int md_revalidate(struct gendisk *disk) |
| 6319 | { |
| 6320 | mddev_t *mddev = disk->private_data; |
| 6321 | |
| 6322 | mddev->changed = 0; |
| 6323 | return 0; |
| 6324 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6325 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | { |
| 6327 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6328 | .open = md_open, |
| 6329 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6330 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6331 | #ifdef CONFIG_COMPAT |
| 6332 | .compat_ioctl = md_compat_ioctl, |
| 6333 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6334 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6335 | .media_changed = md_media_changed, |
| 6336 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | }; |
| 6338 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6339 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | { |
| 6341 | mdk_thread_t *thread = arg; |
| 6342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | /* |
| 6344 | * md_thread is a 'system-thread', it's priority should be very |
| 6345 | * high. We avoid resource deadlocks individually in each |
| 6346 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6347 | * the very same RT priority as kswapd, thus we will never get |
| 6348 | * into a priority inversion deadlock. |
| 6349 | * |
| 6350 | * we definitely have to have equal or higher priority than |
| 6351 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6352 | * many dirty RAID5 blocks. |
| 6353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6355 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6356 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6358 | /* We need to wait INTERRUPTIBLE so that |
| 6359 | * we don't add to the load-average. |
| 6360 | * That means we need to be sure no signals are |
| 6361 | * pending |
| 6362 | */ |
| 6363 | if (signal_pending(current)) |
| 6364 | flush_signals(current); |
| 6365 | |
| 6366 | wait_event_interruptible_timeout |
| 6367 | (thread->wqueue, |
| 6368 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6369 | || kthread_should_stop(), |
| 6370 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6372 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6373 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6374 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | return 0; |
| 6378 | } |
| 6379 | |
| 6380 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6381 | { |
| 6382 | if (thread) { |
| 6383 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6384 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6385 | wake_up(&thread->wqueue); |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6390 | const char *name) |
| 6391 | { |
| 6392 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6394 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | if (!thread) |
| 6396 | return NULL; |
| 6397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | init_waitqueue_head(&thread->wqueue); |
| 6399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | thread->run = run; |
| 6401 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6402 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6403 | thread->tsk = kthread_run(md_thread, thread, |
| 6404 | "%s_%s", |
| 6405 | mdname(thread->mddev), |
| 6406 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6407 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | kfree(thread); |
| 6409 | return NULL; |
| 6410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | return thread; |
| 6412 | } |
| 6413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | void md_unregister_thread(mdk_thread_t *thread) |
| 6415 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6416 | if (!thread) |
| 6417 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6418 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6419 | |
| 6420 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | kfree(thread); |
| 6422 | } |
| 6423 | |
| 6424 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6425 | { |
| 6426 | if (!mddev) { |
| 6427 | MD_BUG(); |
| 6428 | return; |
| 6429 | } |
| 6430 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6431 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6433 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6434 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | return; |
| 6436 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6437 | if (mddev->degraded) |
| 6438 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6439 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6441 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6442 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6443 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6444 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6445 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | } |
| 6447 | |
| 6448 | /* seq_file implementation /proc/mdstat */ |
| 6449 | |
| 6450 | static void status_unused(struct seq_file *seq) |
| 6451 | { |
| 6452 | int i = 0; |
| 6453 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | |
| 6455 | seq_printf(seq, "unused devices: "); |
| 6456 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6457 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | char b[BDEVNAME_SIZE]; |
| 6459 | i++; |
| 6460 | seq_printf(seq, "%s ", |
| 6461 | bdevname(rdev->bdev,b)); |
| 6462 | } |
| 6463 | if (!i) |
| 6464 | seq_printf(seq, "<none>"); |
| 6465 | |
| 6466 | seq_printf(seq, "\n"); |
| 6467 | } |
| 6468 | |
| 6469 | |
| 6470 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6471 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6472 | sector_t max_sectors, resync, res; |
| 6473 | unsigned long dt, db; |
| 6474 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6475 | int scale; |
| 6476 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6478 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | |
| 6480 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6481 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6483 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | |
| 6485 | /* |
| 6486 | * Should not happen. |
| 6487 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6488 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | MD_BUG(); |
| 6490 | return; |
| 6491 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6492 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6493 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6494 | * u32, as those are the requirements for sector_div. |
| 6495 | * Thus 'scale' must be at least 10 |
| 6496 | */ |
| 6497 | scale = 10; |
| 6498 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6499 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6500 | scale++; |
| 6501 | } |
| 6502 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6503 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6504 | |
| 6505 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6507 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | seq_printf(seq, "["); |
| 6509 | for (i = 0; i < x; i++) |
| 6510 | seq_printf(seq, "="); |
| 6511 | seq_printf(seq, ">"); |
| 6512 | for (i = 0; i < y; i++) |
| 6513 | seq_printf(seq, "."); |
| 6514 | seq_printf(seq, "] "); |
| 6515 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6516 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6517 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6518 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6519 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6520 | "check" : |
| 6521 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6522 | "resync" : "recovery"))), |
| 6523 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6524 | (unsigned long long) resync/2, |
| 6525 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | |
| 6527 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | * dt: time from mark until now |
| 6529 | * db: blocks written from mark until now |
| 6530 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6531 | * |
| 6532 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6533 | * So we divide before multiply in case it is 32bit and close |
| 6534 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6535 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6536 | * near the end of resync when the number of remaining sectors |
| 6537 | * is close to 'db'. |
| 6538 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6539 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | */ |
| 6541 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6542 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6543 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6544 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6546 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6547 | sector_div(rt, db/32+1); |
| 6548 | rt *= dt; |
| 6549 | rt >>= 5; |
| 6550 | |
| 6551 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6552 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6554 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6558 | { |
| 6559 | struct list_head *tmp; |
| 6560 | loff_t l = *pos; |
| 6561 | mddev_t *mddev; |
| 6562 | |
| 6563 | if (l >= 0x10000) |
| 6564 | return NULL; |
| 6565 | if (!l--) |
| 6566 | /* header */ |
| 6567 | return (void*)1; |
| 6568 | |
| 6569 | spin_lock(&all_mddevs_lock); |
| 6570 | list_for_each(tmp,&all_mddevs) |
| 6571 | if (!l--) { |
| 6572 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6573 | mddev_get(mddev); |
| 6574 | spin_unlock(&all_mddevs_lock); |
| 6575 | return mddev; |
| 6576 | } |
| 6577 | spin_unlock(&all_mddevs_lock); |
| 6578 | if (!l--) |
| 6579 | return (void*)2;/* tail */ |
| 6580 | return NULL; |
| 6581 | } |
| 6582 | |
| 6583 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6584 | { |
| 6585 | struct list_head *tmp; |
| 6586 | mddev_t *next_mddev, *mddev = v; |
| 6587 | |
| 6588 | ++*pos; |
| 6589 | if (v == (void*)2) |
| 6590 | return NULL; |
| 6591 | |
| 6592 | spin_lock(&all_mddevs_lock); |
| 6593 | if (v == (void*)1) |
| 6594 | tmp = all_mddevs.next; |
| 6595 | else |
| 6596 | tmp = mddev->all_mddevs.next; |
| 6597 | if (tmp != &all_mddevs) |
| 6598 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6599 | else { |
| 6600 | next_mddev = (void*)2; |
| 6601 | *pos = 0x10000; |
| 6602 | } |
| 6603 | spin_unlock(&all_mddevs_lock); |
| 6604 | |
| 6605 | if (v != (void*)1) |
| 6606 | mddev_put(mddev); |
| 6607 | return next_mddev; |
| 6608 | |
| 6609 | } |
| 6610 | |
| 6611 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6612 | { |
| 6613 | mddev_t *mddev = v; |
| 6614 | |
| 6615 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6616 | mddev_put(mddev); |
| 6617 | } |
| 6618 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6619 | struct mdstat_info { |
| 6620 | int event; |
| 6621 | }; |
| 6622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6623 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6624 | { |
| 6625 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6626 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6628 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6629 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | |
| 6631 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6632 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | seq_printf(seq, "Personalities : "); |
| 6634 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6635 | list_for_each_entry(pers, &pers_list, list) |
| 6636 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | |
| 6638 | spin_unlock(&pers_lock); |
| 6639 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6640 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | return 0; |
| 6642 | } |
| 6643 | if (v == (void*)2) { |
| 6644 | status_unused(seq); |
| 6645 | return 0; |
| 6646 | } |
| 6647 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6648 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6652 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6653 | mddev->pers ? "" : "in"); |
| 6654 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6655 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6657 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6658 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6659 | seq_printf(seq, " %s", mddev->pers->name); |
| 6660 | } |
| 6661 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6662 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6663 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | char b[BDEVNAME_SIZE]; |
| 6665 | seq_printf(seq, " %s[%d]", |
| 6666 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6667 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6668 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6669 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | seq_printf(seq, "(F)"); |
| 6671 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6672 | } else if (rdev->raid_disk < 0) |
| 6673 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6674 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | if (!list_empty(&mddev->disks)) { |
| 6678 | if (mddev->pers) |
| 6679 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6680 | (unsigned long long) |
| 6681 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | else |
| 6683 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6684 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6686 | if (mddev->persistent) { |
| 6687 | if (mddev->major_version != 0 || |
| 6688 | mddev->minor_version != 90) { |
| 6689 | seq_printf(seq," super %d.%d", |
| 6690 | mddev->major_version, |
| 6691 | mddev->minor_version); |
| 6692 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6693 | } else if (mddev->external) |
| 6694 | seq_printf(seq, " super external:%s", |
| 6695 | mddev->metadata_type); |
| 6696 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6697 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | |
| 6699 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6700 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6702 | if (mddev->pers->sync_request) { |
| 6703 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6704 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6705 | seq_printf(seq, "\n "); |
| 6706 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6707 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6708 | else if (mddev->recovery_cp < MaxSector) |
| 6709 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6710 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6711 | } else |
| 6712 | seq_printf(seq, "\n "); |
| 6713 | |
| 6714 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6715 | unsigned long chunk_kb; |
| 6716 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6717 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6718 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6719 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6720 | "%lu%s chunk", |
| 6721 | bitmap->pages - bitmap->missing_pages, |
| 6722 | bitmap->pages, |
| 6723 | (bitmap->pages - bitmap->missing_pages) |
| 6724 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6725 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6726 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6727 | if (bitmap->file) { |
| 6728 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6729 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6730 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6731 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6732 | seq_printf(seq, "\n"); |
| 6733 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | seq_printf(seq, "\n"); |
| 6737 | } |
| 6738 | mddev_unlock(mddev); |
| 6739 | |
| 6740 | return 0; |
| 6741 | } |
| 6742 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6743 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | .start = md_seq_start, |
| 6745 | .next = md_seq_next, |
| 6746 | .stop = md_seq_stop, |
| 6747 | .show = md_seq_show, |
| 6748 | }; |
| 6749 | |
| 6750 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6751 | { |
| 6752 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6753 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6754 | if (mi == NULL) |
| 6755 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | |
| 6757 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6758 | if (error) |
| 6759 | kfree(mi); |
| 6760 | else { |
| 6761 | struct seq_file *p = file->private_data; |
| 6762 | p->private = mi; |
| 6763 | mi->event = atomic_read(&md_event_count); |
| 6764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | return error; |
| 6766 | } |
| 6767 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6768 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6769 | { |
| 6770 | struct seq_file *m = filp->private_data; |
| 6771 | struct mdstat_info *mi = m->private; |
| 6772 | int mask; |
| 6773 | |
| 6774 | poll_wait(filp, &md_event_waiters, wait); |
| 6775 | |
| 6776 | /* always allow read */ |
| 6777 | mask = POLLIN | POLLRDNORM; |
| 6778 | |
| 6779 | if (mi->event != atomic_read(&md_event_count)) |
| 6780 | mask |= POLLERR | POLLPRI; |
| 6781 | return mask; |
| 6782 | } |
| 6783 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6784 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6785 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | .open = md_seq_open, |
| 6787 | .read = seq_read, |
| 6788 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6789 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6790 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6791 | }; |
| 6792 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6793 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6796 | list_add_tail(&p->list, &pers_list); |
| 6797 | 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] | 6798 | spin_unlock(&pers_lock); |
| 6799 | return 0; |
| 6800 | } |
| 6801 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6802 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6804 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6806 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | spin_unlock(&pers_lock); |
| 6808 | return 0; |
| 6809 | } |
| 6810 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6811 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | { |
| 6813 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6815 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | |
| 6817 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6818 | rcu_read_lock(); |
| 6819 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6821 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6822 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6823 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6824 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6825 | * as sync_io is counted when a request starts, and |
| 6826 | * disk_stats is counted when it completes. |
| 6827 | * So resync activity will cause curr_events to be smaller than |
| 6828 | * when there was no such activity. |
| 6829 | * non-sync IO will cause disk_stat to increase without |
| 6830 | * increasing sync_io so curr_events will (eventually) |
| 6831 | * be larger than it was before. Once it becomes |
| 6832 | * substantially larger, the test below will cause |
| 6833 | * the array to appear non-idle, and resync will slow |
| 6834 | * down. |
| 6835 | * If there is a lot of outstanding resync activity when |
| 6836 | * we set last_event to curr_events, then all that activity |
| 6837 | * completing might cause the array to appear non-idle |
| 6838 | * and resync will be slowed down even though there might |
| 6839 | * not have been non-resync activity. This will only |
| 6840 | * happen once though. 'last_events' will soon reflect |
| 6841 | * the state where there is little or no outstanding |
| 6842 | * resync requests, and further resync activity will |
| 6843 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6844 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6846 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | rdev->last_events = curr_events; |
| 6848 | idle = 0; |
| 6849 | } |
| 6850 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6851 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | return idle; |
| 6853 | } |
| 6854 | |
| 6855 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6856 | { |
| 6857 | /* another "blocks" (512byte) blocks have been synced */ |
| 6858 | atomic_sub(blocks, &mddev->recovery_active); |
| 6859 | wake_up(&mddev->recovery_wait); |
| 6860 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6861 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | md_wakeup_thread(mddev->thread); |
| 6863 | // stop recovery, signal do_sync .... |
| 6864 | } |
| 6865 | } |
| 6866 | |
| 6867 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6868 | /* md_write_start(mddev, bi) |
| 6869 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6870 | * in superblock) before writing, schedule a superblock update |
| 6871 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6872 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6873 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6875 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6876 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6877 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6878 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6879 | BUG_ON(mddev->ro == 1); |
| 6880 | if (mddev->ro == 2) { |
| 6881 | /* need to switch to read/write */ |
| 6882 | mddev->ro = 0; |
| 6883 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6884 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6885 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6886 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6887 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6888 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6889 | if (mddev->safemode == 1) |
| 6890 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6891 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6892 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6893 | if (mddev->in_sync) { |
| 6894 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6895 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6896 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6897 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6898 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6899 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6900 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6901 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6902 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6903 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6904 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6905 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | void md_write_end(mddev_t *mddev) |
| 6909 | { |
| 6910 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6911 | if (mddev->safemode == 2) |
| 6912 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6913 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6915 | } |
| 6916 | } |
| 6917 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6918 | /* md_allow_write(mddev) |
| 6919 | * Calling this ensures that the array is marked 'active' so that writes |
| 6920 | * may proceed without blocking. It is important to call this before |
| 6921 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6922 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6923 | * |
| 6924 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6925 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6926 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6927 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6928 | { |
| 6929 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6930 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6931 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6932 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6933 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6934 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6935 | |
| 6936 | spin_lock_irq(&mddev->write_lock); |
| 6937 | if (mddev->in_sync) { |
| 6938 | mddev->in_sync = 0; |
| 6939 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6940 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6941 | if (mddev->safemode_delay && |
| 6942 | mddev->safemode == 0) |
| 6943 | mddev->safemode = 1; |
| 6944 | spin_unlock_irq(&mddev->write_lock); |
| 6945 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6946 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6947 | } else |
| 6948 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6949 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6950 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6951 | return -EAGAIN; |
| 6952 | else |
| 6953 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6954 | } |
| 6955 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | #define SYNC_MARKS 10 |
| 6958 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6959 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | { |
| 6961 | mddev_t *mddev2; |
| 6962 | unsigned int currspeed = 0, |
| 6963 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6964 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | unsigned long mark[SYNC_MARKS]; |
| 6966 | sector_t mark_cnt[SYNC_MARKS]; |
| 6967 | int last_mark,m; |
| 6968 | struct list_head *tmp; |
| 6969 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6970 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6971 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6972 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | |
| 6974 | /* just incase thread restarts... */ |
| 6975 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6976 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6977 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6978 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6980 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6981 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6982 | desc = "data-check"; |
| 6983 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6984 | desc = "requested-resync"; |
| 6985 | else |
| 6986 | desc = "resync"; |
| 6987 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6988 | desc = "reshape"; |
| 6989 | else |
| 6990 | desc = "recovery"; |
| 6991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | /* we overload curr_resync somewhat here. |
| 6993 | * 0 == not engaged in resync at all |
| 6994 | * 2 == checking that there is no conflict with another sync |
| 6995 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6996 | * commense |
| 6997 | * other == active in resync - this many blocks |
| 6998 | * |
| 6999 | * Before starting a resync we must have set curr_resync to |
| 7000 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7001 | * less than ours. When we find one that is the same or higher |
| 7002 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7003 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7004 | * This will mean we have to start checking from the beginning again. |
| 7005 | * |
| 7006 | */ |
| 7007 | |
| 7008 | do { |
| 7009 | mddev->curr_resync = 2; |
| 7010 | |
| 7011 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7012 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7013 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7014 | |
| 7015 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7017 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | if (mddev2 == mddev) |
| 7019 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7020 | if (!mddev->parallel_resync |
| 7021 | && mddev2->curr_resync |
| 7022 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | DEFINE_WAIT(wq); |
| 7024 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7025 | /* arbitrarily yield */ |
| 7026 | mddev->curr_resync = 1; |
| 7027 | wake_up(&resync_wait); |
| 7028 | } |
| 7029 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7030 | /* no need to wait here, we can wait the next |
| 7031 | * time 'round when curr_resync == 2 |
| 7032 | */ |
| 7033 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7034 | /* We need to wait 'interruptible' so as not to |
| 7035 | * contribute to the load average, and not to |
| 7036 | * be caught by 'softlockup' |
| 7037 | */ |
| 7038 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 7039 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7040 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7041 | printk(KERN_INFO "md: delaying %s of %s" |
| 7042 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7044 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7046 | if (signal_pending(current)) |
| 7047 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | schedule(); |
| 7049 | finish_wait(&resync_wait, &wq); |
| 7050 | goto try_again; |
| 7051 | } |
| 7052 | finish_wait(&resync_wait, &wq); |
| 7053 | } |
| 7054 | } |
| 7055 | } while (mddev->curr_resync < 2); |
| 7056 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7057 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7058 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7060 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | */ |
| 7062 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7063 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7064 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7065 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7066 | j = mddev->resync_min; |
| 7067 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7068 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7069 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7070 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7071 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7072 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7074 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7075 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7076 | rcu_read_lock(); |
| 7077 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7078 | if (rdev->raid_disk >= 0 && |
| 7079 | !test_bit(Faulty, &rdev->flags) && |
| 7080 | !test_bit(In_sync, &rdev->flags) && |
| 7081 | rdev->recovery_offset < j) |
| 7082 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7083 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7086 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7087 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7088 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7089 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7090 | "(but not more than %d KB/sec) for %s.\n", |
| 7091 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7092 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7093 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7094 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7095 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7097 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7098 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7099 | } |
| 7100 | last_mark = 0; |
| 7101 | mddev->resync_mark = mark[last_mark]; |
| 7102 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7103 | |
| 7104 | /* |
| 7105 | * Tune reconstruction: |
| 7106 | */ |
| 7107 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7108 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7109 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | |
| 7111 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | last_check = 0; |
| 7113 | |
| 7114 | if (j>2) { |
| 7115 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7116 | "md: resuming %s of %s from checkpoint.\n", |
| 7117 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | mddev->curr_resync = j; |
| 7119 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7120 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | |
| 7122 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7123 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7125 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7126 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7127 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7128 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7129 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7130 | > (max_sectors >> 4)) || |
| 7131 | (j - mddev->curr_resync_completed)*2 |
| 7132 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7133 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7134 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7135 | wait_event(mddev->recovery_wait, |
| 7136 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7137 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7138 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7139 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7140 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7141 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7142 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7143 | /* As this condition is controlled by user-space, |
| 7144 | * we can block indefinitely, so use '_interruptible' |
| 7145 | * to avoid triggering warnings. |
| 7146 | */ |
| 7147 | flush_signals(current); /* just in case */ |
| 7148 | wait_event_interruptible(mddev->recovery_wait, |
| 7149 | mddev->resync_max > j |
| 7150 | || kthread_should_stop()); |
| 7151 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7152 | |
| 7153 | if (kthread_should_stop()) |
| 7154 | goto interrupted; |
| 7155 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7156 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7157 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7158 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7159 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | goto out; |
| 7161 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7162 | |
| 7163 | if (!skipped) { /* actual IO requested */ |
| 7164 | io_sectors += sectors; |
| 7165 | atomic_add(sectors, &mddev->recovery_active); |
| 7166 | } |
| 7167 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame^] | 7168 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7169 | break; |
| 7170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | j += sectors; |
| 7172 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7173 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7174 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame^] | 7175 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7176 | * visible in /proc/mdstat |
| 7177 | */ |
| 7178 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7179 | |
| 7180 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | continue; |
| 7182 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7183 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7184 | repeat: |
| 7185 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7186 | /* step marks */ |
| 7187 | int next = (last_mark+1) % SYNC_MARKS; |
| 7188 | |
| 7189 | mddev->resync_mark = mark[next]; |
| 7190 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7191 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7192 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | last_mark = next; |
| 7194 | } |
| 7195 | |
| 7196 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7197 | if (kthread_should_stop()) |
| 7198 | goto interrupted; |
| 7199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | |
| 7201 | /* |
| 7202 | * this loop exits only if either when we are slower than |
| 7203 | * the 'hard' speed limit, or the system was IO-idle for |
| 7204 | * a jiffy. |
| 7205 | * the system might be non-idle CPU-wise, but we only care |
| 7206 | * about not overloading the IO subsystem. (things like an |
| 7207 | * e2fsck being done on the RAID array should execute fast) |
| 7208 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | cond_resched(); |
| 7210 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7211 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7212 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7214 | if (currspeed > speed_min(mddev)) { |
| 7215 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7216 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7217 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7218 | goto repeat; |
| 7219 | } |
| 7220 | } |
| 7221 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7222 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | /* |
| 7224 | * this also signals 'finished resyncing' to md_stop |
| 7225 | */ |
| 7226 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7228 | |
| 7229 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7230 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7232 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7233 | mddev->curr_resync > 2) { |
| 7234 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7235 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7236 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7237 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7238 | "md: checkpointing %s of %s.\n", |
| 7239 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7240 | mddev->recovery_cp = mddev->curr_resync; |
| 7241 | } |
| 7242 | } else |
| 7243 | mddev->recovery_cp = MaxSector; |
| 7244 | } else { |
| 7245 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7246 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7247 | rcu_read_lock(); |
| 7248 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7249 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7250 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7251 | !test_bit(Faulty, &rdev->flags) && |
| 7252 | !test_bit(In_sync, &rdev->flags) && |
| 7253 | rdev->recovery_offset < mddev->curr_resync) |
| 7254 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7255 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7257 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7258 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7261 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7262 | /* We completed so min/max setting can be forgotten if used. */ |
| 7263 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7264 | mddev->resync_min = 0; |
| 7265 | mddev->resync_max = MaxSector; |
| 7266 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7267 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | mddev->curr_resync = 0; |
| 7269 | wake_up(&resync_wait); |
| 7270 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7271 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7272 | return; |
| 7273 | |
| 7274 | interrupted: |
| 7275 | /* |
| 7276 | * got a signal, exit. |
| 7277 | */ |
| 7278 | printk(KERN_INFO |
| 7279 | "md: md_do_sync() got signal ... exiting\n"); |
| 7280 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7281 | goto out; |
| 7282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7284 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7286 | static int remove_and_add_spares(mddev_t *mddev) |
| 7287 | { |
| 7288 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7289 | int spares = 0; |
| 7290 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7291 | mddev->curr_resync_completed = 0; |
| 7292 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7293 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7294 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7295 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7296 | (test_bit(Faulty, &rdev->flags) || |
| 7297 | ! test_bit(In_sync, &rdev->flags)) && |
| 7298 | atomic_read(&rdev->nr_pending)==0) { |
| 7299 | if (mddev->pers->hot_remove_disk( |
| 7300 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7301 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7302 | rdev->raid_disk = -1; |
| 7303 | } |
| 7304 | } |
| 7305 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7306 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7307 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7308 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7309 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7310 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7311 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7312 | if (rdev->raid_disk < 0 |
| 7313 | && !test_bit(Faulty, &rdev->flags)) { |
| 7314 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7315 | if (mddev->pers-> |
| 7316 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7317 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7318 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7319 | spares++; |
| 7320 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7321 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7322 | } else |
| 7323 | break; |
| 7324 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7325 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7326 | } |
| 7327 | return spares; |
| 7328 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7329 | |
| 7330 | static void reap_sync_thread(mddev_t *mddev) |
| 7331 | { |
| 7332 | mdk_rdev_t *rdev; |
| 7333 | |
| 7334 | /* resync has finished, collect result */ |
| 7335 | md_unregister_thread(mddev->sync_thread); |
| 7336 | mddev->sync_thread = NULL; |
| 7337 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7338 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7339 | /* success...*/ |
| 7340 | /* activate any spares */ |
| 7341 | if (mddev->pers->spare_active(mddev)) |
| 7342 | sysfs_notify(&mddev->kobj, NULL, |
| 7343 | "degraded"); |
| 7344 | } |
| 7345 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7346 | mddev->pers->finish_reshape) |
| 7347 | mddev->pers->finish_reshape(mddev); |
| 7348 | md_update_sb(mddev, 1); |
| 7349 | |
| 7350 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7351 | * information must be scrapped |
| 7352 | */ |
| 7353 | if (!mddev->degraded) |
| 7354 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7355 | rdev->saved_raid_disk = -1; |
| 7356 | |
| 7357 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7358 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7359 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7360 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7361 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7362 | /* flag recovery needed just to double check */ |
| 7363 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7364 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7365 | md_new_event(mddev); |
Jonathan Brassow | 768e587e | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7366 | if (mddev->event_work.func) |
| 7367 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7368 | } |
| 7369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | /* |
| 7371 | * This routine is regularly called by all per-raid-array threads to |
| 7372 | * deal with generic issues like resync and super-block update. |
| 7373 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7374 | * need this as they never do any recovery or update the superblock. |
| 7375 | * |
| 7376 | * It does not do any resync itself, but rather "forks" off other threads |
| 7377 | * to do that as needed. |
| 7378 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7379 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7380 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | * and wakeups up this thread which will reap the thread and finish up. |
| 7382 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7383 | * |
| 7384 | * The overall approach is: |
| 7385 | * 1/ if the superblock needs updating, update it. |
| 7386 | * 2/ If a recovery thread is running, don't do anything else. |
| 7387 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7388 | * 4/ If there are any faulty devices, remove them. |
| 7389 | * 5/ If array is degraded, try to add spares devices |
| 7390 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7391 | */ |
| 7392 | void md_check_recovery(mddev_t *mddev) |
| 7393 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7394 | if (mddev->suspended) |
| 7395 | return; |
| 7396 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7397 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7398 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7400 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7401 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7402 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7403 | mdname(mddev)); |
| 7404 | mddev->safemode = 2; |
| 7405 | } |
| 7406 | flush_signals(current); |
| 7407 | } |
| 7408 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7409 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7410 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7412 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7414 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7415 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7416 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7417 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | )) |
| 7419 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7420 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7421 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7422 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7423 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7424 | if (mddev->ro) { |
| 7425 | /* Only thing we do on a ro array is remove |
| 7426 | * failed devices. |
| 7427 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7428 | mdk_rdev_t *rdev; |
| 7429 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7430 | if (rdev->raid_disk >= 0 && |
| 7431 | !test_bit(Blocked, &rdev->flags) && |
| 7432 | test_bit(Faulty, &rdev->flags) && |
| 7433 | atomic_read(&rdev->nr_pending)==0) { |
| 7434 | if (mddev->pers->hot_remove_disk( |
| 7435 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7436 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7437 | rdev->raid_disk = -1; |
| 7438 | } |
| 7439 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7440 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7441 | goto unlock; |
| 7442 | } |
| 7443 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7444 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7445 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7446 | spin_lock_irq(&mddev->write_lock); |
| 7447 | if (mddev->safemode && |
| 7448 | !atomic_read(&mddev->writes_pending) && |
| 7449 | !mddev->in_sync && |
| 7450 | mddev->recovery_cp == MaxSector) { |
| 7451 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7452 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7453 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7454 | } |
| 7455 | if (mddev->safemode == 1) |
| 7456 | mddev->safemode = 0; |
| 7457 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7458 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7459 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7460 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7461 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7462 | if (mddev->flags) |
| 7463 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7466 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7467 | /* resync/recovery still happening */ |
| 7468 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7469 | goto unlock; |
| 7470 | } |
| 7471 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7472 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7473 | goto unlock; |
| 7474 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7475 | /* Set RUNNING before clearing NEEDED to avoid |
| 7476 | * any transients in the value of "sync_action". |
| 7477 | */ |
| 7478 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7479 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7480 | /* Clear some bits that don't mean anything, but |
| 7481 | * might be left set |
| 7482 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7483 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7484 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7485 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7486 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7487 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | /* no recovery is running. |
| 7489 | * remove any failed drives, then |
| 7490 | * add spares if possible. |
| 7491 | * Spare are also removed and re-added, to allow |
| 7492 | * the personality to fail the re-add. |
| 7493 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7495 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7496 | if (mddev->pers->check_reshape == NULL || |
| 7497 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7498 | /* Cannot proceed */ |
| 7499 | goto unlock; |
| 7500 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7501 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7502 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7503 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7504 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7505 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7506 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7507 | } else if (mddev->recovery_cp < MaxSector) { |
| 7508 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7509 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7510 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7511 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7515 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7516 | /* We are adding a device or devices to an array |
| 7517 | * which has the bitmap stored on all devices. |
| 7518 | * So make sure all bitmap pages get written |
| 7519 | */ |
| 7520 | bitmap_write_all(mddev->bitmap); |
| 7521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7523 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7524 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7525 | if (!mddev->sync_thread) { |
| 7526 | printk(KERN_ERR "%s: could not start resync" |
| 7527 | " thread...\n", |
| 7528 | mdname(mddev)); |
| 7529 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7530 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7531 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7532 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7533 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7534 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7535 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7537 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7538 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | } |
| 7540 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7541 | if (!mddev->sync_thread) { |
| 7542 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7543 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7544 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7545 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7546 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7548 | mddev_unlock(mddev); |
| 7549 | } |
| 7550 | } |
| 7551 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7552 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7553 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7554 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7555 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7556 | !test_bit(Blocked, &rdev->flags) && |
| 7557 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7558 | msecs_to_jiffies(5000)); |
| 7559 | rdev_dec_pending(rdev, mddev); |
| 7560 | } |
| 7561 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7562 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7563 | |
| 7564 | /* Bad block management. |
| 7565 | * We can record which blocks on each device are 'bad' and so just |
| 7566 | * fail those blocks, or that stripe, rather than the whole device. |
| 7567 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7568 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7569 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7570 | * A 'shift' can be set so that larger blocks are tracked and |
| 7571 | * consequently larger devices can be covered. |
| 7572 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7573 | * |
| 7574 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7575 | * might need to retry if it is very unlucky. |
| 7576 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7577 | * so we use the write_seqlock_irq variant. |
| 7578 | * |
| 7579 | * When looking for a bad block we specify a range and want to |
| 7580 | * know if any block in the range is bad. So we binary-search |
| 7581 | * to the last range that starts at-or-before the given endpoint, |
| 7582 | * (or "before the sector after the target range") |
| 7583 | * then see if it ends after the given start. |
| 7584 | * We return |
| 7585 | * 0 if there are no known bad blocks in the range |
| 7586 | * 1 if there are known bad block which are all acknowledged |
| 7587 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7588 | * plus the start/length of the first bad section we overlap. |
| 7589 | */ |
| 7590 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7591 | sector_t *first_bad, int *bad_sectors) |
| 7592 | { |
| 7593 | int hi; |
| 7594 | int lo = 0; |
| 7595 | u64 *p = bb->page; |
| 7596 | int rv = 0; |
| 7597 | sector_t target = s + sectors; |
| 7598 | unsigned seq; |
| 7599 | |
| 7600 | if (bb->shift > 0) { |
| 7601 | /* round the start down, and the end up */ |
| 7602 | s >>= bb->shift; |
| 7603 | target += (1<<bb->shift) - 1; |
| 7604 | target >>= bb->shift; |
| 7605 | sectors = target - s; |
| 7606 | } |
| 7607 | /* 'target' is now the first block after the bad range */ |
| 7608 | |
| 7609 | retry: |
| 7610 | seq = read_seqbegin(&bb->lock); |
| 7611 | |
| 7612 | hi = bb->count; |
| 7613 | |
| 7614 | /* Binary search between lo and hi for 'target' |
| 7615 | * i.e. for the last range that starts before 'target' |
| 7616 | */ |
| 7617 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7618 | * are known not to be the last range before target. |
| 7619 | * VARIANT: hi-lo is the number of possible |
| 7620 | * ranges, and decreases until it reaches 1 |
| 7621 | */ |
| 7622 | while (hi - lo > 1) { |
| 7623 | int mid = (lo + hi) / 2; |
| 7624 | sector_t a = BB_OFFSET(p[mid]); |
| 7625 | if (a < target) |
| 7626 | /* This could still be the one, earlier ranges |
| 7627 | * could not. */ |
| 7628 | lo = mid; |
| 7629 | else |
| 7630 | /* This and later ranges are definitely out. */ |
| 7631 | hi = mid; |
| 7632 | } |
| 7633 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7634 | if (hi > lo) { |
| 7635 | /* need to check all range that end after 's' to see if |
| 7636 | * any are unacknowledged. |
| 7637 | */ |
| 7638 | while (lo >= 0 && |
| 7639 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7640 | if (BB_OFFSET(p[lo]) < target) { |
| 7641 | /* starts before the end, and finishes after |
| 7642 | * the start, so they must overlap |
| 7643 | */ |
| 7644 | if (rv != -1 && BB_ACK(p[lo])) |
| 7645 | rv = 1; |
| 7646 | else |
| 7647 | rv = -1; |
| 7648 | *first_bad = BB_OFFSET(p[lo]); |
| 7649 | *bad_sectors = BB_LEN(p[lo]); |
| 7650 | } |
| 7651 | lo--; |
| 7652 | } |
| 7653 | } |
| 7654 | |
| 7655 | if (read_seqretry(&bb->lock, seq)) |
| 7656 | goto retry; |
| 7657 | |
| 7658 | return rv; |
| 7659 | } |
| 7660 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7661 | |
| 7662 | /* |
| 7663 | * Add a range of bad blocks to the table. |
| 7664 | * This might extend the table, or might contract it |
| 7665 | * if two adjacent ranges can be merged. |
| 7666 | * We binary-search to find the 'insertion' point, then |
| 7667 | * decide how best to handle it. |
| 7668 | */ |
| 7669 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7670 | int acknowledged) |
| 7671 | { |
| 7672 | u64 *p; |
| 7673 | int lo, hi; |
| 7674 | int rv = 1; |
| 7675 | |
| 7676 | if (bb->shift < 0) |
| 7677 | /* badblocks are disabled */ |
| 7678 | return 0; |
| 7679 | |
| 7680 | if (bb->shift) { |
| 7681 | /* round the start down, and the end up */ |
| 7682 | sector_t next = s + sectors; |
| 7683 | s >>= bb->shift; |
| 7684 | next += (1<<bb->shift) - 1; |
| 7685 | next >>= bb->shift; |
| 7686 | sectors = next - s; |
| 7687 | } |
| 7688 | |
| 7689 | write_seqlock_irq(&bb->lock); |
| 7690 | |
| 7691 | p = bb->page; |
| 7692 | lo = 0; |
| 7693 | hi = bb->count; |
| 7694 | /* Find the last range that starts at-or-before 's' */ |
| 7695 | while (hi - lo > 1) { |
| 7696 | int mid = (lo + hi) / 2; |
| 7697 | sector_t a = BB_OFFSET(p[mid]); |
| 7698 | if (a <= s) |
| 7699 | lo = mid; |
| 7700 | else |
| 7701 | hi = mid; |
| 7702 | } |
| 7703 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7704 | hi = lo; |
| 7705 | |
| 7706 | if (hi > lo) { |
| 7707 | /* we found a range that might merge with the start |
| 7708 | * of our new range |
| 7709 | */ |
| 7710 | sector_t a = BB_OFFSET(p[lo]); |
| 7711 | sector_t e = a + BB_LEN(p[lo]); |
| 7712 | int ack = BB_ACK(p[lo]); |
| 7713 | if (e >= s) { |
| 7714 | /* Yes, we can merge with a previous range */ |
| 7715 | if (s == a && s + sectors >= e) |
| 7716 | /* new range covers old */ |
| 7717 | ack = acknowledged; |
| 7718 | else |
| 7719 | ack = ack && acknowledged; |
| 7720 | |
| 7721 | if (e < s + sectors) |
| 7722 | e = s + sectors; |
| 7723 | if (e - a <= BB_MAX_LEN) { |
| 7724 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7725 | s = e; |
| 7726 | } else { |
| 7727 | /* does not all fit in one range, |
| 7728 | * make p[lo] maximal |
| 7729 | */ |
| 7730 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7731 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7732 | s = a + BB_MAX_LEN; |
| 7733 | } |
| 7734 | sectors = e - s; |
| 7735 | } |
| 7736 | } |
| 7737 | if (sectors && hi < bb->count) { |
| 7738 | /* 'hi' points to the first range that starts after 's'. |
| 7739 | * Maybe we can merge with the start of that range */ |
| 7740 | sector_t a = BB_OFFSET(p[hi]); |
| 7741 | sector_t e = a + BB_LEN(p[hi]); |
| 7742 | int ack = BB_ACK(p[hi]); |
| 7743 | if (a <= s + sectors) { |
| 7744 | /* merging is possible */ |
| 7745 | if (e <= s + sectors) { |
| 7746 | /* full overlap */ |
| 7747 | e = s + sectors; |
| 7748 | ack = acknowledged; |
| 7749 | } else |
| 7750 | ack = ack && acknowledged; |
| 7751 | |
| 7752 | a = s; |
| 7753 | if (e - a <= BB_MAX_LEN) { |
| 7754 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7755 | s = e; |
| 7756 | } else { |
| 7757 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7758 | s = a + BB_MAX_LEN; |
| 7759 | } |
| 7760 | sectors = e - s; |
| 7761 | lo = hi; |
| 7762 | hi++; |
| 7763 | } |
| 7764 | } |
| 7765 | if (sectors == 0 && hi < bb->count) { |
| 7766 | /* we might be able to combine lo and hi */ |
| 7767 | /* Note: 's' is at the end of 'lo' */ |
| 7768 | sector_t a = BB_OFFSET(p[hi]); |
| 7769 | int lolen = BB_LEN(p[lo]); |
| 7770 | int hilen = BB_LEN(p[hi]); |
| 7771 | int newlen = lolen + hilen - (s - a); |
| 7772 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7773 | /* yes, we can combine them */ |
| 7774 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7775 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7776 | memmove(p + hi, p + hi + 1, |
| 7777 | (bb->count - hi - 1) * 8); |
| 7778 | bb->count--; |
| 7779 | } |
| 7780 | } |
| 7781 | while (sectors) { |
| 7782 | /* didn't merge (it all). |
| 7783 | * Need to add a range just before 'hi' */ |
| 7784 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7785 | /* No room for more */ |
| 7786 | rv = 0; |
| 7787 | break; |
| 7788 | } else { |
| 7789 | int this_sectors = sectors; |
| 7790 | memmove(p + hi + 1, p + hi, |
| 7791 | (bb->count - hi) * 8); |
| 7792 | bb->count++; |
| 7793 | |
| 7794 | if (this_sectors > BB_MAX_LEN) |
| 7795 | this_sectors = BB_MAX_LEN; |
| 7796 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7797 | sectors -= this_sectors; |
| 7798 | s += this_sectors; |
| 7799 | } |
| 7800 | } |
| 7801 | |
| 7802 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7803 | if (!acknowledged) |
| 7804 | bb->unacked_exist = 1; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7805 | write_sequnlock_irq(&bb->lock); |
| 7806 | |
| 7807 | return rv; |
| 7808 | } |
| 7809 | |
| 7810 | int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors, |
| 7811 | int acknowledged) |
| 7812 | { |
| 7813 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7814 | s + rdev->data_offset, sectors, acknowledged); |
| 7815 | if (rv) { |
| 7816 | /* Make sure they get written out promptly */ |
| 7817 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7818 | md_wakeup_thread(rdev->mddev->thread); |
| 7819 | } |
| 7820 | return rv; |
| 7821 | } |
| 7822 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7823 | |
| 7824 | /* |
| 7825 | * Remove a range of bad blocks from the table. |
| 7826 | * This may involve extending the table if we spilt a region, |
| 7827 | * but it must not fail. So if the table becomes full, we just |
| 7828 | * drop the remove request. |
| 7829 | */ |
| 7830 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7831 | { |
| 7832 | u64 *p; |
| 7833 | int lo, hi; |
| 7834 | sector_t target = s + sectors; |
| 7835 | int rv = 0; |
| 7836 | |
| 7837 | if (bb->shift > 0) { |
| 7838 | /* When clearing we round the start up and the end down. |
| 7839 | * This should not matter as the shift should align with |
| 7840 | * the block size and no rounding should ever be needed. |
| 7841 | * However it is better the think a block is bad when it |
| 7842 | * isn't than to think a block is not bad when it is. |
| 7843 | */ |
| 7844 | s += (1<<bb->shift) - 1; |
| 7845 | s >>= bb->shift; |
| 7846 | target >>= bb->shift; |
| 7847 | sectors = target - s; |
| 7848 | } |
| 7849 | |
| 7850 | write_seqlock_irq(&bb->lock); |
| 7851 | |
| 7852 | p = bb->page; |
| 7853 | lo = 0; |
| 7854 | hi = bb->count; |
| 7855 | /* Find the last range that starts before 'target' */ |
| 7856 | while (hi - lo > 1) { |
| 7857 | int mid = (lo + hi) / 2; |
| 7858 | sector_t a = BB_OFFSET(p[mid]); |
| 7859 | if (a < target) |
| 7860 | lo = mid; |
| 7861 | else |
| 7862 | hi = mid; |
| 7863 | } |
| 7864 | if (hi > lo) { |
| 7865 | /* p[lo] is the last range that could overlap the |
| 7866 | * current range. Earlier ranges could also overlap, |
| 7867 | * but only this one can overlap the end of the range. |
| 7868 | */ |
| 7869 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7870 | /* Partial overlap, leave the tail of this range */ |
| 7871 | int ack = BB_ACK(p[lo]); |
| 7872 | sector_t a = BB_OFFSET(p[lo]); |
| 7873 | sector_t end = a + BB_LEN(p[lo]); |
| 7874 | |
| 7875 | if (a < s) { |
| 7876 | /* we need to split this range */ |
| 7877 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7878 | rv = 0; |
| 7879 | goto out; |
| 7880 | } |
| 7881 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7882 | bb->count++; |
| 7883 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7884 | lo++; |
| 7885 | } |
| 7886 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7887 | /* there is no longer an overlap */ |
| 7888 | hi = lo; |
| 7889 | lo--; |
| 7890 | } |
| 7891 | while (lo >= 0 && |
| 7892 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7893 | /* This range does overlap */ |
| 7894 | if (BB_OFFSET(p[lo]) < s) { |
| 7895 | /* Keep the early parts of this range. */ |
| 7896 | int ack = BB_ACK(p[lo]); |
| 7897 | sector_t start = BB_OFFSET(p[lo]); |
| 7898 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7899 | /* now low doesn't overlap, so.. */ |
| 7900 | break; |
| 7901 | } |
| 7902 | lo--; |
| 7903 | } |
| 7904 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7905 | * anything between needs to be discarded |
| 7906 | */ |
| 7907 | if (hi - lo > 1) { |
| 7908 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7909 | bb->count -= (hi - lo - 1); |
| 7910 | } |
| 7911 | } |
| 7912 | |
| 7913 | bb->changed = 1; |
| 7914 | out: |
| 7915 | write_sequnlock_irq(&bb->lock); |
| 7916 | return rv; |
| 7917 | } |
| 7918 | |
| 7919 | int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors) |
| 7920 | { |
| 7921 | return md_clear_badblocks(&rdev->badblocks, |
| 7922 | s + rdev->data_offset, |
| 7923 | sectors); |
| 7924 | } |
| 7925 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 7926 | |
| 7927 | /* |
| 7928 | * Acknowledge all bad blocks in a list. |
| 7929 | * This only succeeds if ->changed is clear. It is used by |
| 7930 | * in-kernel metadata updates |
| 7931 | */ |
| 7932 | void md_ack_all_badblocks(struct badblocks *bb) |
| 7933 | { |
| 7934 | if (bb->page == NULL || bb->changed) |
| 7935 | /* no point even trying */ |
| 7936 | return; |
| 7937 | write_seqlock_irq(&bb->lock); |
| 7938 | |
| 7939 | if (bb->changed == 0) { |
| 7940 | u64 *p = bb->page; |
| 7941 | int i; |
| 7942 | for (i = 0; i < bb->count ; i++) { |
| 7943 | if (!BB_ACK(p[i])) { |
| 7944 | sector_t start = BB_OFFSET(p[i]); |
| 7945 | int len = BB_LEN(p[i]); |
| 7946 | p[i] = BB_MAKE(start, len, 1); |
| 7947 | } |
| 7948 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7949 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7950 | } |
| 7951 | write_sequnlock_irq(&bb->lock); |
| 7952 | } |
| 7953 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 7954 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7955 | /* sysfs access to bad-blocks list. |
| 7956 | * We present two files. |
| 7957 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 7958 | * are recorded as bad. The list is truncated to fit within |
| 7959 | * the one-page limit of sysfs. |
| 7960 | * Writing "sector length" to this file adds an acknowledged |
| 7961 | * bad block list. |
| 7962 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 7963 | * been acknowledged. Writing to this file adds bad blocks |
| 7964 | * without acknowledging them. This is largely for testing. |
| 7965 | */ |
| 7966 | |
| 7967 | static ssize_t |
| 7968 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 7969 | { |
| 7970 | size_t len; |
| 7971 | int i; |
| 7972 | u64 *p = bb->page; |
| 7973 | unsigned seq; |
| 7974 | |
| 7975 | if (bb->shift < 0) |
| 7976 | return 0; |
| 7977 | |
| 7978 | retry: |
| 7979 | seq = read_seqbegin(&bb->lock); |
| 7980 | |
| 7981 | len = 0; |
| 7982 | i = 0; |
| 7983 | |
| 7984 | while (len < PAGE_SIZE && i < bb->count) { |
| 7985 | sector_t s = BB_OFFSET(p[i]); |
| 7986 | unsigned int length = BB_LEN(p[i]); |
| 7987 | int ack = BB_ACK(p[i]); |
| 7988 | i++; |
| 7989 | |
| 7990 | if (unack && ack) |
| 7991 | continue; |
| 7992 | |
| 7993 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 7994 | (unsigned long long)s << bb->shift, |
| 7995 | length << bb->shift); |
| 7996 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7997 | if (unack && len == 0) |
| 7998 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7999 | |
| 8000 | if (read_seqretry(&bb->lock, seq)) |
| 8001 | goto retry; |
| 8002 | |
| 8003 | return len; |
| 8004 | } |
| 8005 | |
| 8006 | #define DO_DEBUG 1 |
| 8007 | |
| 8008 | static ssize_t |
| 8009 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8010 | { |
| 8011 | unsigned long long sector; |
| 8012 | int length; |
| 8013 | char newline; |
| 8014 | #ifdef DO_DEBUG |
| 8015 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8016 | * Normally only a successful write may clear a badblock |
| 8017 | */ |
| 8018 | int clear = 0; |
| 8019 | if (page[0] == '-') { |
| 8020 | clear = 1; |
| 8021 | page++; |
| 8022 | } |
| 8023 | #endif /* DO_DEBUG */ |
| 8024 | |
| 8025 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8026 | case 3: |
| 8027 | if (newline != '\n') |
| 8028 | return -EINVAL; |
| 8029 | case 2: |
| 8030 | if (length <= 0) |
| 8031 | return -EINVAL; |
| 8032 | break; |
| 8033 | default: |
| 8034 | return -EINVAL; |
| 8035 | } |
| 8036 | |
| 8037 | #ifdef DO_DEBUG |
| 8038 | if (clear) { |
| 8039 | md_clear_badblocks(bb, sector, length); |
| 8040 | return len; |
| 8041 | } |
| 8042 | #endif /* DO_DEBUG */ |
| 8043 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8044 | return len; |
| 8045 | else |
| 8046 | return -ENOSPC; |
| 8047 | } |
| 8048 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8049 | static int md_notify_reboot(struct notifier_block *this, |
| 8050 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8051 | { |
| 8052 | struct list_head *tmp; |
| 8053 | mddev_t *mddev; |
| 8054 | |
| 8055 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 8056 | |
| 8057 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 8058 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8059 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8060 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8061 | /* Force a switch to readonly even array |
| 8062 | * appears to still be in use. Hence |
| 8063 | * the '100'. |
| 8064 | */ |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 8065 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 8066 | mddev_unlock(mddev); |
| 8067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8068 | /* |
| 8069 | * certain more exotic SCSI devices are known to be |
| 8070 | * volatile wrt too early system reboots. While the |
| 8071 | * right place to handle this issue is the given |
| 8072 | * driver, we do want to have a safe RAID driver ... |
| 8073 | */ |
| 8074 | mdelay(1000*1); |
| 8075 | } |
| 8076 | return NOTIFY_DONE; |
| 8077 | } |
| 8078 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8079 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8080 | .notifier_call = md_notify_reboot, |
| 8081 | .next = NULL, |
| 8082 | .priority = INT_MAX, /* before any real devices */ |
| 8083 | }; |
| 8084 | |
| 8085 | static void md_geninit(void) |
| 8086 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8087 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 8088 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8089 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8090 | } |
| 8091 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8092 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8093 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8094 | int ret = -ENOMEM; |
| 8095 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8096 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8097 | if (!md_wq) |
| 8098 | goto err_wq; |
| 8099 | |
| 8100 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8101 | if (!md_misc_wq) |
| 8102 | goto err_misc_wq; |
| 8103 | |
| 8104 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8105 | goto err_md; |
| 8106 | |
| 8107 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8108 | goto err_mdp; |
| 8109 | mdp_major = ret; |
| 8110 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8111 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8112 | md_probe, NULL, NULL); |
| 8113 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8114 | md_probe, NULL, NULL); |
| 8115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8116 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8117 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8118 | |
| 8119 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8121 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8122 | err_mdp: |
| 8123 | unregister_blkdev(MD_MAJOR, "md"); |
| 8124 | err_md: |
| 8125 | destroy_workqueue(md_misc_wq); |
| 8126 | err_misc_wq: |
| 8127 | destroy_workqueue(md_wq); |
| 8128 | err_wq: |
| 8129 | return ret; |
| 8130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8131 | |
| 8132 | #ifndef MODULE |
| 8133 | |
| 8134 | /* |
| 8135 | * Searches all registered partitions for autorun RAID arrays |
| 8136 | * at boot time. |
| 8137 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8138 | |
| 8139 | static LIST_HEAD(all_detected_devices); |
| 8140 | struct detected_devices_node { |
| 8141 | struct list_head list; |
| 8142 | dev_t dev; |
| 8143 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8144 | |
| 8145 | void md_autodetect_dev(dev_t dev) |
| 8146 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8147 | struct detected_devices_node *node_detected_dev; |
| 8148 | |
| 8149 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8150 | if (node_detected_dev) { |
| 8151 | node_detected_dev->dev = dev; |
| 8152 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8153 | } else { |
| 8154 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8155 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8157 | } |
| 8158 | |
| 8159 | |
| 8160 | static void autostart_arrays(int part) |
| 8161 | { |
| 8162 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8163 | struct detected_devices_node *node_detected_dev; |
| 8164 | dev_t dev; |
| 8165 | int i_scanned, i_passed; |
| 8166 | |
| 8167 | i_scanned = 0; |
| 8168 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8169 | |
| 8170 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8171 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8172 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8173 | i_scanned++; |
| 8174 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8175 | struct detected_devices_node, list); |
| 8176 | list_del(&node_detected_dev->list); |
| 8177 | dev = node_detected_dev->dev; |
| 8178 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8179 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8180 | if (IS_ERR(rdev)) |
| 8181 | continue; |
| 8182 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8183 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8184 | MD_BUG(); |
| 8185 | continue; |
| 8186 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8187 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8188 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8189 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8190 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8191 | |
| 8192 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8193 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8194 | |
| 8195 | autorun_devices(part); |
| 8196 | } |
| 8197 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8198 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8199 | |
| 8200 | static __exit void md_exit(void) |
| 8201 | { |
| 8202 | mddev_t *mddev; |
| 8203 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8204 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8205 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8206 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8207 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8208 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8209 | unregister_blkdev(mdp_major, "mdp"); |
| 8210 | unregister_reboot_notifier(&md_notifier); |
| 8211 | unregister_sysctl_table(raid_table_header); |
| 8212 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8213 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8214 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8215 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8216 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8217 | destroy_workqueue(md_misc_wq); |
| 8218 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8219 | } |
| 8220 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8221 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8222 | module_exit(md_exit) |
| 8223 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8224 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8225 | { |
| 8226 | return sprintf(buffer, "%d", start_readonly); |
| 8227 | } |
| 8228 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8229 | { |
| 8230 | char *e; |
| 8231 | int num = simple_strtoul(val, &e, 10); |
| 8232 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8233 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8234 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8235 | } |
| 8236 | return -EINVAL; |
| 8237 | } |
| 8238 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8239 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8240 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8241 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8242 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8244 | EXPORT_SYMBOL(register_md_personality); |
| 8245 | EXPORT_SYMBOL(unregister_md_personality); |
| 8246 | EXPORT_SYMBOL(md_error); |
| 8247 | EXPORT_SYMBOL(md_done_sync); |
| 8248 | EXPORT_SYMBOL(md_write_start); |
| 8249 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8250 | EXPORT_SYMBOL(md_register_thread); |
| 8251 | EXPORT_SYMBOL(md_unregister_thread); |
| 8252 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 | EXPORT_SYMBOL(md_check_recovery); |
| 8254 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8255 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8256 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8257 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |