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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 219 | * We have a system wide 'event count' that is incremented |
| 220 | * on any 'interesting' event, and readers of /proc/mdstat |
| 221 | * can use 'poll' or 'select' to find out when the event |
| 222 | * count increases. |
| 223 | * |
| 224 | * Events are: |
| 225 | * start array, stop array, error, add device, remove device, |
| 226 | * start build, activate spare |
| 227 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 228 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 229 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 230 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 231 | { |
| 232 | atomic_inc(&md_event_count); |
| 233 | wake_up(&md_event_waiters); |
| 234 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 236 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 237 | /* Alternate version that can be called from interrupts |
| 238 | * when calling sysfs_notify isn't needed. |
| 239 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 240 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 241 | { |
| 242 | atomic_inc(&md_event_count); |
| 243 | wake_up(&md_event_waiters); |
| 244 | } |
| 245 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * Enables to iterate over all existing md arrays |
| 248 | * all_mddevs_lock protects this list. |
| 249 | */ |
| 250 | static LIST_HEAD(all_mddevs); |
| 251 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 252 | |
| 253 | |
| 254 | /* |
| 255 | * iterates through all used mddevs in the system. |
| 256 | * We take care to grab the all_mddevs_lock whenever navigating |
| 257 | * the list, and to always hold a refcount when unlocked. |
| 258 | * Any code which breaks out of this loop while own |
| 259 | * a reference to the current mddev and must mddev_put it. |
| 260 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 261 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | \ |
| 263 | for (({ spin_lock(&all_mddevs_lock); \ |
| 264 | tmp = all_mddevs.next; \ |
| 265 | mddev = NULL;}); \ |
| 266 | ({ if (tmp != &all_mddevs) \ |
| 267 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 268 | spin_unlock(&all_mddevs_lock); \ |
| 269 | if (mddev) mddev_put(mddev); \ |
| 270 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 271 | tmp != &all_mddevs;}); \ |
| 272 | ({ spin_lock(&all_mddevs_lock); \ |
| 273 | tmp = tmp->next;}) \ |
| 274 | ) |
| 275 | |
| 276 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 277 | /* Rather than calling directly into the personality make_request function, |
| 278 | * IO requests come here first so that we can check if the device is |
| 279 | * being suspended pending a reconfiguration. |
| 280 | * We hold a refcount over the call to ->make_request. By the time that |
| 281 | * call has finished, the bio has been linked into some internal structure |
| 282 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 283 | */ |
| 284 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 286 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 287 | mddev_t *mddev = q->queuedata; |
| 288 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 289 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 290 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 291 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 292 | if (mddev == NULL || mddev->pers == NULL |
| 293 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 294 | bio_io_error(bio); |
| 295 | return 0; |
| 296 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 297 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 298 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 299 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 300 | DEFINE_WAIT(__wait); |
| 301 | for (;;) { |
| 302 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 303 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 304 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 305 | break; |
| 306 | rcu_read_unlock(); |
| 307 | schedule(); |
| 308 | rcu_read_lock(); |
| 309 | } |
| 310 | finish_wait(&mddev->sb_wait, &__wait); |
| 311 | } |
| 312 | atomic_inc(&mddev->active_io); |
| 313 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 314 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 315 | /* |
| 316 | * save the sectors now since our bio can |
| 317 | * go away inside make_request |
| 318 | */ |
| 319 | sectors = bio_sectors(bio); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 320 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 321 | |
| 322 | cpu = part_stat_lock(); |
| 323 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 324 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 325 | part_stat_unlock(); |
| 326 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 327 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 328 | wake_up(&mddev->sb_wait); |
| 329 | |
| 330 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 333 | /* mddev_suspend makes sure no new requests are submitted |
| 334 | * to the device, and that any requests that have been submitted |
| 335 | * are completely handled. |
| 336 | * Once ->stop is called and completes, the module will be completely |
| 337 | * unused. |
| 338 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 339 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 340 | { |
| 341 | BUG_ON(mddev->suspended); |
| 342 | mddev->suspended = 1; |
| 343 | synchronize_rcu(); |
| 344 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 345 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 346 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 348 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 349 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 350 | { |
| 351 | mddev->suspended = 0; |
| 352 | wake_up(&mddev->sb_wait); |
| 353 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 354 | |
| 355 | md_wakeup_thread(mddev->thread); |
| 356 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 357 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 358 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 359 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 360 | int mddev_congested(mddev_t *mddev, int bits) |
| 361 | { |
| 362 | return mddev->suspended; |
| 363 | } |
| 364 | EXPORT_SYMBOL(mddev_congested); |
| 365 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 366 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | */ |
| 369 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 370 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | { |
| 372 | mdk_rdev_t *rdev = bio->bi_private; |
| 373 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | |
| 375 | rdev_dec_pending(rdev, mddev); |
| 376 | |
| 377 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 378 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 379 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | } |
| 381 | bio_put(bio); |
| 382 | } |
| 383 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 384 | static void md_submit_flush_data(struct work_struct *ws); |
| 385 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 386 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 388 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | mdk_rdev_t *rdev; |
| 390 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 391 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 392 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 393 | rcu_read_lock(); |
| 394 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 395 | if (rdev->raid_disk >= 0 && |
| 396 | !test_bit(Faulty, &rdev->flags)) { |
| 397 | /* Take two references, one is dropped |
| 398 | * when request finishes, one after |
| 399 | * we reclaim rcu_read_lock |
| 400 | */ |
| 401 | struct bio *bi; |
| 402 | atomic_inc(&rdev->nr_pending); |
| 403 | atomic_inc(&rdev->nr_pending); |
| 404 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 405 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 406 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | bi->bi_private = rdev; |
| 408 | bi->bi_bdev = rdev->bdev; |
| 409 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | rcu_read_lock(); |
| 412 | rdev_dec_pending(rdev, mddev); |
| 413 | } |
| 414 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 415 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 416 | queue_work(md_wq, &mddev->flush_work); |
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_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 422 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | /* an empty barrier - all done */ |
| 426 | bio_endio(bio, 0); |
| 427 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 429 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 432 | |
| 433 | mddev->flush_bio = NULL; |
| 434 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 437 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
| 439 | spin_lock_irq(&mddev->write_lock); |
| 440 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 441 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 442 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 443 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | spin_unlock_irq(&mddev->write_lock); |
| 445 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 446 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 447 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 448 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 449 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 450 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 451 | /* Support for plugging. |
| 452 | * This mirrors the plugging support in request_queue, but does not |
| 453 | * require having a whole queue or request structures. |
| 454 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 455 | * plugged on. This links tot the private plug_handle structure in the |
| 456 | * personality data where we keep a count of the number of outstanding |
| 457 | * plugs so other code can see if a plug is active. |
| 458 | */ |
| 459 | struct md_plug_cb { |
| 460 | struct blk_plug_cb cb; |
| 461 | mddev_t *mddev; |
| 462 | }; |
| 463 | |
| 464 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 465 | { |
| 466 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 467 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 468 | md_wakeup_thread(mdcb->mddev->thread); |
| 469 | kfree(mdcb); |
| 470 | } |
| 471 | |
| 472 | /* Check that an unplug wakeup will come shortly. |
| 473 | * If not, wakeup the md thread immediately |
| 474 | */ |
| 475 | int mddev_check_plugged(mddev_t *mddev) |
| 476 | { |
| 477 | struct blk_plug *plug = current->plug; |
| 478 | struct md_plug_cb *mdcb; |
| 479 | |
| 480 | if (!plug) |
| 481 | return 0; |
| 482 | |
| 483 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 484 | if (mdcb->cb.callback == plugger_unplug && |
| 485 | mdcb->mddev == mddev) { |
| 486 | /* Already on the list, move to top */ |
| 487 | if (mdcb != list_first_entry(&plug->cb_list, |
| 488 | struct md_plug_cb, |
| 489 | cb.list)) |
| 490 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 491 | return 1; |
| 492 | } |
| 493 | } |
| 494 | /* Not currently on the callback list */ |
| 495 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 496 | if (!mdcb) |
| 497 | return 0; |
| 498 | |
| 499 | mdcb->mddev = mddev; |
| 500 | mdcb->cb.callback = plugger_unplug; |
| 501 | atomic_inc(&mddev->plug_cnt); |
| 502 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 503 | return 1; |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 508 | { |
| 509 | atomic_inc(&mddev->active); |
| 510 | return mddev; |
| 511 | } |
| 512 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 513 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | static void mddev_put(mddev_t *mddev) |
| 516 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 517 | struct bio_set *bs = NULL; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 520 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 521 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 522 | mddev->ctime == 0 && !mddev->hold_active) { |
| 523 | /* Array is not configured at all, and not held active, |
| 524 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 526 | bs = mddev->bio_set; |
| 527 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 528 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 529 | /* We did a probe so need to clean up. Call |
| 530 | * queue_work inside the spinlock so that |
| 531 | * flush_workqueue() after mddev_find will |
| 532 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 533 | */ |
| 534 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 535 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 536 | } else |
| 537 | kfree(mddev); |
| 538 | } |
| 539 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 540 | if (bs) |
| 541 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 544 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 545 | { |
| 546 | mutex_init(&mddev->open_mutex); |
| 547 | mutex_init(&mddev->reconfig_mutex); |
| 548 | mutex_init(&mddev->bitmap_info.mutex); |
| 549 | INIT_LIST_HEAD(&mddev->disks); |
| 550 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 551 | init_timer(&mddev->safemode_timer); |
| 552 | atomic_set(&mddev->active, 1); |
| 553 | atomic_set(&mddev->openers, 0); |
| 554 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 555 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 556 | spin_lock_init(&mddev->write_lock); |
| 557 | atomic_set(&mddev->flush_pending, 0); |
| 558 | init_waitqueue_head(&mddev->sb_wait); |
| 559 | init_waitqueue_head(&mddev->recovery_wait); |
| 560 | mddev->reshape_position = MaxSector; |
| 561 | mddev->resync_min = 0; |
| 562 | mddev->resync_max = MaxSector; |
| 563 | mddev->level = LEVEL_NONE; |
| 564 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 565 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | static mddev_t * mddev_find(dev_t unit) |
| 568 | { |
| 569 | mddev_t *mddev, *new = NULL; |
| 570 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 571 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 572 | unit &= ~((1<<MdpMinorShift)-1); |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | retry: |
| 575 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 577 | if (unit) { |
| 578 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 579 | if (mddev->unit == unit) { |
| 580 | mddev_get(mddev); |
| 581 | spin_unlock(&all_mddevs_lock); |
| 582 | kfree(new); |
| 583 | return mddev; |
| 584 | } |
| 585 | |
| 586 | if (new) { |
| 587 | list_add(&new->all_mddevs, &all_mddevs); |
| 588 | spin_unlock(&all_mddevs_lock); |
| 589 | new->hold_active = UNTIL_IOCTL; |
| 590 | return new; |
| 591 | } |
| 592 | } else if (new) { |
| 593 | /* find an unused unit number */ |
| 594 | static int next_minor = 512; |
| 595 | int start = next_minor; |
| 596 | int is_free = 0; |
| 597 | int dev = 0; |
| 598 | while (!is_free) { |
| 599 | dev = MKDEV(MD_MAJOR, next_minor); |
| 600 | next_minor++; |
| 601 | if (next_minor > MINORMASK) |
| 602 | next_minor = 0; |
| 603 | if (next_minor == start) { |
| 604 | /* Oh dear, all in use. */ |
| 605 | spin_unlock(&all_mddevs_lock); |
| 606 | kfree(new); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | is_free = 1; |
| 611 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 612 | if (mddev->unit == dev) { |
| 613 | is_free = 0; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | new->unit = dev; |
| 618 | new->md_minor = MINOR(dev); |
| 619 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | list_add(&new->all_mddevs, &all_mddevs); |
| 621 | spin_unlock(&all_mddevs_lock); |
| 622 | return new; |
| 623 | } |
| 624 | spin_unlock(&all_mddevs_lock); |
| 625 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 626 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (!new) |
| 628 | return NULL; |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | new->unit = unit; |
| 631 | if (MAJOR(unit) == MD_MAJOR) |
| 632 | new->md_minor = MINOR(unit); |
| 633 | else |
| 634 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 635 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 636 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | goto retry; |
| 639 | } |
| 640 | |
| 641 | static inline int mddev_lock(mddev_t * mddev) |
| 642 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 643 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 646 | static inline int mddev_is_locked(mddev_t *mddev) |
| 647 | { |
| 648 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | static inline int mddev_trylock(mddev_t * mddev) |
| 652 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 653 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 656 | static struct attribute_group md_redundancy_group; |
| 657 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 658 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 660 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 661 | /* These cannot be removed under reconfig_mutex as |
| 662 | * an access to the files will try to take reconfig_mutex |
| 663 | * while holding the file unremovable, which leads to |
| 664 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 665 | * So hold set sysfs_active while the remove in happeing, |
| 666 | * and anything else which might set ->to_remove or my |
| 667 | * otherwise change the sysfs namespace will fail with |
| 668 | * -EBUSY if sysfs_active is still set. |
| 669 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 670 | * test it under the same mutex to ensure its correct value |
| 671 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 672 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 673 | struct attribute_group *to_remove = mddev->to_remove; |
| 674 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 675 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 676 | mutex_unlock(&mddev->reconfig_mutex); |
| 677 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 678 | if (mddev->kobj.sd) { |
| 679 | if (to_remove != &md_redundancy_group) |
| 680 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 681 | if (mddev->pers == NULL || |
| 682 | mddev->pers->sync_request == NULL) { |
| 683 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 684 | if (mddev->sysfs_action) |
| 685 | sysfs_put(mddev->sysfs_action); |
| 686 | mddev->sysfs_action = NULL; |
| 687 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 688 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 689 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 690 | } else |
| 691 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 693 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 696 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 698 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 700 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (rdev->desc_nr == nr) |
| 702 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 708 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | mdk_rdev_t *rdev; |
| 710 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 711 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if (rdev->bdev->bd_dev == dev) |
| 713 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return NULL; |
| 716 | } |
| 717 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 718 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 719 | { |
| 720 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 721 | list_for_each_entry(pers, &pers_list, list) { |
| 722 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 723 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 724 | if (strcmp(pers->name, clevel)==0) |
| 725 | return pers; |
| 726 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 727 | return NULL; |
| 728 | } |
| 729 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 730 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 731 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 733 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 734 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 738 | { |
| 739 | if (rdev->sb_page) |
| 740 | MD_BUG(); |
| 741 | |
| 742 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 743 | if (!rdev->sb_page) { |
| 744 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 745 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 752 | { |
| 753 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 754 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | rdev->sb_loaded = 0; |
| 756 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 757 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 758 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 760 | if (rdev->bb_page) { |
| 761 | put_page(rdev->bb_page); |
| 762 | rdev->bb_page = NULL; |
| 763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 767 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 768 | { |
| 769 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 770 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 771 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 772 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 773 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 774 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 775 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 776 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 777 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 778 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 779 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 780 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 781 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 785 | sector_t sector, int size, struct page *page) |
| 786 | { |
| 787 | /* write first size bytes of page to sector of rdev |
| 788 | * Increment mddev->pending_writes before returning |
| 789 | * and decrement it on completion, waking up sb_wait |
| 790 | * if zero is reached. |
| 791 | * If an error occurred, call md_error |
| 792 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 793 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 794 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 795 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 796 | bio->bi_sector = sector; |
| 797 | bio_add_page(bio, page, size, 0); |
| 798 | bio->bi_private = rdev; |
| 799 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 800 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 801 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 802 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | void md_super_wait(mddev_t *mddev) |
| 806 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 807 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 808 | DEFINE_WAIT(wq); |
| 809 | for(;;) { |
| 810 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 811 | if (atomic_read(&mddev->pending_writes)==0) |
| 812 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 813 | schedule(); |
| 814 | } |
| 815 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 816 | } |
| 817 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 818 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
| 822 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 823 | 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] | 824 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 826 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | struct completion event; |
| 828 | int ret; |
| 829 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 830 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 832 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 833 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 834 | if (metadata_op) |
| 835 | bio->bi_sector = sector + rdev->sb_start; |
| 836 | else |
| 837 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | bio_add_page(bio, page, size, 0); |
| 839 | init_completion(&event); |
| 840 | bio->bi_private = &event; |
| 841 | bio->bi_end_io = bi_complete; |
| 842 | submit_bio(rw, bio); |
| 843 | wait_for_completion(&event); |
| 844 | |
| 845 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 846 | bio_put(bio); |
| 847 | return ret; |
| 848 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 849 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 851 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
| 853 | char b[BDEVNAME_SIZE]; |
| 854 | if (!rdev->sb_page) { |
| 855 | MD_BUG(); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | if (rdev->sb_loaded) |
| 859 | return 0; |
| 860 | |
| 861 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 862 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | goto fail; |
| 864 | rdev->sb_loaded = 1; |
| 865 | return 0; |
| 866 | |
| 867 | fail: |
| 868 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 869 | bdevname(rdev->bdev,b)); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 874 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 875 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 876 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 877 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 878 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 882 | { |
| 883 | int ret; |
| 884 | mdp_super_t *tmp1, *tmp2; |
| 885 | |
| 886 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 887 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 888 | |
| 889 | if (!tmp1 || !tmp2) { |
| 890 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 891 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | goto abort; |
| 893 | } |
| 894 | |
| 895 | *tmp1 = *sb1; |
| 896 | *tmp2 = *sb2; |
| 897 | |
| 898 | /* |
| 899 | * nr_disks is not constant |
| 900 | */ |
| 901 | tmp1->nr_disks = 0; |
| 902 | tmp2->nr_disks = 0; |
| 903 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 904 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 906 | kfree(tmp1); |
| 907 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return ret; |
| 909 | } |
| 910 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 911 | |
| 912 | static u32 md_csum_fold(u32 csum) |
| 913 | { |
| 914 | csum = (csum & 0xffff) + (csum >> 16); |
| 915 | return (csum & 0xffff) + (csum >> 16); |
| 916 | } |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 919 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 920 | u64 newcsum = 0; |
| 921 | u32 *sb32 = (u32*)sb; |
| 922 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | unsigned int disk_csum, csum; |
| 924 | |
| 925 | disk_csum = sb->sb_csum; |
| 926 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 927 | |
| 928 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 929 | newcsum += sb32[i]; |
| 930 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 931 | |
| 932 | |
| 933 | #ifdef CONFIG_ALPHA |
| 934 | /* This used to use csum_partial, which was wrong for several |
| 935 | * reasons including that different results are returned on |
| 936 | * different architectures. It isn't critical that we get exactly |
| 937 | * the same return value as before (we always csum_fold before |
| 938 | * testing, and that removes any differences). However as we |
| 939 | * know that csum_partial always returned a 16bit value on |
| 940 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 941 | */ |
| 942 | sb->sb_csum = md_csum_fold(disk_csum); |
| 943 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 945 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return csum; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /* |
| 951 | * Handle superblock details. |
| 952 | * We want to be able to handle multiple superblock formats |
| 953 | * so we have a common interface to them all, and an array of |
| 954 | * different handlers. |
| 955 | * We rely on user-space to write the initial superblock, and support |
| 956 | * reading and updating of superblocks. |
| 957 | * Interface methods are: |
| 958 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 959 | * loads and validates a superblock on dev. |
| 960 | * if refdev != NULL, compare superblocks on both devices |
| 961 | * Return: |
| 962 | * 0 - dev has a superblock that is compatible with refdev |
| 963 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 964 | * so dev should be used as the refdev in future |
| 965 | * -EINVAL superblock incompatible or invalid |
| 966 | * -othererror e.g. -EIO |
| 967 | * |
| 968 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 969 | * Verify that dev is acceptable into mddev. |
| 970 | * The first time, mddev->raid_disks will be 0, and data from |
| 971 | * dev should be merged in. Subsequent calls check that dev |
| 972 | * is new enough. Return 0 or -EINVAL |
| 973 | * |
| 974 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 975 | * Update the superblock for rdev with data in mddev |
| 976 | * This does not write to disc. |
| 977 | * |
| 978 | */ |
| 979 | |
| 980 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 981 | char *name; |
| 982 | struct module *owner; |
| 983 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 984 | int minor_version); |
| 985 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 986 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 987 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 988 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | }; |
| 990 | |
| 991 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 992 | * Check that the given mddev has no bitmap. |
| 993 | * |
| 994 | * This function is called from the run method of all personalities that do not |
| 995 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 996 | * has a bitmap. Otherwise, it returns 0. |
| 997 | * |
| 998 | */ |
| 999 | int md_check_no_bitmap(mddev_t *mddev) |
| 1000 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1001 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1002 | return 0; |
| 1003 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1004 | mdname(mddev), mddev->pers->name); |
| 1005 | return 1; |
| 1006 | } |
| 1007 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1008 | |
| 1009 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | * load_super for 0.90.0 |
| 1011 | */ |
| 1012 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1013 | { |
| 1014 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1015 | mdp_super_t *sb; |
| 1016 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1019 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | * it's at the end of the disk. |
| 1021 | * |
| 1022 | * It also happens to be a multiple of 4Kb. |
| 1023 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1024 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1026 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | if (ret) return ret; |
| 1028 | |
| 1029 | ret = -EINVAL; |
| 1030 | |
| 1031 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1032 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1035 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1036 | b); |
| 1037 | goto abort; |
| 1038 | } |
| 1039 | |
| 1040 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1041 | sb->minor_version < 90 || |
| 1042 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1044 | sb->major_version, sb->minor_version, |
| 1045 | b); |
| 1046 | goto abort; |
| 1047 | } |
| 1048 | |
| 1049 | if (sb->raid_disks <= 0) |
| 1050 | goto abort; |
| 1051 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1052 | 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] | 1053 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1054 | b); |
| 1055 | goto abort; |
| 1056 | } |
| 1057 | |
| 1058 | rdev->preferred_minor = sb->md_minor; |
| 1059 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1060 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame^] | 1061 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | if (sb->level == LEVEL_MULTIPATH) |
| 1064 | rdev->desc_nr = -1; |
| 1065 | else |
| 1066 | rdev->desc_nr = sb->this_disk.number; |
| 1067 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1068 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1070 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1072 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (!uuid_equal(refsb, sb)) { |
| 1074 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1075 | b, bdevname(refdev->bdev,b2)); |
| 1076 | goto abort; |
| 1077 | } |
| 1078 | if (!sb_equal(refsb, sb)) { |
| 1079 | printk(KERN_WARNING "md: %s has same UUID" |
| 1080 | " but different superblock to %s\n", |
| 1081 | b, bdevname(refdev->bdev, b2)); |
| 1082 | goto abort; |
| 1083 | } |
| 1084 | ev1 = md_event(sb); |
| 1085 | ev2 = md_event(refsb); |
| 1086 | if (ev1 > ev2) |
| 1087 | ret = 1; |
| 1088 | else |
| 1089 | ret = 0; |
| 1090 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1091 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1093 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1094 | /* "this cannot possibly happen" ... */ |
| 1095 | ret = -EINVAL; |
| 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | abort: |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * validate_super for 0.90.0 |
| 1103 | */ |
| 1104 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1105 | { |
| 1106 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1107 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1108 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1110 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1111 | clear_bit(Faulty, &rdev->flags); |
| 1112 | clear_bit(In_sync, &rdev->flags); |
| 1113 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | if (mddev->raid_disks == 0) { |
| 1116 | mddev->major_version = 0; |
| 1117 | mddev->minor_version = sb->minor_version; |
| 1118 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1119 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1120 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | mddev->ctime = sb->ctime; |
| 1122 | mddev->utime = sb->utime; |
| 1123 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1124 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | mddev->layout = sb->layout; |
| 1126 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1127 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1128 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1129 | mddev->bitmap_info.offset = 0; |
| 1130 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1132 | if (mddev->minor_version >= 91) { |
| 1133 | mddev->reshape_position = sb->reshape_position; |
| 1134 | mddev->delta_disks = sb->delta_disks; |
| 1135 | mddev->new_level = sb->new_level; |
| 1136 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1137 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1138 | } else { |
| 1139 | mddev->reshape_position = MaxSector; |
| 1140 | mddev->delta_disks = 0; |
| 1141 | mddev->new_level = mddev->level; |
| 1142 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1143 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1147 | mddev->recovery_cp = MaxSector; |
| 1148 | else { |
| 1149 | if (sb->events_hi == sb->cp_events_hi && |
| 1150 | sb->events_lo == sb->cp_events_lo) { |
| 1151 | mddev->recovery_cp = sb->recovery_cp; |
| 1152 | } else |
| 1153 | mddev->recovery_cp = 0; |
| 1154 | } |
| 1155 | |
| 1156 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1157 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1158 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1159 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1160 | |
| 1161 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1162 | |
| 1163 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1164 | mddev->bitmap_info.file == NULL) |
| 1165 | mddev->bitmap_info.offset = |
| 1166 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1167 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1168 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1169 | /* Insist on good event counter while assembling, except |
| 1170 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1172 | if (sb->disks[rdev->desc_nr].state & ( |
| 1173 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1174 | if (ev1 < mddev->events) |
| 1175 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1176 | } else if (mddev->bitmap) { |
| 1177 | /* if adding to array with a bitmap, then we can accept an |
| 1178 | * older device ... but not too old. |
| 1179 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1180 | if (ev1 < mddev->bitmap->events_cleared) |
| 1181 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1182 | } else { |
| 1183 | if (ev1 < mddev->events) |
| 1184 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1185 | return 0; |
| 1186 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | desc = sb->disks + rdev->desc_nr; |
| 1190 | |
| 1191 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1192 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1193 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1194 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1195 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1197 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1198 | /* active but not in sync implies recovery up to |
| 1199 | * reshape position. We don't know exactly where |
| 1200 | * that is, so set to zero for now */ |
| 1201 | if (mddev->minor_version >= 91) { |
| 1202 | rdev->recovery_offset = 0; |
| 1203 | rdev->raid_disk = desc->raid_disk; |
| 1204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1206 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1207 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1208 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1209 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | * sync_super for 0.90.0 |
| 1215 | */ |
| 1216 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1217 | { |
| 1218 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | mdk_rdev_t *rdev2; |
| 1220 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* make rdev->sb match mddev data.. |
| 1224 | * |
| 1225 | * 1/ zero out disks |
| 1226 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1227 | * 3/ any empty disks < next_spare become removed |
| 1228 | * |
| 1229 | * disks[0] gets initialised to REMOVED because |
| 1230 | * we cannot be sure from other fields if it has |
| 1231 | * been initialised or not. |
| 1232 | */ |
| 1233 | int i; |
| 1234 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1235 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1236 | rdev->sb_size = MD_SB_BYTES; |
| 1237 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1238 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | |
| 1240 | memset(sb, 0, sizeof(*sb)); |
| 1241 | |
| 1242 | sb->md_magic = MD_SB_MAGIC; |
| 1243 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | sb->patch_version = mddev->patch_version; |
| 1245 | sb->gvalid_words = 0; /* ignored */ |
| 1246 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1247 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1248 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1249 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1250 | |
| 1251 | sb->ctime = mddev->ctime; |
| 1252 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1253 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | sb->raid_disks = mddev->raid_disks; |
| 1255 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1256 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | sb->utime = mddev->utime; |
| 1258 | sb->state = 0; |
| 1259 | sb->events_hi = (mddev->events>>32); |
| 1260 | sb->events_lo = (u32)mddev->events; |
| 1261 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1262 | if (mddev->reshape_position == MaxSector) |
| 1263 | sb->minor_version = 90; |
| 1264 | else { |
| 1265 | sb->minor_version = 91; |
| 1266 | sb->reshape_position = mddev->reshape_position; |
| 1267 | sb->new_level = mddev->new_level; |
| 1268 | sb->delta_disks = mddev->delta_disks; |
| 1269 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1270 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1271 | } |
| 1272 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if (mddev->in_sync) |
| 1274 | { |
| 1275 | sb->recovery_cp = mddev->recovery_cp; |
| 1276 | sb->cp_events_hi = (mddev->events>>32); |
| 1277 | sb->cp_events_lo = (u32)mddev->events; |
| 1278 | if (mddev->recovery_cp == MaxSector) |
| 1279 | sb->state = (1<< MD_SB_CLEAN); |
| 1280 | } else |
| 1281 | sb->recovery_cp = 0; |
| 1282 | |
| 1283 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1284 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1286 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1287 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1290 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1292 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1293 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1294 | |
| 1295 | if (rdev2->raid_disk >= 0 && |
| 1296 | sb->minor_version >= 91) |
| 1297 | /* we have nowhere to store the recovery_offset, |
| 1298 | * but if it is not below the reshape_position, |
| 1299 | * we can piggy-back on that. |
| 1300 | */ |
| 1301 | is_active = 1; |
| 1302 | if (rdev2->raid_disk < 0 || |
| 1303 | test_bit(Faulty, &rdev2->flags)) |
| 1304 | is_active = 0; |
| 1305 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1306 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1308 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1309 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | d = &sb->disks[rdev2->desc_nr]; |
| 1311 | nr_disks++; |
| 1312 | d->number = rdev2->desc_nr; |
| 1313 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1314 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1315 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | d->raid_disk = rdev2->raid_disk; |
| 1317 | else |
| 1318 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1319 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1321 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1323 | if (test_bit(In_sync, &rdev2->flags)) |
| 1324 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | active++; |
| 1326 | working++; |
| 1327 | } else { |
| 1328 | d->state = 0; |
| 1329 | spare++; |
| 1330 | working++; |
| 1331 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1332 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1333 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1336 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1337 | mdp_disk_t *d = &sb->disks[i]; |
| 1338 | if (d->state == 0 && d->number == 0) { |
| 1339 | d->number = i; |
| 1340 | d->raid_disk = i; |
| 1341 | d->state = (1<<MD_DISK_REMOVED); |
| 1342 | d->state |= (1<<MD_DISK_FAULTY); |
| 1343 | failed++; |
| 1344 | } |
| 1345 | } |
| 1346 | sb->nr_disks = nr_disks; |
| 1347 | sb->active_disks = active; |
| 1348 | sb->working_disks = working; |
| 1349 | sb->failed_disks = failed; |
| 1350 | sb->spare_disks = spare; |
| 1351 | |
| 1352 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1353 | sb->sb_csum = calc_sb_csum(sb); |
| 1354 | } |
| 1355 | |
| 1356 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1357 | * rdev_size_change for 0.90.0 |
| 1358 | */ |
| 1359 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1360 | 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] | 1361 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1362 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1363 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1364 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1366 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1367 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1368 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1369 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1370 | rdev->sb_page); |
| 1371 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1372 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | * version 1 superblock |
| 1378 | */ |
| 1379 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1380 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1382 | __le32 disk_csum; |
| 1383 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | unsigned long long newcsum; |
| 1385 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1386 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | int i; |
| 1388 | |
| 1389 | disk_csum = sb->sb_csum; |
| 1390 | sb->sb_csum = 0; |
| 1391 | newcsum = 0; |
| 1392 | for (i=0; size>=4; size -= 4 ) |
| 1393 | newcsum += le32_to_cpu(*isuper++); |
| 1394 | |
| 1395 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1396 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
| 1398 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1399 | sb->sb_csum = disk_csum; |
| 1400 | return cpu_to_le32(csum); |
| 1401 | } |
| 1402 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1403 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1404 | int acknowledged); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1406 | { |
| 1407 | struct mdp_superblock_1 *sb; |
| 1408 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1409 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1411 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1414 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | * It is always aligned to a 4K boundary and |
| 1416 | * depeding on minor_version, it can be: |
| 1417 | * 0: At least 8K, but less than 12K, from end of device |
| 1418 | * 1: At start of device |
| 1419 | * 2: 4K from start of device. |
| 1420 | */ |
| 1421 | switch(minor_version) { |
| 1422 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1423 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1424 | sb_start -= 8*2; |
| 1425 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1428 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1431 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | break; |
| 1433 | default: |
| 1434 | return -EINVAL; |
| 1435 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1436 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1438 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1439 | * and it is safe to read 4k, so we do that |
| 1440 | */ |
| 1441 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | if (ret) return ret; |
| 1443 | |
| 1444 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1445 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1448 | sb->major_version != cpu_to_le32(1) || |
| 1449 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1450 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1451 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | return -EINVAL; |
| 1453 | |
| 1454 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1455 | printk("md: invalid superblock checksum on %s\n", |
| 1456 | bdevname(rdev->bdev,b)); |
| 1457 | return -EINVAL; |
| 1458 | } |
| 1459 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1460 | printk("md: data_size too small on %s\n", |
| 1461 | bdevname(rdev->bdev,b)); |
| 1462 | return -EINVAL; |
| 1463 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | rdev->preferred_minor = 0xffff; |
| 1466 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1467 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1469 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1470 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1471 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1472 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1473 | |
| 1474 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1475 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1476 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1477 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1478 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1479 | rdev->desc_nr = -1; |
| 1480 | else |
| 1481 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1482 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1483 | if (!rdev->bb_page) { |
| 1484 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1485 | if (!rdev->bb_page) |
| 1486 | return -ENOMEM; |
| 1487 | } |
| 1488 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1489 | rdev->badblocks.count == 0) { |
| 1490 | /* need to load the bad block list. |
| 1491 | * Currently we limit it to one page. |
| 1492 | */ |
| 1493 | s32 offset; |
| 1494 | sector_t bb_sector; |
| 1495 | u64 *bbp; |
| 1496 | int i; |
| 1497 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1498 | if (sectors > (PAGE_SIZE / 512)) |
| 1499 | return -EINVAL; |
| 1500 | offset = le32_to_cpu(sb->bblog_offset); |
| 1501 | if (offset == 0) |
| 1502 | return -EINVAL; |
| 1503 | bb_sector = (long long)offset; |
| 1504 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1505 | rdev->bb_page, READ, true)) |
| 1506 | return -EIO; |
| 1507 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1508 | rdev->badblocks.shift = sb->bblog_shift; |
| 1509 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1510 | u64 bb = le64_to_cpu(*bbp); |
| 1511 | int count = bb & (0x3ff); |
| 1512 | u64 sector = bb >> 10; |
| 1513 | sector <<= sb->bblog_shift; |
| 1514 | count <<= sb->bblog_shift; |
| 1515 | if (bb + 1 == 0) |
| 1516 | break; |
| 1517 | if (md_set_badblocks(&rdev->badblocks, |
| 1518 | sector, count, 1) == 0) |
| 1519 | return -EINVAL; |
| 1520 | } |
| 1521 | } else if (sb->bblog_offset == 0) |
| 1522 | rdev->badblocks.shift = -1; |
| 1523 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1524 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1525 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1526 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1528 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
| 1530 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1531 | sb->level != refsb->level || |
| 1532 | sb->layout != refsb->layout || |
| 1533 | sb->chunksize != refsb->chunksize) { |
| 1534 | printk(KERN_WARNING "md: %s has strangely different" |
| 1535 | " superblock to %s\n", |
| 1536 | bdevname(rdev->bdev,b), |
| 1537 | bdevname(refdev->bdev,b2)); |
| 1538 | return -EINVAL; |
| 1539 | } |
| 1540 | ev1 = le64_to_cpu(sb->events); |
| 1541 | ev2 = le64_to_cpu(refsb->events); |
| 1542 | |
| 1543 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1544 | ret = 1; |
| 1545 | else |
| 1546 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1548 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1549 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1550 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1552 | rdev->sectors = rdev->sb_start; |
| 1553 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1555 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1556 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1557 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1558 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1562 | { |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1563 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1564 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1566 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1567 | clear_bit(Faulty, &rdev->flags); |
| 1568 | clear_bit(In_sync, &rdev->flags); |
| 1569 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (mddev->raid_disks == 0) { |
| 1572 | mddev->major_version = 1; |
| 1573 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1574 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1575 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1577 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1578 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1579 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | mddev->layout = le32_to_cpu(sb->layout); |
| 1581 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1582 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1583 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1584 | mddev->bitmap_info.offset = 0; |
| 1585 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | |
| 1587 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1588 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1589 | |
| 1590 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1591 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1592 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1593 | mddev->bitmap_info.file == NULL ) |
| 1594 | mddev->bitmap_info.offset = |
| 1595 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1596 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1597 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1598 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1599 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1600 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1601 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1602 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1603 | } else { |
| 1604 | mddev->reshape_position = MaxSector; |
| 1605 | mddev->delta_disks = 0; |
| 1606 | mddev->new_level = mddev->level; |
| 1607 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1608 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1611 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1612 | /* Insist of good event counter while assembling, except for |
| 1613 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1615 | if (rdev->desc_nr >= 0 && |
| 1616 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1617 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1618 | if (ev1 < mddev->events) |
| 1619 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1620 | } else if (mddev->bitmap) { |
| 1621 | /* If adding to array with a bitmap, then we can accept an |
| 1622 | * older device, but not too old. |
| 1623 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1624 | if (ev1 < mddev->bitmap->events_cleared) |
| 1625 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1626 | } else { |
| 1627 | if (ev1 < mddev->events) |
| 1628 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1629 | return 0; |
| 1630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1632 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1633 | if (rdev->desc_nr < 0 || |
| 1634 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1635 | role = 0xffff; |
| 1636 | rdev->desc_nr = -1; |
| 1637 | } else |
| 1638 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | switch(role) { |
| 1640 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | break; |
| 1642 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1643 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | break; |
| 1645 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1646 | if ((le32_to_cpu(sb->feature_map) & |
| 1647 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1648 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1649 | else |
| 1650 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | rdev->raid_disk = role; |
| 1652 | break; |
| 1653 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1654 | if (sb->devflags & WriteMostly1) |
| 1655 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1656 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1657 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1663 | { |
| 1664 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | mdk_rdev_t *rdev2; |
| 1666 | int max_dev, i; |
| 1667 | /* make rdev->sb match mddev and rdev data. */ |
| 1668 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1669 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
| 1671 | sb->feature_map = 0; |
| 1672 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1673 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1676 | |
| 1677 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1678 | sb->events = cpu_to_le64(mddev->events); |
| 1679 | if (mddev->in_sync) |
| 1680 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1681 | else |
| 1682 | sb->resync_offset = cpu_to_le64(0); |
| 1683 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1684 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1685 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1686 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1687 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1688 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1689 | sb->level = cpu_to_le32(mddev->level); |
| 1690 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1691 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1692 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1693 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1694 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1695 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1696 | |
| 1697 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1698 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1699 | sb->feature_map |= |
| 1700 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1701 | sb->recovery_offset = |
| 1702 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1703 | } |
| 1704 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1705 | if (mddev->reshape_position != MaxSector) { |
| 1706 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1707 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1708 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1709 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1710 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1711 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1712 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1713 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1714 | if (rdev->badblocks.count == 0) |
| 1715 | /* Nothing to do for bad blocks*/ ; |
| 1716 | else if (sb->bblog_offset == 0) |
| 1717 | /* Cannot record bad blocks on this device */ |
| 1718 | md_error(mddev, rdev); |
| 1719 | else { |
| 1720 | struct badblocks *bb = &rdev->badblocks; |
| 1721 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1722 | u64 *p = bb->page; |
| 1723 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1724 | if (bb->changed) { |
| 1725 | unsigned seq; |
| 1726 | |
| 1727 | retry: |
| 1728 | seq = read_seqbegin(&bb->lock); |
| 1729 | |
| 1730 | memset(bbp, 0xff, PAGE_SIZE); |
| 1731 | |
| 1732 | for (i = 0 ; i < bb->count ; i++) { |
| 1733 | u64 internal_bb = *p++; |
| 1734 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1735 | | BB_LEN(internal_bb)); |
| 1736 | *bbp++ = cpu_to_le64(store_bb); |
| 1737 | } |
| 1738 | if (read_seqretry(&bb->lock, seq)) |
| 1739 | goto retry; |
| 1740 | |
| 1741 | bb->sector = (rdev->sb_start + |
| 1742 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1743 | bb->size = le16_to_cpu(sb->bblog_size); |
| 1744 | bb->changed = 0; |
| 1745 | } |
| 1746 | } |
| 1747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1749 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | if (rdev2->desc_nr+1 > max_dev) |
| 1751 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1752 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1753 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1754 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1755 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1756 | rdev->sb_size = max_dev * 2 + 256; |
| 1757 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1758 | if (rdev->sb_size & bmask) |
| 1759 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1760 | } else |
| 1761 | max_dev = le32_to_cpu(sb->max_dev); |
| 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | for (i=0; i<max_dev;i++) |
| 1764 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1765 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1766 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1768 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1770 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1772 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1773 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | else |
| 1775 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1776 | } |
| 1777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1779 | } |
| 1780 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1781 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1782 | 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] | 1783 | { |
| 1784 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1785 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1786 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1787 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1788 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1789 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1790 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1791 | max_sectors -= rdev->data_offset; |
| 1792 | if (!num_sectors || num_sectors > max_sectors) |
| 1793 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1794 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1795 | /* minor version 0 with bitmap we can't move */ |
| 1796 | return 0; |
| 1797 | } else { |
| 1798 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1799 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1800 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1801 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1802 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1803 | if (!num_sectors || num_sectors > max_sectors) |
| 1804 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1805 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1806 | } |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1807 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1808 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1809 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1810 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1811 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1812 | rdev->sb_page); |
| 1813 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1814 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1817 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | [0] = { |
| 1819 | .name = "0.90.0", |
| 1820 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1821 | .load_super = super_90_load, |
| 1822 | .validate_super = super_90_validate, |
| 1823 | .sync_super = super_90_sync, |
| 1824 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | }, |
| 1826 | [1] = { |
| 1827 | .name = "md-1", |
| 1828 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1829 | .load_super = super_1_load, |
| 1830 | .validate_super = super_1_validate, |
| 1831 | .sync_super = super_1_sync, |
| 1832 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | }, |
| 1834 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1836 | static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1837 | { |
| 1838 | if (mddev->sync_super) { |
| 1839 | mddev->sync_super(mddev, rdev); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1844 | |
| 1845 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1846 | } |
| 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1849 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1850 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1852 | rcu_read_lock(); |
| 1853 | rdev_for_each_rcu(rdev, mddev1) |
| 1854 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1855 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1856 | rdev2->bdev->bd_contains) { |
| 1857 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1858 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1859 | } |
| 1860 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | static LIST_HEAD(pending_raid_disks); |
| 1865 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1866 | /* |
| 1867 | * Try to register data integrity profile for an mddev |
| 1868 | * |
| 1869 | * This is called when an array is started and after a disk has been kicked |
| 1870 | * from the array. It only succeeds if all working and active component devices |
| 1871 | * are integrity capable with matching profiles. |
| 1872 | */ |
| 1873 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1874 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1875 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1876 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1877 | if (list_empty(&mddev->disks)) |
| 1878 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1879 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1880 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1881 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1882 | /* skip spares and non-functional disks */ |
| 1883 | if (test_bit(Faulty, &rdev->flags)) |
| 1884 | continue; |
| 1885 | if (rdev->raid_disk < 0) |
| 1886 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1887 | if (!reference) { |
| 1888 | /* Use the first rdev as the reference */ |
| 1889 | reference = rdev; |
| 1890 | continue; |
| 1891 | } |
| 1892 | /* does this rdev's profile match the reference profile? */ |
| 1893 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1894 | rdev->bdev->bd_disk) < 0) |
| 1895 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1896 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1897 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1898 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1899 | /* |
| 1900 | * All component devices are integrity capable and have matching |
| 1901 | * profiles, register the common profile for the md device. |
| 1902 | */ |
| 1903 | if (blk_integrity_register(mddev->gendisk, |
| 1904 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1905 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1906 | mdname(mddev)); |
| 1907 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1908 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1909 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1910 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1911 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1912 | mdname(mddev)); |
| 1913 | return -EINVAL; |
| 1914 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1915 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1916 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1917 | EXPORT_SYMBOL(md_integrity_register); |
| 1918 | |
| 1919 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1920 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1921 | { |
| 1922 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1923 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1924 | |
| 1925 | if (!bi_mddev) /* nothing to do */ |
| 1926 | return; |
| 1927 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1928 | return; |
| 1929 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1930 | rdev->bdev->bd_disk) >= 0) |
| 1931 | return; |
| 1932 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1933 | blk_integrity_unregister(mddev->gendisk); |
| 1934 | } |
| 1935 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1938 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1939 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1940 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1941 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1942 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
| 1944 | if (rdev->mddev) { |
| 1945 | MD_BUG(); |
| 1946 | return -EINVAL; |
| 1947 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1948 | |
| 1949 | /* prevent duplicates */ |
| 1950 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1951 | return -EEXIST; |
| 1952 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1953 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1954 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1955 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1956 | if (mddev->pers) { |
| 1957 | /* Cannot change size, so fail |
| 1958 | * If mddev->level <= 0, then we don't care |
| 1959 | * about aligning sizes (e.g. linear) |
| 1960 | */ |
| 1961 | if (mddev->level > 0) |
| 1962 | return -ENOSPC; |
| 1963 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1964 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
| 1967 | /* Verify rdev->desc_nr is unique. |
| 1968 | * If it is -1, assign a free number, else |
| 1969 | * check number is not in use |
| 1970 | */ |
| 1971 | if (rdev->desc_nr < 0) { |
| 1972 | int choice = 0; |
| 1973 | if (mddev->pers) choice = mddev->raid_disks; |
| 1974 | while (find_rdev_nr(mddev, choice)) |
| 1975 | choice++; |
| 1976 | rdev->desc_nr = choice; |
| 1977 | } else { |
| 1978 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1979 | return -EBUSY; |
| 1980 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1981 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1982 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1983 | mdname(mddev), mddev->max_disks); |
| 1984 | return -EBUSY; |
| 1985 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1986 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1987 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1988 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1991 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1992 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1993 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1994 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1995 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1996 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1997 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1998 | /* failure here is OK */; |
| 1999 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2000 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2001 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2002 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2003 | |
| 2004 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2005 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2008 | |
| 2009 | fail: |
| 2010 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2011 | b, mdname(mddev)); |
| 2012 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2015 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2016 | { |
| 2017 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 2018 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2019 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 2023 | { |
| 2024 | char b[BDEVNAME_SIZE]; |
| 2025 | if (!rdev->mddev) { |
| 2026 | MD_BUG(); |
| 2027 | return; |
| 2028 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2029 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2030 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2032 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2033 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2034 | sysfs_put(rdev->sysfs_state); |
| 2035 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2036 | kfree(rdev->badblocks.page); |
| 2037 | rdev->badblocks.count = 0; |
| 2038 | rdev->badblocks.page = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2039 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2040 | * writing to 'remove' to "dev/state". We also need |
| 2041 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2042 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2043 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2044 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2045 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2046 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | /* |
| 2050 | * prevent the device from being mounted, repartitioned or |
| 2051 | * otherwise reused by a RAID array (or any other kernel |
| 2052 | * subsystem), by bd_claiming the device. |
| 2053 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2054 | 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] | 2055 | { |
| 2056 | int err = 0; |
| 2057 | struct block_device *bdev; |
| 2058 | char b[BDEVNAME_SIZE]; |
| 2059 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2060 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 2061 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | if (IS_ERR(bdev)) { |
| 2063 | printk(KERN_ERR "md: could not open %s.\n", |
| 2064 | __bdevname(dev, b)); |
| 2065 | return PTR_ERR(bdev); |
| 2066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | rdev->bdev = bdev; |
| 2068 | return err; |
| 2069 | } |
| 2070 | |
| 2071 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 2072 | { |
| 2073 | struct block_device *bdev = rdev->bdev; |
| 2074 | rdev->bdev = NULL; |
| 2075 | if (!bdev) |
| 2076 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2077 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | void md_autodetect_dev(dev_t dev); |
| 2081 | |
| 2082 | static void export_rdev(mdk_rdev_t * rdev) |
| 2083 | { |
| 2084 | char b[BDEVNAME_SIZE]; |
| 2085 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2086 | bdevname(rdev->bdev,b)); |
| 2087 | if (rdev->mddev) |
| 2088 | MD_BUG(); |
| 2089 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2091 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2092 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | #endif |
| 2094 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2095 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 2099 | { |
| 2100 | unbind_rdev_from_array(rdev); |
| 2101 | export_rdev(rdev); |
| 2102 | } |
| 2103 | |
| 2104 | static void export_array(mddev_t *mddev) |
| 2105 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2106 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2108 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | if (!rdev->mddev) { |
| 2110 | MD_BUG(); |
| 2111 | continue; |
| 2112 | } |
| 2113 | kick_rdev_from_array(rdev); |
| 2114 | } |
| 2115 | if (!list_empty(&mddev->disks)) |
| 2116 | MD_BUG(); |
| 2117 | mddev->raid_disks = 0; |
| 2118 | mddev->major_version = 0; |
| 2119 | } |
| 2120 | |
| 2121 | static void print_desc(mdp_disk_t *desc) |
| 2122 | { |
| 2123 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2124 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2125 | } |
| 2126 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2127 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | { |
| 2129 | int i; |
| 2130 | |
| 2131 | printk(KERN_INFO |
| 2132 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2133 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2134 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2135 | sb->ctime); |
| 2136 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2137 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2138 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2139 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2140 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2141 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2142 | sb->failed_disks, sb->spare_disks, |
| 2143 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2144 | |
| 2145 | printk(KERN_INFO); |
| 2146 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2147 | mdp_disk_t *desc; |
| 2148 | |
| 2149 | desc = sb->disks + i; |
| 2150 | if (desc->number || desc->major || desc->minor || |
| 2151 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2152 | printk(" D %2d: ", i); |
| 2153 | print_desc(desc); |
| 2154 | } |
| 2155 | } |
| 2156 | printk(KERN_INFO "md: THIS: "); |
| 2157 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2160 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2161 | { |
| 2162 | __u8 *uuid; |
| 2163 | |
| 2164 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2165 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2166 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2167 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2168 | le32_to_cpu(sb->major_version), |
| 2169 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2170 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2171 | sb->set_name, |
| 2172 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2173 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2174 | |
| 2175 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2176 | printk(KERN_INFO |
| 2177 | "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] | 2178 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2179 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2180 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2181 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2182 | le32_to_cpu(sb->level), |
| 2183 | (unsigned long long)le64_to_cpu(sb->size), |
| 2184 | le32_to_cpu(sb->raid_disks), |
| 2185 | le32_to_cpu(sb->layout), |
| 2186 | le32_to_cpu(sb->chunksize), |
| 2187 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2188 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2189 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2190 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2191 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2192 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2193 | sb->devflags, |
| 2194 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2195 | (unsigned long long)le64_to_cpu(sb->events), |
| 2196 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2197 | le32_to_cpu(sb->sb_csum), |
| 2198 | le32_to_cpu(sb->max_dev) |
| 2199 | ); |
| 2200 | } |
| 2201 | |
| 2202 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | { |
| 2204 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2205 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2206 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2207 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2208 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2210 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2211 | switch (major_version) { |
| 2212 | case 0: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2213 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2214 | break; |
| 2215 | case 1: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2216 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2217 | break; |
| 2218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | } else |
| 2220 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2221 | } |
| 2222 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2223 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2225 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | mdk_rdev_t *rdev; |
| 2227 | mddev_t *mddev; |
| 2228 | char b[BDEVNAME_SIZE]; |
| 2229 | |
| 2230 | printk("\n"); |
| 2231 | printk("md: **********************************\n"); |
| 2232 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2233 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2234 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2236 | if (mddev->bitmap) |
| 2237 | bitmap_print_sb(mddev->bitmap); |
| 2238 | else |
| 2239 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2240 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2242 | printk("\n"); |
| 2243 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2244 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2245 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | } |
| 2247 | printk("md: **********************************\n"); |
| 2248 | printk("\n"); |
| 2249 | } |
| 2250 | |
| 2251 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2252 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2254 | /* Update each superblock (in-memory image), but |
| 2255 | * if we are allowed to, skip spares which already |
| 2256 | * have the right event counter, or have one earlier |
| 2257 | * (which would mean they aren't being marked as dirty |
| 2258 | * with the rest of the array) |
| 2259 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2261 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2262 | if (rdev->sb_events == mddev->events || |
| 2263 | (nospares && |
| 2264 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2265 | rdev->sb_events+1 == mddev->events)) { |
| 2266 | /* Don't update this superblock */ |
| 2267 | rdev->sb_loaded = 2; |
| 2268 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2269 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2270 | rdev->sb_loaded = 1; |
| 2271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2275 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2278 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2279 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2280 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2283 | /* First make sure individual recovery_offsets are correct */ |
| 2284 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2285 | if (rdev->raid_disk >= 0 && |
| 2286 | mddev->delta_disks >= 0 && |
| 2287 | !test_bit(In_sync, &rdev->flags) && |
| 2288 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2289 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2290 | |
| 2291 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2292 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2293 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2294 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2295 | if (!mddev->external) |
| 2296 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2297 | wake_up(&mddev->sb_wait); |
| 2298 | return; |
| 2299 | } |
| 2300 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2301 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2302 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2303 | mddev->utime = get_seconds(); |
| 2304 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2305 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2306 | force_change = 1; |
| 2307 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2308 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2309 | * though if events isn't the right even/odd, we will have to do |
| 2310 | * spares after all |
| 2311 | */ |
| 2312 | nospares = 1; |
| 2313 | if (force_change) |
| 2314 | nospares = 0; |
| 2315 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2316 | /* If the array is degraded, then skipping spares is both |
| 2317 | * dangerous and fairly pointless. |
| 2318 | * Dangerous because a device that was removed from the array |
| 2319 | * might have a event_count that still looks up-to-date, |
| 2320 | * so it can be re-added without a resync. |
| 2321 | * Pointless because if there are any spares to skip, |
| 2322 | * then a recovery will happen and soon that array won't |
| 2323 | * be degraded any more and the spare can go back to sleep then. |
| 2324 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2325 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2326 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2327 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2328 | |
| 2329 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2330 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2331 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2332 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2333 | && mddev->can_decrease_events |
| 2334 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2335 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2336 | mddev->can_decrease_events = 0; |
| 2337 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2338 | /* otherwise we have to go forward and ... */ |
| 2339 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2340 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | |
| 2343 | if (!mddev->events) { |
| 2344 | /* |
| 2345 | * oops, this 64-bit counter should never wrap. |
| 2346 | * Either we are in around ~1 trillion A.C., assuming |
| 2347 | * 1 reboot per second, or we have a bug: |
| 2348 | */ |
| 2349 | MD_BUG(); |
| 2350 | mddev->events --; |
| 2351 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2352 | |
| 2353 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2354 | if (rdev->badblocks.changed) |
| 2355 | any_badblocks_changed++; |
| 2356 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2357 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2358 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | |
| 2360 | dprintk(KERN_INFO |
| 2361 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2362 | mdname(mddev),mddev->in_sync); |
| 2363 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2364 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2365 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | char b[BDEVNAME_SIZE]; |
| 2367 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2368 | if (rdev->sb_loaded != 1) |
| 2369 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2370 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | dprintk("(skipping faulty "); |
| 2372 | |
| 2373 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2374 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2375 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2376 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2377 | rdev->sb_page); |
| 2378 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2379 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2380 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2381 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2382 | if (rdev->badblocks.size) { |
| 2383 | md_super_write(mddev, rdev, |
| 2384 | rdev->badblocks.sector, |
| 2385 | rdev->badblocks.size << 9, |
| 2386 | rdev->bb_page); |
| 2387 | rdev->badblocks.size = 0; |
| 2388 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | } else |
| 2391 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2392 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | /* only need to write one superblock... */ |
| 2394 | break; |
| 2395 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2396 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2397 | /* 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] | 2398 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2399 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2400 | if (mddev->in_sync != sync_req || |
| 2401 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2402 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2403 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2404 | goto repeat; |
| 2405 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2406 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2407 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2408 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2409 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2410 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2411 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2412 | if (any_badblocks_changed) |
| 2413 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2414 | md_ack_all_badblocks(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2417 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2418 | * We want to accept with case. For this we use cmd_match. |
| 2419 | */ |
| 2420 | static int cmd_match(const char *cmd, const char *str) |
| 2421 | { |
| 2422 | /* See if cmd, written into a sysfs file, matches |
| 2423 | * str. They must either be the same, or cmd can |
| 2424 | * have a trailing newline |
| 2425 | */ |
| 2426 | while (*cmd && *str && *cmd == *str) { |
| 2427 | cmd++; |
| 2428 | str++; |
| 2429 | } |
| 2430 | if (*cmd == '\n') |
| 2431 | cmd++; |
| 2432 | if (*str || *cmd) |
| 2433 | return 0; |
| 2434 | return 1; |
| 2435 | } |
| 2436 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2437 | struct rdev_sysfs_entry { |
| 2438 | struct attribute attr; |
| 2439 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2440 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2441 | }; |
| 2442 | |
| 2443 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2444 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2445 | { |
| 2446 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2447 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2448 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2449 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2450 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2451 | sep = ","; |
| 2452 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2453 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2454 | len += sprintf(page+len, "%sin_sync",sep); |
| 2455 | sep = ","; |
| 2456 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2457 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2458 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2459 | sep = ","; |
| 2460 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2461 | if (test_bit(Blocked, &rdev->flags)) { |
| 2462 | len += sprintf(page+len, "%sblocked", sep); |
| 2463 | sep = ","; |
| 2464 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2465 | if (!test_bit(Faulty, &rdev->flags) && |
| 2466 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2467 | len += sprintf(page+len, "%sspare", sep); |
| 2468 | sep = ","; |
| 2469 | } |
| 2470 | return len+sprintf(page+len, "\n"); |
| 2471 | } |
| 2472 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2473 | static ssize_t |
| 2474 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2475 | { |
| 2476 | /* can write |
| 2477 | * faulty - simulates and error |
| 2478 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2479 | * writemostly - sets write_mostly |
| 2480 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2481 | * blocked - sets the Blocked flag |
| 2482 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2483 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2484 | */ |
| 2485 | int err = -EINVAL; |
| 2486 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2487 | md_error(rdev->mddev, rdev); |
| 2488 | err = 0; |
| 2489 | } else if (cmd_match(buf, "remove")) { |
| 2490 | if (rdev->raid_disk >= 0) |
| 2491 | err = -EBUSY; |
| 2492 | else { |
| 2493 | mddev_t *mddev = rdev->mddev; |
| 2494 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2495 | if (mddev->pers) |
| 2496 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2497 | md_new_event(mddev); |
| 2498 | err = 0; |
| 2499 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2500 | } else if (cmd_match(buf, "writemostly")) { |
| 2501 | set_bit(WriteMostly, &rdev->flags); |
| 2502 | err = 0; |
| 2503 | } else if (cmd_match(buf, "-writemostly")) { |
| 2504 | clear_bit(WriteMostly, &rdev->flags); |
| 2505 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2506 | } else if (cmd_match(buf, "blocked")) { |
| 2507 | set_bit(Blocked, &rdev->flags); |
| 2508 | err = 0; |
| 2509 | } else if (cmd_match(buf, "-blocked")) { |
| 2510 | clear_bit(Blocked, &rdev->flags); |
| 2511 | wake_up(&rdev->blocked_wait); |
| 2512 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2513 | md_wakeup_thread(rdev->mddev->thread); |
| 2514 | |
| 2515 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2516 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2517 | set_bit(In_sync, &rdev->flags); |
| 2518 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2519 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2520 | if (!err) |
| 2521 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2522 | return err ? err : len; |
| 2523 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2524 | static struct rdev_sysfs_entry rdev_state = |
| 2525 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2526 | |
| 2527 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2528 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2529 | { |
| 2530 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2531 | } |
| 2532 | |
| 2533 | static ssize_t |
| 2534 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2535 | { |
| 2536 | char *e; |
| 2537 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2538 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2539 | atomic_set(&rdev->corrected_errors, n); |
| 2540 | return len; |
| 2541 | } |
| 2542 | return -EINVAL; |
| 2543 | } |
| 2544 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2545 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2546 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2547 | static ssize_t |
| 2548 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2549 | { |
| 2550 | if (rdev->raid_disk < 0) |
| 2551 | return sprintf(page, "none\n"); |
| 2552 | else |
| 2553 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2554 | } |
| 2555 | |
| 2556 | static ssize_t |
| 2557 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2558 | { |
| 2559 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2560 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2561 | int slot = simple_strtoul(buf, &e, 10); |
| 2562 | if (strncmp(buf, "none", 4)==0) |
| 2563 | slot = -1; |
| 2564 | else if (e==buf || (*e && *e!= '\n')) |
| 2565 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2566 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2567 | /* Setting 'slot' on an active array requires also |
| 2568 | * updating the 'rd%d' link, and communicating |
| 2569 | * with the personality with ->hot_*_disk. |
| 2570 | * For now we only support removing |
| 2571 | * failed/spare devices. This normally happens automatically, |
| 2572 | * but not when the metadata is externally managed. |
| 2573 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2574 | if (rdev->raid_disk == -1) |
| 2575 | return -EEXIST; |
| 2576 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2577 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2578 | return -EINVAL; |
| 2579 | err = rdev->mddev->pers-> |
| 2580 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2581 | if (err) |
| 2582 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2583 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2584 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2585 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2586 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2587 | } else if (rdev->mddev->pers) { |
| 2588 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2589 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2590 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2591 | */ |
| 2592 | |
| 2593 | if (rdev->raid_disk != -1) |
| 2594 | return -EBUSY; |
| 2595 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2596 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2597 | return -EBUSY; |
| 2598 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2599 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2600 | return -EINVAL; |
| 2601 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2602 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2603 | if (rdev2->raid_disk == slot) |
| 2604 | return -EEXIST; |
| 2605 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2606 | if (slot >= rdev->mddev->raid_disks && |
| 2607 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2608 | return -ENOSPC; |
| 2609 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2610 | rdev->raid_disk = slot; |
| 2611 | if (test_bit(In_sync, &rdev->flags)) |
| 2612 | rdev->saved_raid_disk = slot; |
| 2613 | else |
| 2614 | rdev->saved_raid_disk = -1; |
| 2615 | err = rdev->mddev->pers-> |
| 2616 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2617 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2618 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2619 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2620 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2621 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2622 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2623 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2624 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2625 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2626 | if (slot >= rdev->mddev->raid_disks && |
| 2627 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2628 | return -ENOSPC; |
| 2629 | rdev->raid_disk = slot; |
| 2630 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2631 | clear_bit(Faulty, &rdev->flags); |
| 2632 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2633 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2634 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2635 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2636 | return len; |
| 2637 | } |
| 2638 | |
| 2639 | |
| 2640 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2641 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2642 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2643 | static ssize_t |
| 2644 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2645 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2646 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | static ssize_t |
| 2650 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2651 | { |
| 2652 | char *e; |
| 2653 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2654 | if (e==buf || (*e && *e != '\n')) |
| 2655 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2656 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2657 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2658 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2659 | /* Must set offset before size, so overlap checks |
| 2660 | * can be sane */ |
| 2661 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2662 | rdev->data_offset = offset; |
| 2663 | return len; |
| 2664 | } |
| 2665 | |
| 2666 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2667 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2668 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2669 | static ssize_t |
| 2670 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2671 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2672 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2673 | } |
| 2674 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2675 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2676 | { |
| 2677 | /* check if two start/length pairs overlap */ |
| 2678 | if (s1+l1 <= s2) |
| 2679 | return 0; |
| 2680 | if (s2+l2 <= s1) |
| 2681 | return 0; |
| 2682 | return 1; |
| 2683 | } |
| 2684 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2685 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2686 | { |
| 2687 | unsigned long long blocks; |
| 2688 | sector_t new; |
| 2689 | |
| 2690 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2691 | return -EINVAL; |
| 2692 | |
| 2693 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2694 | return -EINVAL; /* sector conversion overflow */ |
| 2695 | |
| 2696 | new = blocks * 2; |
| 2697 | if (new != blocks * 2) |
| 2698 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2699 | |
| 2700 | *sectors = new; |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2704 | static ssize_t |
| 2705 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2706 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2707 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2708 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2709 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2710 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2711 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2712 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2713 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2714 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2715 | sectors = super_types[my_mddev->major_version]. |
| 2716 | rdev_size_change(rdev, sectors); |
| 2717 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2718 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2719 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2720 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2721 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2722 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2723 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2724 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2725 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2726 | rdev->sectors = sectors; |
| 2727 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2728 | /* need to check that all other rdevs with the same ->bdev |
| 2729 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2730 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2731 | * we have to change it back, we will have the lock again. |
| 2732 | */ |
| 2733 | mddev_t *mddev; |
| 2734 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2735 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2736 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2737 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2738 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2739 | mdk_rdev_t *rdev2; |
| 2740 | |
| 2741 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2742 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2743 | if (rdev->bdev == rdev2->bdev && |
| 2744 | rdev != rdev2 && |
| 2745 | overlaps(rdev->data_offset, rdev->sectors, |
| 2746 | rdev2->data_offset, |
| 2747 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2748 | overlap = 1; |
| 2749 | break; |
| 2750 | } |
| 2751 | mddev_unlock(mddev); |
| 2752 | if (overlap) { |
| 2753 | mddev_put(mddev); |
| 2754 | break; |
| 2755 | } |
| 2756 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2757 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2758 | if (overlap) { |
| 2759 | /* Someone else could have slipped in a size |
| 2760 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2761 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2762 | * safe, and trust userspace not to race with |
| 2763 | * itself |
| 2764 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2765 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2766 | return -EBUSY; |
| 2767 | } |
| 2768 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2769 | return len; |
| 2770 | } |
| 2771 | |
| 2772 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2773 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2774 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2775 | |
| 2776 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2777 | { |
| 2778 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2779 | |
| 2780 | if (test_bit(In_sync, &rdev->flags) || |
| 2781 | recovery_start == MaxSector) |
| 2782 | return sprintf(page, "none\n"); |
| 2783 | |
| 2784 | return sprintf(page, "%llu\n", recovery_start); |
| 2785 | } |
| 2786 | |
| 2787 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2788 | { |
| 2789 | unsigned long long recovery_start; |
| 2790 | |
| 2791 | if (cmd_match(buf, "none")) |
| 2792 | recovery_start = MaxSector; |
| 2793 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2794 | return -EINVAL; |
| 2795 | |
| 2796 | if (rdev->mddev->pers && |
| 2797 | rdev->raid_disk >= 0) |
| 2798 | return -EBUSY; |
| 2799 | |
| 2800 | rdev->recovery_offset = recovery_start; |
| 2801 | if (recovery_start == MaxSector) |
| 2802 | set_bit(In_sync, &rdev->flags); |
| 2803 | else |
| 2804 | clear_bit(In_sync, &rdev->flags); |
| 2805 | return len; |
| 2806 | } |
| 2807 | |
| 2808 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2809 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2810 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2811 | |
| 2812 | static ssize_t |
| 2813 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2814 | static ssize_t |
| 2815 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2816 | |
| 2817 | static ssize_t bb_show(mdk_rdev_t *rdev, char *page) |
| 2818 | { |
| 2819 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2820 | } |
| 2821 | static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2822 | { |
| 2823 | return badblocks_store(&rdev->badblocks, page, len, 0); |
| 2824 | } |
| 2825 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2826 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2827 | |
| 2828 | |
| 2829 | static ssize_t ubb_show(mdk_rdev_t *rdev, char *page) |
| 2830 | { |
| 2831 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2832 | } |
| 2833 | static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len) |
| 2834 | { |
| 2835 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2836 | } |
| 2837 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2838 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2839 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2840 | static struct attribute *rdev_default_attrs[] = { |
| 2841 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2842 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2843 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2844 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2845 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2846 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2847 | &rdev_bad_blocks.attr, |
| 2848 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2849 | NULL, |
| 2850 | }; |
| 2851 | static ssize_t |
| 2852 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2853 | { |
| 2854 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2855 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2856 | mddev_t *mddev = rdev->mddev; |
| 2857 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2858 | |
| 2859 | if (!entry->show) |
| 2860 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2861 | |
| 2862 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2863 | if (!rv) { |
| 2864 | if (rdev->mddev == NULL) |
| 2865 | rv = -EBUSY; |
| 2866 | else |
| 2867 | rv = entry->show(rdev, page); |
| 2868 | mddev_unlock(mddev); |
| 2869 | } |
| 2870 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | static ssize_t |
| 2874 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2875 | const char *page, size_t length) |
| 2876 | { |
| 2877 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2878 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2879 | ssize_t rv; |
| 2880 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2881 | |
| 2882 | if (!entry->store) |
| 2883 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2884 | if (!capable(CAP_SYS_ADMIN)) |
| 2885 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2886 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2887 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2888 | if (rdev->mddev == NULL) |
| 2889 | rv = -EBUSY; |
| 2890 | else |
| 2891 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2892 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2893 | } |
| 2894 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | static void rdev_free(struct kobject *ko) |
| 2898 | { |
| 2899 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2900 | kfree(rdev); |
| 2901 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2902 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2903 | .show = rdev_attr_show, |
| 2904 | .store = rdev_attr_store, |
| 2905 | }; |
| 2906 | static struct kobj_type rdev_ktype = { |
| 2907 | .release = rdev_free, |
| 2908 | .sysfs_ops = &rdev_sysfs_ops, |
| 2909 | .default_attrs = rdev_default_attrs, |
| 2910 | }; |
| 2911 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2912 | int md_rdev_init(mdk_rdev_t *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2913 | { |
| 2914 | rdev->desc_nr = -1; |
| 2915 | rdev->saved_raid_disk = -1; |
| 2916 | rdev->raid_disk = -1; |
| 2917 | rdev->flags = 0; |
| 2918 | rdev->data_offset = 0; |
| 2919 | rdev->sb_events = 0; |
| 2920 | rdev->last_read_error.tv_sec = 0; |
| 2921 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2922 | rdev->sb_loaded = 0; |
| 2923 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2924 | atomic_set(&rdev->nr_pending, 0); |
| 2925 | atomic_set(&rdev->read_errors, 0); |
| 2926 | atomic_set(&rdev->corrected_errors, 0); |
| 2927 | |
| 2928 | INIT_LIST_HEAD(&rdev->same_set); |
| 2929 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2930 | |
| 2931 | /* Add space to store bad block list. |
| 2932 | * This reserves the space even on arrays where it cannot |
| 2933 | * be used - I wonder if that matters |
| 2934 | */ |
| 2935 | rdev->badblocks.count = 0; |
| 2936 | rdev->badblocks.shift = 0; |
| 2937 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2938 | seqlock_init(&rdev->badblocks.lock); |
| 2939 | if (rdev->badblocks.page == NULL) |
| 2940 | return -ENOMEM; |
| 2941 | |
| 2942 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2943 | } |
| 2944 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | /* |
| 2946 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2947 | * |
| 2948 | * mark the device faulty if: |
| 2949 | * |
| 2950 | * - the device is nonexistent (zero size) |
| 2951 | * - the device has no valid superblock |
| 2952 | * |
| 2953 | * a faulty rdev _never_ has rdev->sb set. |
| 2954 | */ |
| 2955 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2956 | { |
| 2957 | char b[BDEVNAME_SIZE]; |
| 2958 | int err; |
| 2959 | mdk_rdev_t *rdev; |
| 2960 | sector_t size; |
| 2961 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2962 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | if (!rdev) { |
| 2964 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2965 | return ERR_PTR(-ENOMEM); |
| 2966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2968 | err = md_rdev_init(rdev); |
| 2969 | if (err) |
| 2970 | goto abort_free; |
| 2971 | err = alloc_disk_sb(rdev); |
| 2972 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | goto abort_free; |
| 2974 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2975 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | if (err) |
| 2977 | goto abort_free; |
| 2978 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2979 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2980 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2981 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | if (!size) { |
| 2983 | printk(KERN_WARNING |
| 2984 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2985 | bdevname(rdev->bdev,b)); |
| 2986 | err = -EINVAL; |
| 2987 | goto abort_free; |
| 2988 | } |
| 2989 | |
| 2990 | if (super_format >= 0) { |
| 2991 | err = super_types[super_format]. |
| 2992 | load_super(rdev, NULL, super_minor); |
| 2993 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2994 | printk(KERN_WARNING |
| 2995 | "md: %s does not have a valid v%d.%d " |
| 2996 | "superblock, not importing!\n", |
| 2997 | bdevname(rdev->bdev,b), |
| 2998 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | goto abort_free; |
| 3000 | } |
| 3001 | if (err < 0) { |
| 3002 | printk(KERN_WARNING |
| 3003 | "md: could not read %s's sb, not importing!\n", |
| 3004 | bdevname(rdev->bdev,b)); |
| 3005 | goto abort_free; |
| 3006 | } |
| 3007 | } |
NeilBrown | 9f2f38307 | 2011-07-28 11:31:47 +1000 | [diff] [blame^] | 3008 | if (super_format == -1) |
| 3009 | /* hot-add for 0.90, or non-persistent: so no badblocks */ |
| 3010 | rdev->badblocks.shift = -1; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | return rdev; |
| 3013 | |
| 3014 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3015 | if (rdev->bdev) |
| 3016 | unlock_rdev(rdev); |
| 3017 | free_disk_sb(rdev); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3018 | kfree(rdev->badblocks.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | kfree(rdev); |
| 3020 | return ERR_PTR(err); |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Check a full RAID array for plausibility |
| 3025 | */ |
| 3026 | |
| 3027 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3028 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | { |
| 3030 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3031 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | char b[BDEVNAME_SIZE]; |
| 3033 | |
| 3034 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3035 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | switch (super_types[mddev->major_version]. |
| 3037 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3038 | case 1: |
| 3039 | freshest = rdev; |
| 3040 | break; |
| 3041 | case 0: |
| 3042 | break; |
| 3043 | default: |
| 3044 | printk( KERN_ERR \ |
| 3045 | "md: fatal superblock inconsistency in %s" |
| 3046 | " -- removing from array\n", |
| 3047 | bdevname(rdev->bdev,b)); |
| 3048 | kick_rdev_from_array(rdev); |
| 3049 | } |
| 3050 | |
| 3051 | |
| 3052 | super_types[mddev->major_version]. |
| 3053 | validate_super(mddev, freshest); |
| 3054 | |
| 3055 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3056 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3057 | if (mddev->max_disks && |
| 3058 | (rdev->desc_nr >= mddev->max_disks || |
| 3059 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3060 | printk(KERN_WARNING |
| 3061 | "md: %s: %s: only %d devices permitted\n", |
| 3062 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3063 | mddev->max_disks); |
| 3064 | kick_rdev_from_array(rdev); |
| 3065 | continue; |
| 3066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | if (rdev != freshest) |
| 3068 | if (super_types[mddev->major_version]. |
| 3069 | validate_super(mddev, rdev)) { |
| 3070 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3071 | " from array!\n", |
| 3072 | bdevname(rdev->bdev,b)); |
| 3073 | kick_rdev_from_array(rdev); |
| 3074 | continue; |
| 3075 | } |
| 3076 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3077 | rdev->desc_nr = i++; |
| 3078 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3079 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3080 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3081 | rdev->raid_disk = -1; |
| 3082 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
| 3084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3087 | /* Read a fixed-point number. |
| 3088 | * Numbers in sysfs attributes should be in "standard" units where |
| 3089 | * possible, so time should be in seconds. |
| 3090 | * However we internally use a a much smaller unit such as |
| 3091 | * milliseconds or jiffies. |
| 3092 | * This function takes a decimal number with a possible fractional |
| 3093 | * component, and produces an integer which is the result of |
| 3094 | * multiplying that number by 10^'scale'. |
| 3095 | * all without any floating-point arithmetic. |
| 3096 | */ |
| 3097 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3098 | { |
| 3099 | unsigned long result = 0; |
| 3100 | long decimals = -1; |
| 3101 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3102 | if (*cp == '.') |
| 3103 | decimals = 0; |
| 3104 | else if (decimals < scale) { |
| 3105 | unsigned int value; |
| 3106 | value = *cp - '0'; |
| 3107 | result = result * 10 + value; |
| 3108 | if (decimals >= 0) |
| 3109 | decimals++; |
| 3110 | } |
| 3111 | cp++; |
| 3112 | } |
| 3113 | if (*cp == '\n') |
| 3114 | cp++; |
| 3115 | if (*cp) |
| 3116 | return -EINVAL; |
| 3117 | if (decimals < 0) |
| 3118 | decimals = 0; |
| 3119 | while (decimals < scale) { |
| 3120 | result *= 10; |
| 3121 | decimals ++; |
| 3122 | } |
| 3123 | *res = result; |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3128 | static void md_safemode_timeout(unsigned long data); |
| 3129 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3130 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3131 | safe_delay_show(mddev_t *mddev, char *page) |
| 3132 | { |
| 3133 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3134 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3135 | } |
| 3136 | static ssize_t |
| 3137 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 3138 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3139 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3140 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3141 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3142 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3143 | if (msec == 0) |
| 3144 | mddev->safemode_delay = 0; |
| 3145 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3146 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3147 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3148 | if (mddev->safemode_delay == 0) |
| 3149 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3150 | if (mddev->safemode_delay < old_delay) |
| 3151 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3152 | } |
| 3153 | return len; |
| 3154 | } |
| 3155 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3156 | __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] | 3157 | |
| 3158 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3159 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3160 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3161 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3162 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3163 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3164 | else if (mddev->clevel[0]) |
| 3165 | return sprintf(page, "%s\n", mddev->clevel); |
| 3166 | else if (mddev->level != LEVEL_NONE) |
| 3167 | return sprintf(page, "%d\n", mddev->level); |
| 3168 | else |
| 3169 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3170 | } |
| 3171 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3172 | static ssize_t |
| 3173 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3174 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3175 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3176 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3177 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3178 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3179 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3180 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3181 | |
| 3182 | if (mddev->pers == NULL) { |
| 3183 | if (len == 0) |
| 3184 | return 0; |
| 3185 | if (len >= sizeof(mddev->clevel)) |
| 3186 | return -ENOSPC; |
| 3187 | strncpy(mddev->clevel, buf, len); |
| 3188 | if (mddev->clevel[len-1] == '\n') |
| 3189 | len--; |
| 3190 | mddev->clevel[len] = 0; |
| 3191 | mddev->level = LEVEL_NONE; |
| 3192 | return rv; |
| 3193 | } |
| 3194 | |
| 3195 | /* request to change the personality. Need to ensure: |
| 3196 | * - array is not engaged in resync/recovery/reshape |
| 3197 | * - old personality can be suspended |
| 3198 | * - new personality will access other array. |
| 3199 | */ |
| 3200 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3201 | if (mddev->sync_thread || |
| 3202 | mddev->reshape_position != MaxSector || |
| 3203 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3204 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3205 | |
| 3206 | if (!mddev->pers->quiesce) { |
| 3207 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3208 | mdname(mddev), mddev->pers->name); |
| 3209 | return -EINVAL; |
| 3210 | } |
| 3211 | |
| 3212 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3213 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3214 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3215 | strncpy(clevel, buf, len); |
| 3216 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3217 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3218 | clevel[len] = 0; |
| 3219 | if (strict_strtol(clevel, 10, &level)) |
| 3220 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3221 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3222 | if (request_module("md-%s", clevel) != 0) |
| 3223 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3224 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3225 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3226 | if (!pers || !try_module_get(pers->owner)) { |
| 3227 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3228 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3229 | return -EINVAL; |
| 3230 | } |
| 3231 | spin_unlock(&pers_lock); |
| 3232 | |
| 3233 | if (pers == mddev->pers) { |
| 3234 | /* Nothing to do! */ |
| 3235 | module_put(pers->owner); |
| 3236 | return rv; |
| 3237 | } |
| 3238 | if (!pers->takeover) { |
| 3239 | module_put(pers->owner); |
| 3240 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3241 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3242 | return -EINVAL; |
| 3243 | } |
| 3244 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3245 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3246 | rdev->new_raid_disk = rdev->raid_disk; |
| 3247 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3248 | /* ->takeover must set new_* and/or delta_disks |
| 3249 | * if it succeeds, and may set them when it fails. |
| 3250 | */ |
| 3251 | priv = pers->takeover(mddev); |
| 3252 | if (IS_ERR(priv)) { |
| 3253 | mddev->new_level = mddev->level; |
| 3254 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3255 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3256 | mddev->raid_disks -= mddev->delta_disks; |
| 3257 | mddev->delta_disks = 0; |
| 3258 | module_put(pers->owner); |
| 3259 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3260 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3261 | return PTR_ERR(priv); |
| 3262 | } |
| 3263 | |
| 3264 | /* Looks like we have a winner */ |
| 3265 | mddev_suspend(mddev); |
| 3266 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3267 | |
| 3268 | if (mddev->pers->sync_request == NULL && |
| 3269 | pers->sync_request != NULL) { |
| 3270 | /* need to add the md_redundancy_group */ |
| 3271 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3272 | printk(KERN_WARNING |
| 3273 | "md: cannot register extra attributes for %s\n", |
| 3274 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3275 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3276 | } |
| 3277 | if (mddev->pers->sync_request != NULL && |
| 3278 | pers->sync_request == NULL) { |
| 3279 | /* need to remove the md_redundancy_group */ |
| 3280 | if (mddev->to_remove == NULL) |
| 3281 | mddev->to_remove = &md_redundancy_group; |
| 3282 | } |
| 3283 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3284 | if (mddev->pers->sync_request == NULL && |
| 3285 | mddev->external) { |
| 3286 | /* We are converting from a no-redundancy array |
| 3287 | * to a redundancy array and metadata is managed |
| 3288 | * externally so we need to be sure that writes |
| 3289 | * won't block due to a need to transition |
| 3290 | * clean->dirty |
| 3291 | * until external management is started. |
| 3292 | */ |
| 3293 | mddev->in_sync = 0; |
| 3294 | mddev->safemode_delay = 0; |
| 3295 | mddev->safemode = 0; |
| 3296 | } |
| 3297 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3298 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3299 | if (rdev->raid_disk < 0) |
| 3300 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3301 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3302 | rdev->new_raid_disk = -1; |
| 3303 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3304 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3305 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3306 | } |
| 3307 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3308 | if (rdev->raid_disk < 0) |
| 3309 | continue; |
| 3310 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3311 | continue; |
| 3312 | rdev->raid_disk = rdev->new_raid_disk; |
| 3313 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3314 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3315 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3316 | if (sysfs_link_rdev(mddev, rdev)) |
| 3317 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3318 | " for %s after level change\n", |
| 3319 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3320 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3324 | mddev->pers = pers; |
| 3325 | mddev->private = priv; |
| 3326 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3327 | mddev->level = mddev->new_level; |
| 3328 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3329 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3330 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3331 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3332 | if (mddev->pers->sync_request == NULL) { |
| 3333 | /* this is now an array without redundancy, so |
| 3334 | * it must always be in_sync |
| 3335 | */ |
| 3336 | mddev->in_sync = 1; |
| 3337 | del_timer_sync(&mddev->safemode_timer); |
| 3338 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3339 | pers->run(mddev); |
| 3340 | mddev_resume(mddev); |
| 3341 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3342 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3343 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3344 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3345 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3346 | return rv; |
| 3347 | } |
| 3348 | |
| 3349 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3350 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3351 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3352 | |
| 3353 | static ssize_t |
| 3354 | layout_show(mddev_t *mddev, char *page) |
| 3355 | { |
| 3356 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3357 | if (mddev->reshape_position != MaxSector && |
| 3358 | mddev->layout != mddev->new_layout) |
| 3359 | return sprintf(page, "%d (%d)\n", |
| 3360 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3361 | return sprintf(page, "%d\n", mddev->layout); |
| 3362 | } |
| 3363 | |
| 3364 | static ssize_t |
| 3365 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3366 | { |
| 3367 | char *e; |
| 3368 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3369 | |
| 3370 | if (!*buf || (*e && *e != '\n')) |
| 3371 | return -EINVAL; |
| 3372 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3373 | if (mddev->pers) { |
| 3374 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3375 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3376 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3377 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3378 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3379 | if (err) { |
| 3380 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3381 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3382 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3383 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3384 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3385 | if (mddev->reshape_position == MaxSector) |
| 3386 | mddev->layout = n; |
| 3387 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3388 | return len; |
| 3389 | } |
| 3390 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3391 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3392 | |
| 3393 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3394 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3395 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3396 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3397 | if (mddev->raid_disks == 0) |
| 3398 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3399 | if (mddev->reshape_position != MaxSector && |
| 3400 | mddev->delta_disks != 0) |
| 3401 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3402 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3403 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3404 | } |
| 3405 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3406 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3407 | |
| 3408 | static ssize_t |
| 3409 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3410 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3411 | char *e; |
| 3412 | int rv = 0; |
| 3413 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3414 | |
| 3415 | if (!*buf || (*e && *e != '\n')) |
| 3416 | return -EINVAL; |
| 3417 | |
| 3418 | if (mddev->pers) |
| 3419 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3420 | else if (mddev->reshape_position != MaxSector) { |
| 3421 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3422 | mddev->delta_disks = n - olddisks; |
| 3423 | mddev->raid_disks = n; |
| 3424 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3425 | mddev->raid_disks = n; |
| 3426 | return rv ? rv : len; |
| 3427 | } |
| 3428 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3429 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3430 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3431 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3432 | chunk_size_show(mddev_t *mddev, char *page) |
| 3433 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3434 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3435 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3436 | return sprintf(page, "%d (%d)\n", |
| 3437 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3438 | mddev->chunk_sectors << 9); |
| 3439 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | static ssize_t |
| 3443 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3444 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3445 | char *e; |
| 3446 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3447 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3448 | if (!*buf || (*e && *e != '\n')) |
| 3449 | return -EINVAL; |
| 3450 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3451 | if (mddev->pers) { |
| 3452 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3453 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3454 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3455 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3456 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3457 | if (err) { |
| 3458 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3459 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3460 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3461 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3462 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3463 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3464 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3465 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3466 | return len; |
| 3467 | } |
| 3468 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3469 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3470 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3471 | static ssize_t |
| 3472 | resync_start_show(mddev_t *mddev, char *page) |
| 3473 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3474 | if (mddev->recovery_cp == MaxSector) |
| 3475 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3476 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3477 | } |
| 3478 | |
| 3479 | static ssize_t |
| 3480 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3481 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3482 | char *e; |
| 3483 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3484 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3485 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3486 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3487 | if (cmd_match(buf, "none")) |
| 3488 | n = MaxSector; |
| 3489 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3490 | return -EINVAL; |
| 3491 | |
| 3492 | mddev->recovery_cp = n; |
| 3493 | return len; |
| 3494 | } |
| 3495 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3496 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3497 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3498 | /* |
| 3499 | * The array state can be: |
| 3500 | * |
| 3501 | * clear |
| 3502 | * No devices, no size, no level |
| 3503 | * Equivalent to STOP_ARRAY ioctl |
| 3504 | * inactive |
| 3505 | * May have some settings, but array is not active |
| 3506 | * all IO results in error |
| 3507 | * When written, doesn't tear down array, but just stops it |
| 3508 | * suspended (not supported yet) |
| 3509 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3510 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3511 | * readonly |
| 3512 | * no resync can happen. no superblocks get written. |
| 3513 | * write requests fail |
| 3514 | * read-auto |
| 3515 | * like readonly, but behaves like 'clean' on a write request. |
| 3516 | * |
| 3517 | * clean - no pending writes, but otherwise active. |
| 3518 | * When written to inactive array, starts without resync |
| 3519 | * If a write request arrives then |
| 3520 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3521 | * if not known, block and switch to write-pending |
| 3522 | * If written to an active array that has pending writes, then fails. |
| 3523 | * active |
| 3524 | * fully active: IO and resync can be happening. |
| 3525 | * When written to inactive array, starts with resync |
| 3526 | * |
| 3527 | * write-pending |
| 3528 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3529 | * |
| 3530 | * active-idle |
| 3531 | * like active, but no writes have been seen for a while (100msec). |
| 3532 | * |
| 3533 | */ |
| 3534 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3535 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3536 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3537 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3538 | "write-pending", "active-idle", NULL }; |
| 3539 | |
| 3540 | static int match_word(const char *word, char **list) |
| 3541 | { |
| 3542 | int n; |
| 3543 | for (n=0; list[n]; n++) |
| 3544 | if (cmd_match(word, list[n])) |
| 3545 | break; |
| 3546 | return n; |
| 3547 | } |
| 3548 | |
| 3549 | static ssize_t |
| 3550 | array_state_show(mddev_t *mddev, char *page) |
| 3551 | { |
| 3552 | enum array_state st = inactive; |
| 3553 | |
| 3554 | if (mddev->pers) |
| 3555 | switch(mddev->ro) { |
| 3556 | case 1: |
| 3557 | st = readonly; |
| 3558 | break; |
| 3559 | case 2: |
| 3560 | st = read_auto; |
| 3561 | break; |
| 3562 | case 0: |
| 3563 | if (mddev->in_sync) |
| 3564 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3565 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3566 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3567 | else if (mddev->safemode) |
| 3568 | st = active_idle; |
| 3569 | else |
| 3570 | st = active; |
| 3571 | } |
| 3572 | else { |
| 3573 | if (list_empty(&mddev->disks) && |
| 3574 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3575 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3576 | st = clear; |
| 3577 | else |
| 3578 | st = inactive; |
| 3579 | } |
| 3580 | return sprintf(page, "%s\n", array_states[st]); |
| 3581 | } |
| 3582 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3583 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3584 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3585 | static int do_md_run(mddev_t * mddev); |
| 3586 | static int restart_array(mddev_t *mddev); |
| 3587 | |
| 3588 | static ssize_t |
| 3589 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3590 | { |
| 3591 | int err = -EINVAL; |
| 3592 | enum array_state st = match_word(buf, array_states); |
| 3593 | switch(st) { |
| 3594 | case bad_word: |
| 3595 | break; |
| 3596 | case clear: |
| 3597 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3598 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3599 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3600 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3601 | break; |
| 3602 | case inactive: |
| 3603 | /* stopping an active array */ |
| 3604 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3605 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3606 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3607 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3608 | } else |
| 3609 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3610 | break; |
| 3611 | case suspended: |
| 3612 | break; /* not supported yet */ |
| 3613 | case readonly: |
| 3614 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3615 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3616 | else { |
| 3617 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3618 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3619 | err = do_md_run(mddev); |
| 3620 | } |
| 3621 | break; |
| 3622 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3623 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3624 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3625 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3626 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3627 | err = restart_array(mddev); |
| 3628 | if (err == 0) { |
| 3629 | mddev->ro = 2; |
| 3630 | set_disk_ro(mddev->gendisk, 0); |
| 3631 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3632 | } else { |
| 3633 | mddev->ro = 2; |
| 3634 | err = do_md_run(mddev); |
| 3635 | } |
| 3636 | break; |
| 3637 | case clean: |
| 3638 | if (mddev->pers) { |
| 3639 | restart_array(mddev); |
| 3640 | spin_lock_irq(&mddev->write_lock); |
| 3641 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3642 | if (mddev->in_sync == 0) { |
| 3643 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3644 | if (mddev->safemode == 1) |
| 3645 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3646 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3647 | } |
| 3648 | err = 0; |
| 3649 | } else |
| 3650 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3651 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3652 | } else |
| 3653 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3654 | break; |
| 3655 | case active: |
| 3656 | if (mddev->pers) { |
| 3657 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3658 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3659 | wake_up(&mddev->sb_wait); |
| 3660 | err = 0; |
| 3661 | } else { |
| 3662 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3663 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3664 | err = do_md_run(mddev); |
| 3665 | } |
| 3666 | break; |
| 3667 | case write_pending: |
| 3668 | case active_idle: |
| 3669 | /* these cannot be set */ |
| 3670 | break; |
| 3671 | } |
| 3672 | if (err) |
| 3673 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3674 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3675 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3676 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3677 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3678 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3679 | static struct md_sysfs_entry md_array_state = |
| 3680 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3681 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3682 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3683 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3684 | return sprintf(page, "%d\n", |
| 3685 | atomic_read(&mddev->max_corr_read_errors)); |
| 3686 | } |
| 3687 | |
| 3688 | static ssize_t |
| 3689 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3690 | { |
| 3691 | char *e; |
| 3692 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3693 | |
| 3694 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3695 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3696 | return len; |
| 3697 | } |
| 3698 | return -EINVAL; |
| 3699 | } |
| 3700 | |
| 3701 | static struct md_sysfs_entry max_corr_read_errors = |
| 3702 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3703 | max_corrected_read_errors_store); |
| 3704 | |
| 3705 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3706 | null_show(mddev_t *mddev, char *page) |
| 3707 | { |
| 3708 | return -EINVAL; |
| 3709 | } |
| 3710 | |
| 3711 | static ssize_t |
| 3712 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3713 | { |
| 3714 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3715 | /* The new device is added to the array. |
| 3716 | * If the array has a persistent superblock, we read the |
| 3717 | * superblock to initialise info and check validity. |
| 3718 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3719 | * which mainly checks size. |
| 3720 | */ |
| 3721 | char *e; |
| 3722 | int major = simple_strtoul(buf, &e, 10); |
| 3723 | int minor; |
| 3724 | dev_t dev; |
| 3725 | mdk_rdev_t *rdev; |
| 3726 | int err; |
| 3727 | |
| 3728 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3729 | return -EINVAL; |
| 3730 | minor = simple_strtoul(e+1, &e, 10); |
| 3731 | if (*e && *e != '\n') |
| 3732 | return -EINVAL; |
| 3733 | dev = MKDEV(major, minor); |
| 3734 | if (major != MAJOR(dev) || |
| 3735 | minor != MINOR(dev)) |
| 3736 | return -EOVERFLOW; |
| 3737 | |
| 3738 | |
| 3739 | if (mddev->persistent) { |
| 3740 | rdev = md_import_device(dev, mddev->major_version, |
| 3741 | mddev->minor_version); |
| 3742 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3743 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3744 | mdk_rdev_t, same_set); |
| 3745 | err = super_types[mddev->major_version] |
| 3746 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3747 | if (err < 0) |
| 3748 | goto out; |
| 3749 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3750 | } else if (mddev->external) |
| 3751 | rdev = md_import_device(dev, -2, -1); |
| 3752 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3753 | rdev = md_import_device(dev, -1, -1); |
| 3754 | |
| 3755 | if (IS_ERR(rdev)) |
| 3756 | return PTR_ERR(rdev); |
| 3757 | err = bind_rdev_to_array(rdev, mddev); |
| 3758 | out: |
| 3759 | if (err) |
| 3760 | export_rdev(rdev); |
| 3761 | return err ? err : len; |
| 3762 | } |
| 3763 | |
| 3764 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3765 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3766 | |
| 3767 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3768 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3769 | { |
| 3770 | char *end; |
| 3771 | unsigned long chunk, end_chunk; |
| 3772 | |
| 3773 | if (!mddev->bitmap) |
| 3774 | goto out; |
| 3775 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3776 | while (*buf) { |
| 3777 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3778 | if (buf == end) break; |
| 3779 | if (*end == '-') { /* range */ |
| 3780 | buf = end + 1; |
| 3781 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3782 | if (buf == end) break; |
| 3783 | } |
| 3784 | if (*end && !isspace(*end)) break; |
| 3785 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3786 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3787 | } |
| 3788 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3789 | out: |
| 3790 | return len; |
| 3791 | } |
| 3792 | |
| 3793 | static struct md_sysfs_entry md_bitmap = |
| 3794 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3795 | |
| 3796 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3797 | size_show(mddev_t *mddev, char *page) |
| 3798 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3799 | return sprintf(page, "%llu\n", |
| 3800 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3803 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3804 | |
| 3805 | static ssize_t |
| 3806 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3807 | { |
| 3808 | /* If array is inactive, we can reduce the component size, but |
| 3809 | * not increase it (except from 0). |
| 3810 | * If array is active, we can try an on-line resize |
| 3811 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3812 | sector_t sectors; |
| 3813 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3814 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3815 | if (err < 0) |
| 3816 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3817 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3818 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3819 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3820 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3821 | if (mddev->dev_sectors == 0 || |
| 3822 | mddev->dev_sectors > sectors) |
| 3823 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3824 | else |
| 3825 | err = -ENOSPC; |
| 3826 | } |
| 3827 | return err ? err : len; |
| 3828 | } |
| 3829 | |
| 3830 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3831 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3832 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3833 | |
| 3834 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3835 | * This is one of |
| 3836 | * 'none' for arrays with no metadata (good luck...) |
| 3837 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3838 | * or N.M for internally known formats |
| 3839 | */ |
| 3840 | static ssize_t |
| 3841 | metadata_show(mddev_t *mddev, char *page) |
| 3842 | { |
| 3843 | if (mddev->persistent) |
| 3844 | return sprintf(page, "%d.%d\n", |
| 3845 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3846 | else if (mddev->external) |
| 3847 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3848 | else |
| 3849 | return sprintf(page, "none\n"); |
| 3850 | } |
| 3851 | |
| 3852 | static ssize_t |
| 3853 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3854 | { |
| 3855 | int major, minor; |
| 3856 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3857 | /* Changing the details of 'external' metadata is |
| 3858 | * always permitted. Otherwise there must be |
| 3859 | * no devices attached to the array. |
| 3860 | */ |
| 3861 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3862 | ; |
| 3863 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3864 | return -EBUSY; |
| 3865 | |
| 3866 | if (cmd_match(buf, "none")) { |
| 3867 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3868 | mddev->external = 0; |
| 3869 | mddev->major_version = 0; |
| 3870 | mddev->minor_version = 90; |
| 3871 | return len; |
| 3872 | } |
| 3873 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3874 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3875 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3876 | namelen = sizeof(mddev->metadata_type)-1; |
| 3877 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3878 | mddev->metadata_type[namelen] = 0; |
| 3879 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3880 | mddev->metadata_type[--namelen] = 0; |
| 3881 | mddev->persistent = 0; |
| 3882 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3883 | mddev->major_version = 0; |
| 3884 | mddev->minor_version = 90; |
| 3885 | return len; |
| 3886 | } |
| 3887 | major = simple_strtoul(buf, &e, 10); |
| 3888 | if (e==buf || *e != '.') |
| 3889 | return -EINVAL; |
| 3890 | buf = e+1; |
| 3891 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3892 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3893 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3894 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3895 | return -ENOENT; |
| 3896 | mddev->major_version = major; |
| 3897 | mddev->minor_version = minor; |
| 3898 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3899 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3900 | return len; |
| 3901 | } |
| 3902 | |
| 3903 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3904 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3905 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3906 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3907 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3908 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3909 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3910 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3911 | type = "frozen"; |
| 3912 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3913 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3914 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3915 | type = "reshape"; |
| 3916 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3917 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3918 | type = "resync"; |
| 3919 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3920 | type = "check"; |
| 3921 | else |
| 3922 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3923 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3924 | type = "recover"; |
| 3925 | } |
| 3926 | return sprintf(page, "%s\n", type); |
| 3927 | } |
| 3928 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3929 | static void reap_sync_thread(mddev_t *mddev); |
| 3930 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3931 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3932 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3933 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3934 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3935 | return -EINVAL; |
| 3936 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3937 | if (cmd_match(page, "frozen")) |
| 3938 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3939 | else |
| 3940 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3941 | |
| 3942 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3943 | if (mddev->sync_thread) { |
| 3944 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3945 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3946 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3947 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3948 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3949 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3950 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3951 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3952 | else if (cmd_match(page, "recover")) { |
| 3953 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3954 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3955 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3956 | int err; |
| 3957 | if (mddev->pers->start_reshape == NULL) |
| 3958 | return -EINVAL; |
| 3959 | err = mddev->pers->start_reshape(mddev); |
| 3960 | if (err) |
| 3961 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3962 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3963 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3964 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3965 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3966 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3967 | return -EINVAL; |
| 3968 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3969 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3970 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3971 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3972 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3973 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3974 | return len; |
| 3975 | } |
| 3976 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3977 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3978 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3979 | { |
| 3980 | return sprintf(page, "%llu\n", |
| 3981 | (unsigned long long) mddev->resync_mismatches); |
| 3982 | } |
| 3983 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3984 | static struct md_sysfs_entry md_scan_mode = |
| 3985 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3986 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3987 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3988 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3989 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3990 | static ssize_t |
| 3991 | sync_min_show(mddev_t *mddev, char *page) |
| 3992 | { |
| 3993 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3994 | mddev->sync_speed_min ? "local": "system"); |
| 3995 | } |
| 3996 | |
| 3997 | static ssize_t |
| 3998 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3999 | { |
| 4000 | int min; |
| 4001 | char *e; |
| 4002 | if (strncmp(buf, "system", 6)==0) { |
| 4003 | mddev->sync_speed_min = 0; |
| 4004 | return len; |
| 4005 | } |
| 4006 | min = simple_strtoul(buf, &e, 10); |
| 4007 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4008 | return -EINVAL; |
| 4009 | mddev->sync_speed_min = min; |
| 4010 | return len; |
| 4011 | } |
| 4012 | |
| 4013 | static struct md_sysfs_entry md_sync_min = |
| 4014 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4015 | |
| 4016 | static ssize_t |
| 4017 | sync_max_show(mddev_t *mddev, char *page) |
| 4018 | { |
| 4019 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4020 | mddev->sync_speed_max ? "local": "system"); |
| 4021 | } |
| 4022 | |
| 4023 | static ssize_t |
| 4024 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 4025 | { |
| 4026 | int max; |
| 4027 | char *e; |
| 4028 | if (strncmp(buf, "system", 6)==0) { |
| 4029 | mddev->sync_speed_max = 0; |
| 4030 | return len; |
| 4031 | } |
| 4032 | max = simple_strtoul(buf, &e, 10); |
| 4033 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4034 | return -EINVAL; |
| 4035 | mddev->sync_speed_max = max; |
| 4036 | return len; |
| 4037 | } |
| 4038 | |
| 4039 | static struct md_sysfs_entry md_sync_max = |
| 4040 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4041 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4042 | static ssize_t |
| 4043 | degraded_show(mddev_t *mddev, char *page) |
| 4044 | { |
| 4045 | return sprintf(page, "%d\n", mddev->degraded); |
| 4046 | } |
| 4047 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4048 | |
| 4049 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4050 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 4051 | { |
| 4052 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4053 | } |
| 4054 | |
| 4055 | static ssize_t |
| 4056 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 4057 | { |
| 4058 | long n; |
| 4059 | |
| 4060 | if (strict_strtol(buf, 10, &n)) |
| 4061 | return -EINVAL; |
| 4062 | |
| 4063 | if (n != 0 && n != 1) |
| 4064 | return -EINVAL; |
| 4065 | |
| 4066 | mddev->parallel_resync = n; |
| 4067 | |
| 4068 | if (mddev->sync_thread) |
| 4069 | wake_up(&resync_wait); |
| 4070 | |
| 4071 | return len; |
| 4072 | } |
| 4073 | |
| 4074 | /* force parallel resync, even with shared block devices */ |
| 4075 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4076 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4077 | sync_force_parallel_show, sync_force_parallel_store); |
| 4078 | |
| 4079 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4080 | sync_speed_show(mddev_t *mddev, char *page) |
| 4081 | { |
| 4082 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4083 | if (mddev->curr_resync == 0) |
| 4084 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4085 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4086 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4087 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4088 | db = resync - mddev->resync_mark_cnt; |
| 4089 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4090 | } |
| 4091 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4092 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4093 | |
| 4094 | static ssize_t |
| 4095 | sync_completed_show(mddev_t *mddev, char *page) |
| 4096 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4097 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4098 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4099 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4100 | return sprintf(page, "none\n"); |
| 4101 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4102 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4103 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4104 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4105 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4106 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4107 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4108 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4109 | } |
| 4110 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4111 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4112 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4113 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4114 | min_sync_show(mddev_t *mddev, char *page) |
| 4115 | { |
| 4116 | return sprintf(page, "%llu\n", |
| 4117 | (unsigned long long)mddev->resync_min); |
| 4118 | } |
| 4119 | static ssize_t |
| 4120 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4121 | { |
| 4122 | unsigned long long min; |
| 4123 | if (strict_strtoull(buf, 10, &min)) |
| 4124 | return -EINVAL; |
| 4125 | if (min > mddev->resync_max) |
| 4126 | return -EINVAL; |
| 4127 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4128 | return -EBUSY; |
| 4129 | |
| 4130 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4131 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4132 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4133 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4134 | return -EINVAL; |
| 4135 | } |
| 4136 | mddev->resync_min = min; |
| 4137 | |
| 4138 | return len; |
| 4139 | } |
| 4140 | |
| 4141 | static struct md_sysfs_entry md_min_sync = |
| 4142 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4143 | |
| 4144 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4145 | max_sync_show(mddev_t *mddev, char *page) |
| 4146 | { |
| 4147 | if (mddev->resync_max == MaxSector) |
| 4148 | return sprintf(page, "max\n"); |
| 4149 | else |
| 4150 | return sprintf(page, "%llu\n", |
| 4151 | (unsigned long long)mddev->resync_max); |
| 4152 | } |
| 4153 | static ssize_t |
| 4154 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4155 | { |
| 4156 | if (strncmp(buf, "max", 3) == 0) |
| 4157 | mddev->resync_max = MaxSector; |
| 4158 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4159 | unsigned long long max; |
| 4160 | if (strict_strtoull(buf, 10, &max)) |
| 4161 | return -EINVAL; |
| 4162 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4163 | return -EINVAL; |
| 4164 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4165 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4166 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4167 | return -EBUSY; |
| 4168 | |
| 4169 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4170 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4171 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4172 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4173 | return -EINVAL; |
| 4174 | } |
| 4175 | mddev->resync_max = max; |
| 4176 | } |
| 4177 | wake_up(&mddev->recovery_wait); |
| 4178 | return len; |
| 4179 | } |
| 4180 | |
| 4181 | static struct md_sysfs_entry md_max_sync = |
| 4182 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4183 | |
| 4184 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4185 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4186 | { |
| 4187 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4188 | } |
| 4189 | |
| 4190 | static ssize_t |
| 4191 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4192 | { |
| 4193 | char *e; |
| 4194 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4195 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4196 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4197 | if (mddev->pers == NULL || |
| 4198 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4199 | return -EINVAL; |
| 4200 | if (buf == e || (*e && *e != '\n')) |
| 4201 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4202 | |
| 4203 | mddev->suspend_lo = new; |
| 4204 | if (new >= old) |
| 4205 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4206 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4207 | else { |
| 4208 | /* Expanding suspended region - need to wait */ |
| 4209 | mddev->pers->quiesce(mddev, 1); |
| 4210 | mddev->pers->quiesce(mddev, 0); |
| 4211 | } |
| 4212 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4213 | } |
| 4214 | static struct md_sysfs_entry md_suspend_lo = |
| 4215 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4216 | |
| 4217 | |
| 4218 | static ssize_t |
| 4219 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4220 | { |
| 4221 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4222 | } |
| 4223 | |
| 4224 | static ssize_t |
| 4225 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4226 | { |
| 4227 | char *e; |
| 4228 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4229 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4230 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4231 | if (mddev->pers == NULL || |
| 4232 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4233 | return -EINVAL; |
| 4234 | if (buf == e || (*e && *e != '\n')) |
| 4235 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4236 | |
| 4237 | mddev->suspend_hi = new; |
| 4238 | if (new <= old) |
| 4239 | /* Shrinking suspended region */ |
| 4240 | mddev->pers->quiesce(mddev, 2); |
| 4241 | else { |
| 4242 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4243 | mddev->pers->quiesce(mddev, 1); |
| 4244 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4245 | } |
| 4246 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4247 | } |
| 4248 | static struct md_sysfs_entry md_suspend_hi = |
| 4249 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4250 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4251 | static ssize_t |
| 4252 | reshape_position_show(mddev_t *mddev, char *page) |
| 4253 | { |
| 4254 | if (mddev->reshape_position != MaxSector) |
| 4255 | return sprintf(page, "%llu\n", |
| 4256 | (unsigned long long)mddev->reshape_position); |
| 4257 | strcpy(page, "none\n"); |
| 4258 | return 5; |
| 4259 | } |
| 4260 | |
| 4261 | static ssize_t |
| 4262 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4263 | { |
| 4264 | char *e; |
| 4265 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4266 | if (mddev->pers) |
| 4267 | return -EBUSY; |
| 4268 | if (buf == e || (*e && *e != '\n')) |
| 4269 | return -EINVAL; |
| 4270 | mddev->reshape_position = new; |
| 4271 | mddev->delta_disks = 0; |
| 4272 | mddev->new_level = mddev->level; |
| 4273 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4274 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4275 | return len; |
| 4276 | } |
| 4277 | |
| 4278 | static struct md_sysfs_entry md_reshape_position = |
| 4279 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4280 | reshape_position_store); |
| 4281 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4282 | static ssize_t |
| 4283 | array_size_show(mddev_t *mddev, char *page) |
| 4284 | { |
| 4285 | if (mddev->external_size) |
| 4286 | return sprintf(page, "%llu\n", |
| 4287 | (unsigned long long)mddev->array_sectors/2); |
| 4288 | else |
| 4289 | return sprintf(page, "default\n"); |
| 4290 | } |
| 4291 | |
| 4292 | static ssize_t |
| 4293 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4294 | { |
| 4295 | sector_t sectors; |
| 4296 | |
| 4297 | if (strncmp(buf, "default", 7) == 0) { |
| 4298 | if (mddev->pers) |
| 4299 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4300 | else |
| 4301 | sectors = mddev->array_sectors; |
| 4302 | |
| 4303 | mddev->external_size = 0; |
| 4304 | } else { |
| 4305 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4306 | return -EINVAL; |
| 4307 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4308 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4309 | |
| 4310 | mddev->external_size = 1; |
| 4311 | } |
| 4312 | |
| 4313 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4314 | if (mddev->pers) { |
| 4315 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4316 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4317 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4318 | return len; |
| 4319 | } |
| 4320 | |
| 4321 | static struct md_sysfs_entry md_array_size = |
| 4322 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4323 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4324 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4325 | static struct attribute *md_default_attrs[] = { |
| 4326 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4327 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4328 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4329 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4330 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4331 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4332 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4333 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4334 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4335 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4336 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4337 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4338 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4339 | NULL, |
| 4340 | }; |
| 4341 | |
| 4342 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4343 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4344 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4345 | &md_sync_min.attr, |
| 4346 | &md_sync_max.attr, |
| 4347 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4348 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4349 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4350 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4351 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4352 | &md_suspend_lo.attr, |
| 4353 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4354 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4355 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4356 | NULL, |
| 4357 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4358 | static struct attribute_group md_redundancy_group = { |
| 4359 | .name = NULL, |
| 4360 | .attrs = md_redundancy_attrs, |
| 4361 | }; |
| 4362 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4363 | |
| 4364 | static ssize_t |
| 4365 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4366 | { |
| 4367 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4368 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4369 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4370 | |
| 4371 | if (!entry->show) |
| 4372 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4373 | rv = mddev_lock(mddev); |
| 4374 | if (!rv) { |
| 4375 | rv = entry->show(mddev, page); |
| 4376 | mddev_unlock(mddev); |
| 4377 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4378 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | static ssize_t |
| 4382 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4383 | const char *page, size_t length) |
| 4384 | { |
| 4385 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4386 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4387 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4388 | |
| 4389 | if (!entry->store) |
| 4390 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4391 | if (!capable(CAP_SYS_ADMIN)) |
| 4392 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4393 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4394 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4395 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4396 | if (!rv) { |
| 4397 | rv = entry->store(mddev, page, length); |
| 4398 | mddev_unlock(mddev); |
| 4399 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4400 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | static void md_free(struct kobject *ko) |
| 4404 | { |
| 4405 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4406 | |
| 4407 | if (mddev->sysfs_state) |
| 4408 | sysfs_put(mddev->sysfs_state); |
| 4409 | |
| 4410 | if (mddev->gendisk) { |
| 4411 | del_gendisk(mddev->gendisk); |
| 4412 | put_disk(mddev->gendisk); |
| 4413 | } |
| 4414 | if (mddev->queue) |
| 4415 | blk_cleanup_queue(mddev->queue); |
| 4416 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4417 | kfree(mddev); |
| 4418 | } |
| 4419 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4420 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4421 | .show = md_attr_show, |
| 4422 | .store = md_attr_store, |
| 4423 | }; |
| 4424 | static struct kobj_type md_ktype = { |
| 4425 | .release = md_free, |
| 4426 | .sysfs_ops = &md_sysfs_ops, |
| 4427 | .default_attrs = md_default_attrs, |
| 4428 | }; |
| 4429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | int mdp_major = 0; |
| 4431 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4432 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4433 | { |
| 4434 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4435 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4436 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4437 | kobject_del(&mddev->kobj); |
| 4438 | kobject_put(&mddev->kobj); |
| 4439 | } |
| 4440 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4441 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4443 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | mddev_t *mddev = mddev_find(dev); |
| 4445 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4446 | int partitioned; |
| 4447 | int shift; |
| 4448 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4449 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
| 4451 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4452 | return -ENODEV; |
| 4453 | |
| 4454 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4455 | shift = partitioned ? MdpMinorShift : 0; |
| 4456 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4458 | /* wait for any previous instance of this device to be |
| 4459 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4460 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4461 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4462 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4463 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4464 | error = -EEXIST; |
| 4465 | if (mddev->gendisk) |
| 4466 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4467 | |
| 4468 | if (name) { |
| 4469 | /* Need to ensure that 'name' is not a duplicate. |
| 4470 | */ |
| 4471 | mddev_t *mddev2; |
| 4472 | spin_lock(&all_mddevs_lock); |
| 4473 | |
| 4474 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4475 | if (mddev2->gendisk && |
| 4476 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4477 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4478 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4479 | } |
| 4480 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4482 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4483 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4484 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4485 | if (!mddev->queue) |
| 4486 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4487 | mddev->queue->queuedata = mddev; |
| 4488 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4489 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | disk = alloc_disk(1 << shift); |
| 4492 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4493 | blk_cleanup_queue(mddev->queue); |
| 4494 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4495 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4497 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4499 | if (name) |
| 4500 | strcpy(disk->disk_name, name); |
| 4501 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4503 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | disk->fops = &md_fops; |
| 4506 | disk->private_data = mddev; |
| 4507 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4508 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4509 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4510 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4511 | * remove it now. |
| 4512 | */ |
| 4513 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4515 | /* As soon as we call add_disk(), another thread could get |
| 4516 | * through to md_open, so make sure it doesn't get too far |
| 4517 | */ |
| 4518 | mutex_lock(&mddev->open_mutex); |
| 4519 | add_disk(disk); |
| 4520 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4521 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4522 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4523 | if (error) { |
| 4524 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4525 | * __must_check, we must do something with the result |
| 4526 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4527 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4528 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4529 | error = 0; |
| 4530 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4531 | if (mddev->kobj.sd && |
| 4532 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4533 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4534 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4535 | abort: |
| 4536 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4537 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4538 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4539 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4540 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4541 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4542 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4546 | { |
| 4547 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | return NULL; |
| 4549 | } |
| 4550 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4551 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4552 | { |
| 4553 | /* val must be "md_*" where * is not all digits. |
| 4554 | * We allocate an array with a large free minor number, and |
| 4555 | * set the name to val. val must not already be an active name. |
| 4556 | */ |
| 4557 | int len = strlen(val); |
| 4558 | char buf[DISK_NAME_LEN]; |
| 4559 | |
| 4560 | while (len && val[len-1] == '\n') |
| 4561 | len--; |
| 4562 | if (len >= DISK_NAME_LEN) |
| 4563 | return -E2BIG; |
| 4564 | strlcpy(buf, val, len+1); |
| 4565 | if (strncmp(buf, "md_", 3) != 0) |
| 4566 | return -EINVAL; |
| 4567 | return md_alloc(0, buf); |
| 4568 | } |
| 4569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | static void md_safemode_timeout(unsigned long data) |
| 4571 | { |
| 4572 | mddev_t *mddev = (mddev_t *) data; |
| 4573 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4574 | if (!atomic_read(&mddev->writes_pending)) { |
| 4575 | mddev->safemode = 1; |
| 4576 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4577 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | md_wakeup_thread(mddev->thread); |
| 4580 | } |
| 4581 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4582 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4584 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4586 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4588 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4590 | if (list_empty(&mddev->disks)) |
| 4591 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
| 4594 | if (mddev->pers) |
| 4595 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4596 | /* Cannot run until previous stop completes properly */ |
| 4597 | if (mddev->sysfs_active) |
| 4598 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | /* |
| 4601 | * Analyze all RAID superblock(s) |
| 4602 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4603 | if (!mddev->raid_disks) { |
| 4604 | if (!mddev->persistent) |
| 4605 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4606 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4609 | if (mddev->level != LEVEL_NONE) |
| 4610 | request_module("md-level-%d", mddev->level); |
| 4611 | else if (mddev->clevel[0]) |
| 4612 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | |
| 4614 | /* |
| 4615 | * Drop all container device buffers, from now on |
| 4616 | * the only valid external interface is through the md |
| 4617 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4619 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4620 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | continue; |
| 4622 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4623 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4624 | |
| 4625 | /* perform some consistency tests on the device. |
| 4626 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4627 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4628 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4629 | if (rdev->meta_bdev) { |
| 4630 | /* Nothing to check */; |
| 4631 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4632 | if (mddev->dev_sectors && |
| 4633 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4634 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4635 | printk("md: %s: data overlaps metadata\n", |
| 4636 | mdname(mddev)); |
| 4637 | return -EINVAL; |
| 4638 | } |
| 4639 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4640 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4641 | > rdev->data_offset) { |
| 4642 | printk("md: %s: metadata overlaps data\n", |
| 4643 | mdname(mddev)); |
| 4644 | return -EINVAL; |
| 4645 | } |
| 4646 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4647 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4650 | if (mddev->bio_set == NULL) |
NeilBrown | a519b26 | 2011-07-28 07:56:24 +1000 | [diff] [blame] | 4651 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, |
| 4652 | sizeof(mddev_t *)); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4655 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4656 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4658 | if (mddev->level != LEVEL_NONE) |
| 4659 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4660 | mddev->level); |
| 4661 | else |
| 4662 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4663 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | return -EINVAL; |
| 4665 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4666 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4668 | if (mddev->level != pers->level) { |
| 4669 | mddev->level = pers->level; |
| 4670 | mddev->new_level = pers->level; |
| 4671 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4672 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4674 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4675 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4676 | /* This personality cannot handle reshaping... */ |
| 4677 | mddev->pers = NULL; |
| 4678 | module_put(pers->owner); |
| 4679 | return -EINVAL; |
| 4680 | } |
| 4681 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4682 | if (pers->sync_request) { |
| 4683 | /* Warn if this is a potentially silly |
| 4684 | * configuration. |
| 4685 | */ |
| 4686 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4687 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4688 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4689 | |
| 4690 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4691 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4692 | if (rdev < rdev2 && |
| 4693 | rdev->bdev->bd_contains == |
| 4694 | rdev2->bdev->bd_contains) { |
| 4695 | printk(KERN_WARNING |
| 4696 | "%s: WARNING: %s appears to be" |
| 4697 | " on the same physical disk as" |
| 4698 | " %s.\n", |
| 4699 | mdname(mddev), |
| 4700 | bdevname(rdev->bdev,b), |
| 4701 | bdevname(rdev2->bdev,b2)); |
| 4702 | warned = 1; |
| 4703 | } |
| 4704 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4705 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4706 | if (warned) |
| 4707 | printk(KERN_WARNING |
| 4708 | "True protection against single-disk" |
| 4709 | " failure might be compromised.\n"); |
| 4710 | } |
| 4711 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4712 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4713 | /* may be over-ridden by personality */ |
| 4714 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4715 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4716 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4718 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4719 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4720 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4721 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4722 | if (err) |
| 4723 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4724 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4725 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4726 | " but 'external_size' not in effect?\n", __func__); |
| 4727 | printk(KERN_ERR |
| 4728 | "md: invalid array_size %llu > default size %llu\n", |
| 4729 | (unsigned long long)mddev->array_sectors / 2, |
| 4730 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4731 | err = -EINVAL; |
| 4732 | mddev->pers->stop(mddev); |
| 4733 | } |
| 4734 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4735 | err = bitmap_create(mddev); |
| 4736 | if (err) { |
| 4737 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4738 | mdname(mddev), err); |
| 4739 | mddev->pers->stop(mddev); |
| 4740 | } |
| 4741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | module_put(mddev->pers->owner); |
| 4744 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4745 | bitmap_destroy(mddev); |
| 4746 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4748 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4749 | if (mddev->kobj.sd && |
| 4750 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4751 | printk(KERN_WARNING |
| 4752 | "md: cannot register extra attributes for %s\n", |
| 4753 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4754 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4755 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4756 | mddev->ro = 0; |
| 4757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4759 | atomic_set(&mddev->max_corr_read_errors, |
| 4760 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | mddev->safemode = 0; |
| 4762 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4763 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4764 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4766 | smp_wmb(); |
| 4767 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4768 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4769 | if (rdev->raid_disk >= 0) |
| 4770 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4771 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
| 4773 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4774 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4775 | if (mddev->flags) |
| 4776 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4778 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4779 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4780 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4781 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4782 | return 0; |
| 4783 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4784 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4786 | static int do_md_run(mddev_t *mddev) |
| 4787 | { |
| 4788 | int err; |
| 4789 | |
| 4790 | err = md_run(mddev); |
| 4791 | if (err) |
| 4792 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4793 | err = bitmap_load(mddev); |
| 4794 | if (err) { |
| 4795 | bitmap_destroy(mddev); |
| 4796 | goto out; |
| 4797 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4798 | |
| 4799 | md_wakeup_thread(mddev->thread); |
| 4800 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4801 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4802 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4803 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4804 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4805 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4806 | out: |
| 4807 | return err; |
| 4808 | } |
| 4809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | static int restart_array(mddev_t *mddev) |
| 4811 | { |
| 4812 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4814 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4816 | return -ENXIO; |
| 4817 | if (!mddev->pers) |
| 4818 | return -EINVAL; |
| 4819 | if (!mddev->ro) |
| 4820 | return -EBUSY; |
| 4821 | mddev->safemode = 0; |
| 4822 | mddev->ro = 0; |
| 4823 | set_disk_ro(disk, 0); |
| 4824 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4825 | mdname(mddev)); |
| 4826 | /* Kick recovery or resync if necessary */ |
| 4827 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4828 | md_wakeup_thread(mddev->thread); |
| 4829 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4830 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4831 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | } |
| 4833 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4834 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4835 | * to the file ourselves */ |
| 4836 | static int deny_bitmap_write_access(struct file * file) |
| 4837 | { |
| 4838 | struct inode *inode = file->f_mapping->host; |
| 4839 | |
| 4840 | spin_lock(&inode->i_lock); |
| 4841 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4842 | spin_unlock(&inode->i_lock); |
| 4843 | return -ETXTBSY; |
| 4844 | } |
| 4845 | atomic_set(&inode->i_writecount, -1); |
| 4846 | spin_unlock(&inode->i_lock); |
| 4847 | |
| 4848 | return 0; |
| 4849 | } |
| 4850 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4851 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4852 | { |
| 4853 | struct inode *inode = file->f_mapping->host; |
| 4854 | |
| 4855 | spin_lock(&inode->i_lock); |
| 4856 | atomic_set(&inode->i_writecount, 1); |
| 4857 | spin_unlock(&inode->i_lock); |
| 4858 | } |
| 4859 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4860 | static void md_clean(mddev_t *mddev) |
| 4861 | { |
| 4862 | mddev->array_sectors = 0; |
| 4863 | mddev->external_size = 0; |
| 4864 | mddev->dev_sectors = 0; |
| 4865 | mddev->raid_disks = 0; |
| 4866 | mddev->recovery_cp = 0; |
| 4867 | mddev->resync_min = 0; |
| 4868 | mddev->resync_max = MaxSector; |
| 4869 | mddev->reshape_position = MaxSector; |
| 4870 | mddev->external = 0; |
| 4871 | mddev->persistent = 0; |
| 4872 | mddev->level = LEVEL_NONE; |
| 4873 | mddev->clevel[0] = 0; |
| 4874 | mddev->flags = 0; |
| 4875 | mddev->ro = 0; |
| 4876 | mddev->metadata_type[0] = 0; |
| 4877 | mddev->chunk_sectors = 0; |
| 4878 | mddev->ctime = mddev->utime = 0; |
| 4879 | mddev->layout = 0; |
| 4880 | mddev->max_disks = 0; |
| 4881 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4882 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4883 | mddev->delta_disks = 0; |
| 4884 | mddev->new_level = LEVEL_NONE; |
| 4885 | mddev->new_layout = 0; |
| 4886 | mddev->new_chunk_sectors = 0; |
| 4887 | mddev->curr_resync = 0; |
| 4888 | mddev->resync_mismatches = 0; |
| 4889 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4890 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4891 | mddev->recovery = 0; |
| 4892 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4893 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4894 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4895 | mddev->safemode = 0; |
| 4896 | mddev->bitmap_info.offset = 0; |
| 4897 | mddev->bitmap_info.default_offset = 0; |
| 4898 | mddev->bitmap_info.chunksize = 0; |
| 4899 | mddev->bitmap_info.daemon_sleep = 0; |
| 4900 | mddev->bitmap_info.max_write_behind = 0; |
| 4901 | } |
| 4902 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4903 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4904 | { |
| 4905 | if (mddev->sync_thread) { |
| 4906 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4907 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4908 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | del_timer_sync(&mddev->safemode_timer); |
| 4912 | |
| 4913 | bitmap_flush(mddev); |
| 4914 | md_super_wait(mddev); |
| 4915 | |
| 4916 | if (!mddev->in_sync || mddev->flags) { |
| 4917 | /* mark array as shutdown cleanly */ |
| 4918 | mddev->in_sync = 1; |
| 4919 | md_update_sb(mddev, 1); |
| 4920 | } |
| 4921 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4922 | |
| 4923 | void md_stop_writes(mddev_t *mddev) |
| 4924 | { |
| 4925 | mddev_lock(mddev); |
| 4926 | __md_stop_writes(mddev); |
| 4927 | mddev_unlock(mddev); |
| 4928 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4929 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4930 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4931 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4932 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4933 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4934 | mddev->pers->stop(mddev); |
| 4935 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4936 | mddev->to_remove = &md_redundancy_group; |
| 4937 | module_put(mddev->pers->owner); |
| 4938 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4939 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4940 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4941 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4942 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4943 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4944 | { |
| 4945 | int err = 0; |
| 4946 | mutex_lock(&mddev->open_mutex); |
| 4947 | if (atomic_read(&mddev->openers) > is_open) { |
| 4948 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4949 | err = -EBUSY; |
| 4950 | goto out; |
| 4951 | } |
| 4952 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4953 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4954 | |
| 4955 | err = -ENXIO; |
| 4956 | if (mddev->ro==1) |
| 4957 | goto out; |
| 4958 | mddev->ro = 1; |
| 4959 | set_disk_ro(mddev->gendisk, 1); |
| 4960 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4961 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4962 | err = 0; |
| 4963 | } |
| 4964 | out: |
| 4965 | mutex_unlock(&mddev->open_mutex); |
| 4966 | return err; |
| 4967 | } |
| 4968 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4969 | /* mode: |
| 4970 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4971 | * 2 - stop but do not disassemble array |
| 4972 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4973 | 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] | 4974 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4976 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4978 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4979 | if (atomic_read(&mddev->openers) > is_open || |
| 4980 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4981 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4982 | mutex_unlock(&mddev->open_mutex); |
| 4983 | return -EBUSY; |
| 4984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4986 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4987 | if (mddev->ro) |
| 4988 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4989 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4990 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4991 | md_stop(mddev); |
| 4992 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4993 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4994 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4995 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4996 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4997 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4998 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4999 | if (rdev->raid_disk >= 0) |
| 5000 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5001 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5002 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5003 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5004 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5005 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5006 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5007 | if (mddev->ro) |
| 5008 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5009 | } else |
| 5010 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | /* |
| 5012 | * Free resources if final stop |
| 5013 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5014 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5016 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5017 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5018 | if (mddev->bitmap_info.file) { |
| 5019 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5020 | fput(mddev->bitmap_info.file); |
| 5021 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5022 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5023 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | export_array(mddev); |
| 5026 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5027 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5028 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5029 | if (mddev->hold_active == UNTIL_STOP) |
| 5030 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5031 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5032 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5033 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5034 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5035 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | } |
| 5037 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5038 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | static void autorun_array(mddev_t *mddev) |
| 5040 | { |
| 5041 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | int err; |
| 5043 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5044 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5045 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
| 5047 | printk(KERN_INFO "md: running: "); |
| 5048 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5049 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | char b[BDEVNAME_SIZE]; |
| 5051 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5052 | } |
| 5053 | printk("\n"); |
| 5054 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5055 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | if (err) { |
| 5057 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5058 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | /* |
| 5063 | * lets try to run arrays based on all disks that have arrived |
| 5064 | * until now. (those are in pending_raid_disks) |
| 5065 | * |
| 5066 | * the method: pick the first pending disk, collect all disks with |
| 5067 | * the same UUID, remove all from the pending list and put them into |
| 5068 | * the 'same_array' list. Then order this list based on superblock |
| 5069 | * update time (freshest comes first), kick out 'old' disks and |
| 5070 | * compare superblocks. If everything's fine then run it. |
| 5071 | * |
| 5072 | * If "unit" is allocated, then bump its reference count |
| 5073 | */ |
| 5074 | static void autorun_devices(int part) |
| 5075 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5076 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | mddev_t *mddev; |
| 5078 | char b[BDEVNAME_SIZE]; |
| 5079 | |
| 5080 | printk(KERN_INFO "md: autorun ...\n"); |
| 5081 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5082 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5084 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | rdev0 = list_entry(pending_raid_disks.next, |
| 5086 | mdk_rdev_t, same_set); |
| 5087 | |
| 5088 | printk(KERN_INFO "md: considering %s ...\n", |
| 5089 | bdevname(rdev0->bdev,b)); |
| 5090 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5091 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5093 | printk(KERN_INFO "md: adding %s ...\n", |
| 5094 | bdevname(rdev->bdev,b)); |
| 5095 | list_move(&rdev->same_set, &candidates); |
| 5096 | } |
| 5097 | /* |
| 5098 | * now we have a set of devices, with all of them having |
| 5099 | * mostly sane superblocks. It's time to allocate the |
| 5100 | * mddev. |
| 5101 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5102 | if (part) { |
| 5103 | dev = MKDEV(mdp_major, |
| 5104 | rdev0->preferred_minor << MdpMinorShift); |
| 5105 | unit = MINOR(dev) >> MdpMinorShift; |
| 5106 | } else { |
| 5107 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5108 | unit = MINOR(dev); |
| 5109 | } |
| 5110 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5112 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5113 | break; |
| 5114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | |
| 5116 | md_probe(dev, NULL, NULL); |
| 5117 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5118 | if (!mddev || !mddev->gendisk) { |
| 5119 | if (mddev) |
| 5120 | mddev_put(mddev); |
| 5121 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | "md: cannot allocate memory for md drive.\n"); |
| 5123 | break; |
| 5124 | } |
| 5125 | if (mddev_lock(mddev)) |
| 5126 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5127 | mdname(mddev)); |
| 5128 | else if (mddev->raid_disks || mddev->major_version |
| 5129 | || !list_empty(&mddev->disks)) { |
| 5130 | printk(KERN_WARNING |
| 5131 | "md: %s already running, cannot run %s\n", |
| 5132 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5133 | mddev_unlock(mddev); |
| 5134 | } else { |
| 5135 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5136 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5137 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | list_del_init(&rdev->same_set); |
| 5139 | if (bind_rdev_to_array(rdev, mddev)) |
| 5140 | export_rdev(rdev); |
| 5141 | } |
| 5142 | autorun_array(mddev); |
| 5143 | mddev_unlock(mddev); |
| 5144 | } |
| 5145 | /* on success, candidates will be empty, on error |
| 5146 | * it won't... |
| 5147 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5148 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5149 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | mddev_put(mddev); |
| 5153 | } |
| 5154 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5155 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5156 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | static int get_version(void __user * arg) |
| 5159 | { |
| 5160 | mdu_version_t ver; |
| 5161 | |
| 5162 | ver.major = MD_MAJOR_VERSION; |
| 5163 | ver.minor = MD_MINOR_VERSION; |
| 5164 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5165 | |
| 5166 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5167 | return -EFAULT; |
| 5168 | |
| 5169 | return 0; |
| 5170 | } |
| 5171 | |
| 5172 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5173 | { |
| 5174 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5175 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5178 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5179 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5181 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | failed++; |
| 5183 | else { |
| 5184 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5185 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5186 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | else |
| 5188 | spare++; |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | info.major_version = mddev->major_version; |
| 5193 | info.minor_version = mddev->minor_version; |
| 5194 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5195 | info.ctime = mddev->ctime; |
| 5196 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5197 | info.size = mddev->dev_sectors / 2; |
| 5198 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5199 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | info.nr_disks = nr; |
| 5201 | info.raid_disks = mddev->raid_disks; |
| 5202 | info.md_minor = mddev->md_minor; |
| 5203 | info.not_persistent= !mddev->persistent; |
| 5204 | |
| 5205 | info.utime = mddev->utime; |
| 5206 | info.state = 0; |
| 5207 | if (mddev->in_sync) |
| 5208 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5209 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5210 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5211 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | info.working_disks = working; |
| 5213 | info.failed_disks = failed; |
| 5214 | info.spare_disks = spare; |
| 5215 | |
| 5216 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5217 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
| 5219 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5220 | return -EFAULT; |
| 5221 | |
| 5222 | return 0; |
| 5223 | } |
| 5224 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5225 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5226 | { |
| 5227 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5228 | char *ptr, *buf = NULL; |
| 5229 | int err = -ENOMEM; |
| 5230 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5231 | if (md_allow_write(mddev)) |
| 5232 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5233 | else |
| 5234 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5235 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5236 | if (!file) |
| 5237 | goto out; |
| 5238 | |
| 5239 | /* bitmap disabled, zero the first byte and copy out */ |
| 5240 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5241 | file->pathname[0] = '\0'; |
| 5242 | goto copy_out; |
| 5243 | } |
| 5244 | |
| 5245 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5246 | if (!buf) |
| 5247 | goto out; |
| 5248 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5249 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5250 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5251 | goto out; |
| 5252 | |
| 5253 | strcpy(file->pathname, ptr); |
| 5254 | |
| 5255 | copy_out: |
| 5256 | err = 0; |
| 5257 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5258 | err = -EFAULT; |
| 5259 | out: |
| 5260 | kfree(buf); |
| 5261 | kfree(file); |
| 5262 | return err; |
| 5263 | } |
| 5264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5266 | { |
| 5267 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | mdk_rdev_t *rdev; |
| 5269 | |
| 5270 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5271 | return -EFAULT; |
| 5272 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5273 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | if (rdev) { |
| 5275 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5276 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5277 | info.raid_disk = rdev->raid_disk; |
| 5278 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5279 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5281 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5283 | info.state |= (1<<MD_DISK_SYNC); |
| 5284 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5285 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5286 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | } else { |
| 5288 | info.major = info.minor = 0; |
| 5289 | info.raid_disk = -1; |
| 5290 | info.state = (1<<MD_DISK_REMOVED); |
| 5291 | } |
| 5292 | |
| 5293 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5294 | return -EFAULT; |
| 5295 | |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
| 5299 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5300 | { |
| 5301 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5302 | mdk_rdev_t *rdev; |
| 5303 | dev_t dev = MKDEV(info->major,info->minor); |
| 5304 | |
| 5305 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5306 | return -EOVERFLOW; |
| 5307 | |
| 5308 | if (!mddev->raid_disks) { |
| 5309 | int err; |
| 5310 | /* expecting a device which has a superblock */ |
| 5311 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5312 | if (IS_ERR(rdev)) { |
| 5313 | printk(KERN_WARNING |
| 5314 | "md: md_import_device returned %ld\n", |
| 5315 | PTR_ERR(rdev)); |
| 5316 | return PTR_ERR(rdev); |
| 5317 | } |
| 5318 | if (!list_empty(&mddev->disks)) { |
| 5319 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5320 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5321 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5323 | if (err < 0) { |
| 5324 | printk(KERN_WARNING |
| 5325 | "md: %s has different UUID to %s\n", |
| 5326 | bdevname(rdev->bdev,b), |
| 5327 | bdevname(rdev0->bdev,b2)); |
| 5328 | export_rdev(rdev); |
| 5329 | return -EINVAL; |
| 5330 | } |
| 5331 | } |
| 5332 | err = bind_rdev_to_array(rdev, mddev); |
| 5333 | if (err) |
| 5334 | export_rdev(rdev); |
| 5335 | return err; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | * add_new_disk can be used once the array is assembled |
| 5340 | * to add "hot spares". They must already have a superblock |
| 5341 | * written |
| 5342 | */ |
| 5343 | if (mddev->pers) { |
| 5344 | int err; |
| 5345 | if (!mddev->pers->hot_add_disk) { |
| 5346 | printk(KERN_WARNING |
| 5347 | "%s: personality does not support diskops!\n", |
| 5348 | mdname(mddev)); |
| 5349 | return -EINVAL; |
| 5350 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5351 | if (mddev->persistent) |
| 5352 | rdev = md_import_device(dev, mddev->major_version, |
| 5353 | mddev->minor_version); |
| 5354 | else |
| 5355 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | if (IS_ERR(rdev)) { |
| 5357 | printk(KERN_WARNING |
| 5358 | "md: md_import_device returned %ld\n", |
| 5359 | PTR_ERR(rdev)); |
| 5360 | return PTR_ERR(rdev); |
| 5361 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5362 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5363 | if (!mddev->persistent) { |
| 5364 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5365 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5366 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5367 | set_bit(In_sync, &rdev->flags); |
| 5368 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5369 | rdev->raid_disk = -1; |
| 5370 | } else |
| 5371 | super_types[mddev->major_version]. |
| 5372 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5373 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5374 | (!test_bit(In_sync, &rdev->flags) || |
| 5375 | rdev->raid_disk != info->raid_disk)) { |
| 5376 | /* This was a hot-add request, but events doesn't |
| 5377 | * match, so reject it. |
| 5378 | */ |
| 5379 | export_rdev(rdev); |
| 5380 | return -EINVAL; |
| 5381 | } |
| 5382 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5383 | if (test_bit(In_sync, &rdev->flags)) |
| 5384 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5385 | else |
| 5386 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5387 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5388 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5389 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5390 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5391 | else |
| 5392 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | rdev->raid_disk = -1; |
| 5395 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5396 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5397 | /* If there is hot_add_disk but no hot_remove_disk |
| 5398 | * then added disks for geometry changes, |
| 5399 | * and should be added immediately. |
| 5400 | */ |
| 5401 | super_types[mddev->major_version]. |
| 5402 | validate_super(mddev, rdev); |
| 5403 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5404 | if (err) |
| 5405 | unbind_rdev_from_array(rdev); |
| 5406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | if (err) |
| 5408 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5409 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5410 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5411 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5412 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5413 | if (mddev->degraded) |
| 5414 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5415 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5416 | if (!err) |
| 5417 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5418 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | return err; |
| 5420 | } |
| 5421 | |
| 5422 | /* otherwise, add_new_disk is only allowed |
| 5423 | * for major_version==0 superblocks |
| 5424 | */ |
| 5425 | if (mddev->major_version != 0) { |
| 5426 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5427 | mdname(mddev)); |
| 5428 | return -EINVAL; |
| 5429 | } |
| 5430 | |
| 5431 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5432 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5433 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | if (IS_ERR(rdev)) { |
| 5435 | printk(KERN_WARNING |
| 5436 | "md: error, md_import_device() returned %ld\n", |
| 5437 | PTR_ERR(rdev)); |
| 5438 | return PTR_ERR(rdev); |
| 5439 | } |
| 5440 | rdev->desc_nr = info->number; |
| 5441 | if (info->raid_disk < mddev->raid_disks) |
| 5442 | rdev->raid_disk = info->raid_disk; |
| 5443 | else |
| 5444 | rdev->raid_disk = -1; |
| 5445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5447 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5448 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5450 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5451 | set_bit(WriteMostly, &rdev->flags); |
| 5452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | if (!mddev->persistent) { |
| 5454 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5455 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5456 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5457 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5458 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5460 | err = bind_rdev_to_array(rdev, mddev); |
| 5461 | if (err) { |
| 5462 | export_rdev(rdev); |
| 5463 | return err; |
| 5464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
| 5470 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5471 | { |
| 5472 | char b[BDEVNAME_SIZE]; |
| 5473 | mdk_rdev_t *rdev; |
| 5474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | rdev = find_rdev(mddev, dev); |
| 5476 | if (!rdev) |
| 5477 | return -ENXIO; |
| 5478 | |
| 5479 | if (rdev->raid_disk >= 0) |
| 5480 | goto busy; |
| 5481 | |
| 5482 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5483 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5484 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | |
| 5486 | return 0; |
| 5487 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5488 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5490 | return -EBUSY; |
| 5491 | } |
| 5492 | |
| 5493 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5494 | { |
| 5495 | char b[BDEVNAME_SIZE]; |
| 5496 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | mdk_rdev_t *rdev; |
| 5498 | |
| 5499 | if (!mddev->pers) |
| 5500 | return -ENODEV; |
| 5501 | |
| 5502 | if (mddev->major_version != 0) { |
| 5503 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5504 | " version-0 superblocks.\n", |
| 5505 | mdname(mddev)); |
| 5506 | return -EINVAL; |
| 5507 | } |
| 5508 | if (!mddev->pers->hot_add_disk) { |
| 5509 | printk(KERN_WARNING |
| 5510 | "%s: personality does not support diskops!\n", |
| 5511 | mdname(mddev)); |
| 5512 | return -EINVAL; |
| 5513 | } |
| 5514 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5515 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | if (IS_ERR(rdev)) { |
| 5517 | printk(KERN_WARNING |
| 5518 | "md: error, md_import_device() returned %ld\n", |
| 5519 | PTR_ERR(rdev)); |
| 5520 | return -EINVAL; |
| 5521 | } |
| 5522 | |
| 5523 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5524 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5526 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5528 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5530 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | printk(KERN_WARNING |
| 5532 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5533 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5534 | err = -EINVAL; |
| 5535 | goto abort_export; |
| 5536 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5537 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5539 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5540 | err = bind_rdev_to_array(rdev, mddev); |
| 5541 | if (err) |
| 5542 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | |
| 5544 | /* |
| 5545 | * The rest should better be atomic, we can have disk failures |
| 5546 | * noticed in interrupt contexts ... |
| 5547 | */ |
| 5548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | rdev->raid_disk = -1; |
| 5550 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5551 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | |
| 5553 | /* |
| 5554 | * Kick recovery, maybe this spare has to be added to the |
| 5555 | * array immediately. |
| 5556 | */ |
| 5557 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5558 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5559 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | return 0; |
| 5561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | abort_export: |
| 5563 | export_rdev(rdev); |
| 5564 | return err; |
| 5565 | } |
| 5566 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5567 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5568 | { |
| 5569 | int err; |
| 5570 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5571 | if (mddev->pers) { |
| 5572 | if (!mddev->pers->quiesce) |
| 5573 | return -EBUSY; |
| 5574 | if (mddev->recovery || mddev->sync_thread) |
| 5575 | return -EBUSY; |
| 5576 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5577 | } |
| 5578 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5579 | |
| 5580 | if (fd >= 0) { |
| 5581 | if (mddev->bitmap) |
| 5582 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5583 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5584 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5585 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5586 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5587 | mdname(mddev)); |
| 5588 | return -EBADF; |
| 5589 | } |
| 5590 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5591 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5592 | if (err) { |
| 5593 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5594 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5595 | fput(mddev->bitmap_info.file); |
| 5596 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5597 | return err; |
| 5598 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5599 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5600 | } else if (mddev->bitmap == NULL) |
| 5601 | return -ENOENT; /* cannot remove what isn't there */ |
| 5602 | err = 0; |
| 5603 | if (mddev->pers) { |
| 5604 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5605 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5606 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5607 | if (!err) |
| 5608 | err = bitmap_load(mddev); |
| 5609 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5610 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5611 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5612 | fd = -1; /* make sure to put the file */ |
| 5613 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5614 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5615 | } |
| 5616 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5617 | if (mddev->bitmap_info.file) { |
| 5618 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5619 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5620 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5621 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5624 | return err; |
| 5625 | } |
| 5626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | /* |
| 5628 | * set_array_info is used two different ways |
| 5629 | * The original usage is when creating a new array. |
| 5630 | * In this usage, raid_disks is > 0 and it together with |
| 5631 | * level, size, not_persistent,layout,chunksize determine the |
| 5632 | * shape of the array. |
| 5633 | * This will always create an array with a type-0.90.0 superblock. |
| 5634 | * The newer usage is when assembling an array. |
| 5635 | * In this case raid_disks will be 0, and the major_version field is |
| 5636 | * use to determine which style super-blocks are to be found on the devices. |
| 5637 | * The minor and patch _version numbers are also kept incase the |
| 5638 | * super_block handler wishes to interpret them. |
| 5639 | */ |
| 5640 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5641 | { |
| 5642 | |
| 5643 | if (info->raid_disks == 0) { |
| 5644 | /* just setting version number for superblock loading */ |
| 5645 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5646 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | super_types[info->major_version].name == NULL) { |
| 5648 | /* maybe try to auto-load a module? */ |
| 5649 | printk(KERN_INFO |
| 5650 | "md: superblock version %d not known\n", |
| 5651 | info->major_version); |
| 5652 | return -EINVAL; |
| 5653 | } |
| 5654 | mddev->major_version = info->major_version; |
| 5655 | mddev->minor_version = info->minor_version; |
| 5656 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5657 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5658 | /* ensure mddev_put doesn't delete this now that there |
| 5659 | * is some minimal configuration. |
| 5660 | */ |
| 5661 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | return 0; |
| 5663 | } |
| 5664 | mddev->major_version = MD_MAJOR_VERSION; |
| 5665 | mddev->minor_version = MD_MINOR_VERSION; |
| 5666 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5667 | mddev->ctime = get_seconds(); |
| 5668 | |
| 5669 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5670 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5671 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | mddev->raid_disks = info->raid_disks; |
| 5673 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5674 | * openned |
| 5675 | */ |
| 5676 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5677 | mddev->recovery_cp = MaxSector; |
| 5678 | else |
| 5679 | mddev->recovery_cp = 0; |
| 5680 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5681 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | |
| 5683 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5684 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | |
| 5686 | mddev->max_disks = MD_SB_DISKS; |
| 5687 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5688 | if (mddev->persistent) |
| 5689 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5690 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5692 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5693 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5694 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5695 | mddev->reshape_position = MaxSector; |
| 5696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | /* |
| 5698 | * Generate a 128 bit UUID |
| 5699 | */ |
| 5700 | get_random_bytes(mddev->uuid, 16); |
| 5701 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5702 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5703 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5704 | mddev->new_layout = mddev->layout; |
| 5705 | mddev->delta_disks = 0; |
| 5706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | return 0; |
| 5708 | } |
| 5709 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5710 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5711 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5712 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5713 | |
| 5714 | if (mddev->external_size) |
| 5715 | return; |
| 5716 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5717 | mddev->array_sectors = array_sectors; |
| 5718 | } |
| 5719 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5720 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5721 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5722 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5723 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5724 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5725 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5726 | |
| 5727 | if (mddev->pers->resize == NULL) |
| 5728 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5729 | /* The "num_sectors" is the number of sectors of each device that |
| 5730 | * is used. This can only make sense for arrays with redundancy. |
| 5731 | * linear and raid0 always use whatever space is available. We can only |
| 5732 | * consider changing this number if no resync or reconstruction is |
| 5733 | * 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] | 5734 | * 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] | 5735 | * of each device. If num_sectors is zero, we find the largest size |
| 5736 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5737 | */ |
| 5738 | if (mddev->sync_thread) |
| 5739 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5740 | if (mddev->bitmap) |
| 5741 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5742 | * grow, and re-add. |
| 5743 | */ |
| 5744 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5745 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5746 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5747 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5748 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5749 | num_sectors = avail; |
| 5750 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5751 | return -ENOSPC; |
| 5752 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5753 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5754 | if (!rv) |
| 5755 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5756 | return rv; |
| 5757 | } |
| 5758 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5759 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5760 | { |
| 5761 | int rv; |
| 5762 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5763 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5764 | return -EINVAL; |
| 5765 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5766 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5767 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5768 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5769 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5770 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5771 | |
| 5772 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5773 | if (rv < 0) |
| 5774 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5775 | return rv; |
| 5776 | } |
| 5777 | |
| 5778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | /* |
| 5780 | * update_array_info is used to change the configuration of an |
| 5781 | * on-line array. |
| 5782 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5783 | * fields in the info are checked against the array. |
| 5784 | * Any differences that cannot be handled will cause an error. |
| 5785 | * Normally, only one change can be managed at a time. |
| 5786 | */ |
| 5787 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5788 | { |
| 5789 | int rv = 0; |
| 5790 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5791 | int state = 0; |
| 5792 | |
| 5793 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5794 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5795 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | |
| 5797 | if (mddev->major_version != info->major_version || |
| 5798 | mddev->minor_version != info->minor_version || |
| 5799 | /* mddev->patch_version != info->patch_version || */ |
| 5800 | mddev->ctime != info->ctime || |
| 5801 | mddev->level != info->level || |
| 5802 | /* mddev->layout != info->layout || */ |
| 5803 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5804 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5805 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5806 | ((state^info->state) & 0xfffffe00) |
| 5807 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | return -EINVAL; |
| 5809 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5810 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5811 | cnt++; |
| 5812 | if (mddev->raid_disks != info->raid_disks) |
| 5813 | cnt++; |
| 5814 | if (mddev->layout != info->layout) |
| 5815 | cnt++; |
| 5816 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5817 | cnt++; |
| 5818 | if (cnt == 0) |
| 5819 | return 0; |
| 5820 | if (cnt > 1) |
| 5821 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | |
| 5823 | if (mddev->layout != info->layout) { |
| 5824 | /* Change layout |
| 5825 | * we don't need to do anything at the md level, the |
| 5826 | * personality will take care of it all. |
| 5827 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5828 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5830 | else { |
| 5831 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5832 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5833 | if (rv) |
| 5834 | mddev->new_layout = mddev->layout; |
| 5835 | return rv; |
| 5836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5838 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5839 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5841 | if (mddev->raid_disks != info->raid_disks) |
| 5842 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5844 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5845 | if (mddev->pers->quiesce == NULL) |
| 5846 | return -EINVAL; |
| 5847 | if (mddev->recovery || mddev->sync_thread) |
| 5848 | return -EBUSY; |
| 5849 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5850 | /* add the bitmap */ |
| 5851 | if (mddev->bitmap) |
| 5852 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5853 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5854 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5855 | mddev->bitmap_info.offset = |
| 5856 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5857 | mddev->pers->quiesce(mddev, 1); |
| 5858 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5859 | if (!rv) |
| 5860 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5861 | if (rv) |
| 5862 | bitmap_destroy(mddev); |
| 5863 | mddev->pers->quiesce(mddev, 0); |
| 5864 | } else { |
| 5865 | /* remove the bitmap */ |
| 5866 | if (!mddev->bitmap) |
| 5867 | return -ENOENT; |
| 5868 | if (mddev->bitmap->file) |
| 5869 | return -EINVAL; |
| 5870 | mddev->pers->quiesce(mddev, 1); |
| 5871 | bitmap_destroy(mddev); |
| 5872 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5873 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5874 | } |
| 5875 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5876 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | return rv; |
| 5878 | } |
| 5879 | |
| 5880 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5881 | { |
| 5882 | mdk_rdev_t *rdev; |
| 5883 | |
| 5884 | if (mddev->pers == NULL) |
| 5885 | return -ENODEV; |
| 5886 | |
| 5887 | rdev = find_rdev(mddev, dev); |
| 5888 | if (!rdev) |
| 5889 | return -ENODEV; |
| 5890 | |
| 5891 | md_error(mddev, rdev); |
| 5892 | return 0; |
| 5893 | } |
| 5894 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5895 | /* |
| 5896 | * We have a problem here : there is no easy way to give a CHS |
| 5897 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5898 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5899 | * dosfs just mad... ;-) |
| 5900 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5901 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5902 | { |
| 5903 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5904 | |
| 5905 | geo->heads = 2; |
| 5906 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5907 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5908 | return 0; |
| 5909 | } |
| 5910 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5911 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | unsigned int cmd, unsigned long arg) |
| 5913 | { |
| 5914 | int err = 0; |
| 5915 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5917 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | |
| 5919 | if (!capable(CAP_SYS_ADMIN)) |
| 5920 | return -EACCES; |
| 5921 | |
| 5922 | /* |
| 5923 | * Commands dealing with the RAID driver but not any |
| 5924 | * particular array: |
| 5925 | */ |
| 5926 | switch (cmd) |
| 5927 | { |
| 5928 | case RAID_VERSION: |
| 5929 | err = get_version(argp); |
| 5930 | goto done; |
| 5931 | |
| 5932 | case PRINT_RAID_DEBUG: |
| 5933 | err = 0; |
| 5934 | md_print_devices(); |
| 5935 | goto done; |
| 5936 | |
| 5937 | #ifndef MODULE |
| 5938 | case RAID_AUTORUN: |
| 5939 | err = 0; |
| 5940 | autostart_arrays(arg); |
| 5941 | goto done; |
| 5942 | #endif |
| 5943 | default:; |
| 5944 | } |
| 5945 | |
| 5946 | /* |
| 5947 | * Commands creating/starting a new array: |
| 5948 | */ |
| 5949 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5950 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | |
| 5952 | if (!mddev) { |
| 5953 | BUG(); |
| 5954 | goto abort; |
| 5955 | } |
| 5956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | err = mddev_lock(mddev); |
| 5958 | if (err) { |
| 5959 | printk(KERN_INFO |
| 5960 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5961 | err, cmd); |
| 5962 | goto abort; |
| 5963 | } |
| 5964 | |
| 5965 | switch (cmd) |
| 5966 | { |
| 5967 | case SET_ARRAY_INFO: |
| 5968 | { |
| 5969 | mdu_array_info_t info; |
| 5970 | if (!arg) |
| 5971 | memset(&info, 0, sizeof(info)); |
| 5972 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5973 | err = -EFAULT; |
| 5974 | goto abort_unlock; |
| 5975 | } |
| 5976 | if (mddev->pers) { |
| 5977 | err = update_array_info(mddev, &info); |
| 5978 | if (err) { |
| 5979 | printk(KERN_WARNING "md: couldn't update" |
| 5980 | " array info. %d\n", err); |
| 5981 | goto abort_unlock; |
| 5982 | } |
| 5983 | goto done_unlock; |
| 5984 | } |
| 5985 | if (!list_empty(&mddev->disks)) { |
| 5986 | printk(KERN_WARNING |
| 5987 | "md: array %s already has disks!\n", |
| 5988 | mdname(mddev)); |
| 5989 | err = -EBUSY; |
| 5990 | goto abort_unlock; |
| 5991 | } |
| 5992 | if (mddev->raid_disks) { |
| 5993 | printk(KERN_WARNING |
| 5994 | "md: array %s already initialised!\n", |
| 5995 | mdname(mddev)); |
| 5996 | err = -EBUSY; |
| 5997 | goto abort_unlock; |
| 5998 | } |
| 5999 | err = set_array_info(mddev, &info); |
| 6000 | if (err) { |
| 6001 | printk(KERN_WARNING "md: couldn't set" |
| 6002 | " array info. %d\n", err); |
| 6003 | goto abort_unlock; |
| 6004 | } |
| 6005 | } |
| 6006 | goto done_unlock; |
| 6007 | |
| 6008 | default:; |
| 6009 | } |
| 6010 | |
| 6011 | /* |
| 6012 | * Commands querying/configuring an existing array: |
| 6013 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6014 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6015 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6016 | if ((!mddev->raid_disks && !mddev->external) |
| 6017 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6018 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6019 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | err = -ENODEV; |
| 6021 | goto abort_unlock; |
| 6022 | } |
| 6023 | |
| 6024 | /* |
| 6025 | * Commands even a read-only array can execute: |
| 6026 | */ |
| 6027 | switch (cmd) |
| 6028 | { |
| 6029 | case GET_ARRAY_INFO: |
| 6030 | err = get_array_info(mddev, argp); |
| 6031 | goto done_unlock; |
| 6032 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6033 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 6034 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6035 | goto done_unlock; |
| 6036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | case GET_DISK_INFO: |
| 6038 | err = get_disk_info(mddev, argp); |
| 6039 | goto done_unlock; |
| 6040 | |
| 6041 | case RESTART_ARRAY_RW: |
| 6042 | err = restart_array(mddev); |
| 6043 | goto done_unlock; |
| 6044 | |
| 6045 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6046 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | goto done_unlock; |
| 6048 | |
| 6049 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6050 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | goto done_unlock; |
| 6052 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6053 | case BLKROSET: |
| 6054 | if (get_user(ro, (int __user *)(arg))) { |
| 6055 | err = -EFAULT; |
| 6056 | goto done_unlock; |
| 6057 | } |
| 6058 | err = -EINVAL; |
| 6059 | |
| 6060 | /* if the bdev is going readonly the value of mddev->ro |
| 6061 | * does not matter, no writes are coming |
| 6062 | */ |
| 6063 | if (ro) |
| 6064 | goto done_unlock; |
| 6065 | |
| 6066 | /* are we are already prepared for writes? */ |
| 6067 | if (mddev->ro != 1) |
| 6068 | goto done_unlock; |
| 6069 | |
| 6070 | /* transitioning to readauto need only happen for |
| 6071 | * arrays that call md_write_start |
| 6072 | */ |
| 6073 | if (mddev->pers) { |
| 6074 | err = restart_array(mddev); |
| 6075 | if (err == 0) { |
| 6076 | mddev->ro = 2; |
| 6077 | set_disk_ro(mddev->gendisk, 0); |
| 6078 | } |
| 6079 | } |
| 6080 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | /* |
| 6084 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6085 | * superblock, so we do not allow them on read-only arrays. |
| 6086 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6087 | * here and hit the 'default' below, so only disallow |
| 6088 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6090 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6091 | if (mddev->ro == 2) { |
| 6092 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6093 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6094 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6095 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6096 | } else { |
| 6097 | err = -EROFS; |
| 6098 | goto abort_unlock; |
| 6099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | switch (cmd) |
| 6103 | { |
| 6104 | case ADD_NEW_DISK: |
| 6105 | { |
| 6106 | mdu_disk_info_t info; |
| 6107 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6108 | err = -EFAULT; |
| 6109 | else |
| 6110 | err = add_new_disk(mddev, &info); |
| 6111 | goto done_unlock; |
| 6112 | } |
| 6113 | |
| 6114 | case HOT_REMOVE_DISK: |
| 6115 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6116 | goto done_unlock; |
| 6117 | |
| 6118 | case HOT_ADD_DISK: |
| 6119 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6120 | goto done_unlock; |
| 6121 | |
| 6122 | case SET_DISK_FAULTY: |
| 6123 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6124 | goto done_unlock; |
| 6125 | |
| 6126 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6127 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | goto done_unlock; |
| 6129 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6130 | case SET_BITMAP_FILE: |
| 6131 | err = set_bitmap_file(mddev, (int)arg); |
| 6132 | goto done_unlock; |
| 6133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | err = -EINVAL; |
| 6136 | goto abort_unlock; |
| 6137 | } |
| 6138 | |
| 6139 | done_unlock: |
| 6140 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6141 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6142 | err != -EINVAL) |
| 6143 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | mddev_unlock(mddev); |
| 6145 | |
| 6146 | return err; |
| 6147 | done: |
| 6148 | if (err) |
| 6149 | MD_BUG(); |
| 6150 | abort: |
| 6151 | return err; |
| 6152 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6153 | #ifdef CONFIG_COMPAT |
| 6154 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6155 | unsigned int cmd, unsigned long arg) |
| 6156 | { |
| 6157 | switch (cmd) { |
| 6158 | case HOT_REMOVE_DISK: |
| 6159 | case HOT_ADD_DISK: |
| 6160 | case SET_DISK_FAULTY: |
| 6161 | case SET_BITMAP_FILE: |
| 6162 | /* These take in integer arg, do not convert */ |
| 6163 | break; |
| 6164 | default: |
| 6165 | arg = (unsigned long)compat_ptr(arg); |
| 6166 | break; |
| 6167 | } |
| 6168 | |
| 6169 | return md_ioctl(bdev, mode, cmd, arg); |
| 6170 | } |
| 6171 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6173 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | { |
| 6175 | /* |
| 6176 | * Succeed if we can lock the mddev, which confirms that |
| 6177 | * it isn't being stopped right now. |
| 6178 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6179 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | int err; |
| 6181 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6182 | if (mddev->gendisk != bdev->bd_disk) { |
| 6183 | /* we are racing with mddev_put which is discarding this |
| 6184 | * bd_disk. |
| 6185 | */ |
| 6186 | mddev_put(mddev); |
| 6187 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6188 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6189 | /* Then retry the open from the top */ |
| 6190 | return -ERESTARTSYS; |
| 6191 | } |
| 6192 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6193 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6194 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | goto out; |
| 6196 | |
| 6197 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6198 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6199 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6201 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | out: |
| 6203 | return err; |
| 6204 | } |
| 6205 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6206 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6208 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6210 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6211 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | mddev_put(mddev); |
| 6213 | |
| 6214 | return 0; |
| 6215 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6216 | |
| 6217 | static int md_media_changed(struct gendisk *disk) |
| 6218 | { |
| 6219 | mddev_t *mddev = disk->private_data; |
| 6220 | |
| 6221 | return mddev->changed; |
| 6222 | } |
| 6223 | |
| 6224 | static int md_revalidate(struct gendisk *disk) |
| 6225 | { |
| 6226 | mddev_t *mddev = disk->private_data; |
| 6227 | |
| 6228 | mddev->changed = 0; |
| 6229 | return 0; |
| 6230 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6231 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | { |
| 6233 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6234 | .open = md_open, |
| 6235 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6236 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6237 | #ifdef CONFIG_COMPAT |
| 6238 | .compat_ioctl = md_compat_ioctl, |
| 6239 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6240 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6241 | .media_changed = md_media_changed, |
| 6242 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | }; |
| 6244 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6245 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | { |
| 6247 | mdk_thread_t *thread = arg; |
| 6248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | /* |
| 6250 | * md_thread is a 'system-thread', it's priority should be very |
| 6251 | * high. We avoid resource deadlocks individually in each |
| 6252 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6253 | * the very same RT priority as kswapd, thus we will never get |
| 6254 | * into a priority inversion deadlock. |
| 6255 | * |
| 6256 | * we definitely have to have equal or higher priority than |
| 6257 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6258 | * many dirty RAID5 blocks. |
| 6259 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6261 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6262 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6264 | /* We need to wait INTERRUPTIBLE so that |
| 6265 | * we don't add to the load-average. |
| 6266 | * That means we need to be sure no signals are |
| 6267 | * pending |
| 6268 | */ |
| 6269 | if (signal_pending(current)) |
| 6270 | flush_signals(current); |
| 6271 | |
| 6272 | wait_event_interruptible_timeout |
| 6273 | (thread->wqueue, |
| 6274 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6275 | || kthread_should_stop(), |
| 6276 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6278 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6279 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6280 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | return 0; |
| 6284 | } |
| 6285 | |
| 6286 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6287 | { |
| 6288 | if (thread) { |
| 6289 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6290 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6291 | wake_up(&thread->wqueue); |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6296 | const char *name) |
| 6297 | { |
| 6298 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6300 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6301 | if (!thread) |
| 6302 | return NULL; |
| 6303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | init_waitqueue_head(&thread->wqueue); |
| 6305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | thread->run = run; |
| 6307 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6308 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6309 | thread->tsk = kthread_run(md_thread, thread, |
| 6310 | "%s_%s", |
| 6311 | mdname(thread->mddev), |
| 6312 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6313 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | kfree(thread); |
| 6315 | return NULL; |
| 6316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | return thread; |
| 6318 | } |
| 6319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | void md_unregister_thread(mdk_thread_t *thread) |
| 6321 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6322 | if (!thread) |
| 6323 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6324 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6325 | |
| 6326 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | kfree(thread); |
| 6328 | } |
| 6329 | |
| 6330 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6331 | { |
| 6332 | if (!mddev) { |
| 6333 | MD_BUG(); |
| 6334 | return; |
| 6335 | } |
| 6336 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6337 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6339 | |
| 6340 | if (mddev->external) |
| 6341 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6342 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6344 | mdname(mddev), |
| 6345 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6346 | __builtin_return_address(0),__builtin_return_address(1), |
| 6347 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6348 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6349 | if (!mddev->pers) |
| 6350 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | if (!mddev->pers->error_handler) |
| 6352 | return; |
| 6353 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6354 | if (mddev->degraded) |
| 6355 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6356 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6358 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6359 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6360 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6361 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6362 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | /* seq_file implementation /proc/mdstat */ |
| 6366 | |
| 6367 | static void status_unused(struct seq_file *seq) |
| 6368 | { |
| 6369 | int i = 0; |
| 6370 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
| 6372 | seq_printf(seq, "unused devices: "); |
| 6373 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6374 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | char b[BDEVNAME_SIZE]; |
| 6376 | i++; |
| 6377 | seq_printf(seq, "%s ", |
| 6378 | bdevname(rdev->bdev,b)); |
| 6379 | } |
| 6380 | if (!i) |
| 6381 | seq_printf(seq, "<none>"); |
| 6382 | |
| 6383 | seq_printf(seq, "\n"); |
| 6384 | } |
| 6385 | |
| 6386 | |
| 6387 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6388 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6389 | sector_t max_sectors, resync, res; |
| 6390 | unsigned long dt, db; |
| 6391 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6392 | int scale; |
| 6393 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6395 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | |
| 6397 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6398 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6400 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | |
| 6402 | /* |
| 6403 | * Should not happen. |
| 6404 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6405 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | MD_BUG(); |
| 6407 | return; |
| 6408 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6409 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6410 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6411 | * u32, as those are the requirements for sector_div. |
| 6412 | * Thus 'scale' must be at least 10 |
| 6413 | */ |
| 6414 | scale = 10; |
| 6415 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6416 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6417 | scale++; |
| 6418 | } |
| 6419 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6420 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6421 | |
| 6422 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6424 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | seq_printf(seq, "["); |
| 6426 | for (i = 0; i < x; i++) |
| 6427 | seq_printf(seq, "="); |
| 6428 | seq_printf(seq, ">"); |
| 6429 | for (i = 0; i < y; i++) |
| 6430 | seq_printf(seq, "."); |
| 6431 | seq_printf(seq, "] "); |
| 6432 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6433 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6434 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6435 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6436 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6437 | "check" : |
| 6438 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6439 | "resync" : "recovery"))), |
| 6440 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6441 | (unsigned long long) resync/2, |
| 6442 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | |
| 6444 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | * dt: time from mark until now |
| 6446 | * db: blocks written from mark until now |
| 6447 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6448 | * |
| 6449 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6450 | * So we divide before multiply in case it is 32bit and close |
| 6451 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6452 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6453 | * near the end of resync when the number of remaining sectors |
| 6454 | * is close to 'db'. |
| 6455 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6456 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | */ |
| 6458 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6459 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6460 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6461 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6463 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6464 | sector_div(rt, db/32+1); |
| 6465 | rt *= dt; |
| 6466 | rt >>= 5; |
| 6467 | |
| 6468 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6469 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6470 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6471 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6475 | { |
| 6476 | struct list_head *tmp; |
| 6477 | loff_t l = *pos; |
| 6478 | mddev_t *mddev; |
| 6479 | |
| 6480 | if (l >= 0x10000) |
| 6481 | return NULL; |
| 6482 | if (!l--) |
| 6483 | /* header */ |
| 6484 | return (void*)1; |
| 6485 | |
| 6486 | spin_lock(&all_mddevs_lock); |
| 6487 | list_for_each(tmp,&all_mddevs) |
| 6488 | if (!l--) { |
| 6489 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6490 | mddev_get(mddev); |
| 6491 | spin_unlock(&all_mddevs_lock); |
| 6492 | return mddev; |
| 6493 | } |
| 6494 | spin_unlock(&all_mddevs_lock); |
| 6495 | if (!l--) |
| 6496 | return (void*)2;/* tail */ |
| 6497 | return NULL; |
| 6498 | } |
| 6499 | |
| 6500 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6501 | { |
| 6502 | struct list_head *tmp; |
| 6503 | mddev_t *next_mddev, *mddev = v; |
| 6504 | |
| 6505 | ++*pos; |
| 6506 | if (v == (void*)2) |
| 6507 | return NULL; |
| 6508 | |
| 6509 | spin_lock(&all_mddevs_lock); |
| 6510 | if (v == (void*)1) |
| 6511 | tmp = all_mddevs.next; |
| 6512 | else |
| 6513 | tmp = mddev->all_mddevs.next; |
| 6514 | if (tmp != &all_mddevs) |
| 6515 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6516 | else { |
| 6517 | next_mddev = (void*)2; |
| 6518 | *pos = 0x10000; |
| 6519 | } |
| 6520 | spin_unlock(&all_mddevs_lock); |
| 6521 | |
| 6522 | if (v != (void*)1) |
| 6523 | mddev_put(mddev); |
| 6524 | return next_mddev; |
| 6525 | |
| 6526 | } |
| 6527 | |
| 6528 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6529 | { |
| 6530 | mddev_t *mddev = v; |
| 6531 | |
| 6532 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6533 | mddev_put(mddev); |
| 6534 | } |
| 6535 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6536 | struct mdstat_info { |
| 6537 | int event; |
| 6538 | }; |
| 6539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6541 | { |
| 6542 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6543 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6545 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6546 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | |
| 6548 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6549 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | seq_printf(seq, "Personalities : "); |
| 6551 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6552 | list_for_each_entry(pers, &pers_list, list) |
| 6553 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | |
| 6555 | spin_unlock(&pers_lock); |
| 6556 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6557 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | return 0; |
| 6559 | } |
| 6560 | if (v == (void*)2) { |
| 6561 | status_unused(seq); |
| 6562 | return 0; |
| 6563 | } |
| 6564 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6565 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6569 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6570 | mddev->pers ? "" : "in"); |
| 6571 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6572 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6574 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6575 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | seq_printf(seq, " %s", mddev->pers->name); |
| 6577 | } |
| 6578 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6579 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6580 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | char b[BDEVNAME_SIZE]; |
| 6582 | seq_printf(seq, " %s[%d]", |
| 6583 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6584 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6585 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6586 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | seq_printf(seq, "(F)"); |
| 6588 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6589 | } else if (rdev->raid_disk < 0) |
| 6590 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6591 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | if (!list_empty(&mddev->disks)) { |
| 6595 | if (mddev->pers) |
| 6596 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6597 | (unsigned long long) |
| 6598 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6599 | else |
| 6600 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6601 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6603 | if (mddev->persistent) { |
| 6604 | if (mddev->major_version != 0 || |
| 6605 | mddev->minor_version != 90) { |
| 6606 | seq_printf(seq," super %d.%d", |
| 6607 | mddev->major_version, |
| 6608 | mddev->minor_version); |
| 6609 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6610 | } else if (mddev->external) |
| 6611 | seq_printf(seq, " super external:%s", |
| 6612 | mddev->metadata_type); |
| 6613 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6614 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | |
| 6616 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6617 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6619 | if (mddev->pers->sync_request) { |
| 6620 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6621 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6622 | seq_printf(seq, "\n "); |
| 6623 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6624 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6625 | else if (mddev->recovery_cp < MaxSector) |
| 6626 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6627 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6628 | } else |
| 6629 | seq_printf(seq, "\n "); |
| 6630 | |
| 6631 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6632 | unsigned long chunk_kb; |
| 6633 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6634 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6635 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6636 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6637 | "%lu%s chunk", |
| 6638 | bitmap->pages - bitmap->missing_pages, |
| 6639 | bitmap->pages, |
| 6640 | (bitmap->pages - bitmap->missing_pages) |
| 6641 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6642 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6643 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6644 | if (bitmap->file) { |
| 6645 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6646 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6647 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6648 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6649 | seq_printf(seq, "\n"); |
| 6650 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | seq_printf(seq, "\n"); |
| 6654 | } |
| 6655 | mddev_unlock(mddev); |
| 6656 | |
| 6657 | return 0; |
| 6658 | } |
| 6659 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6660 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | .start = md_seq_start, |
| 6662 | .next = md_seq_next, |
| 6663 | .stop = md_seq_stop, |
| 6664 | .show = md_seq_show, |
| 6665 | }; |
| 6666 | |
| 6667 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6668 | { |
| 6669 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6670 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6671 | if (mi == NULL) |
| 6672 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | |
| 6674 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6675 | if (error) |
| 6676 | kfree(mi); |
| 6677 | else { |
| 6678 | struct seq_file *p = file->private_data; |
| 6679 | p->private = mi; |
| 6680 | mi->event = atomic_read(&md_event_count); |
| 6681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | return error; |
| 6683 | } |
| 6684 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6685 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6686 | { |
| 6687 | struct seq_file *m = filp->private_data; |
| 6688 | struct mdstat_info *mi = m->private; |
| 6689 | int mask; |
| 6690 | |
| 6691 | poll_wait(filp, &md_event_waiters, wait); |
| 6692 | |
| 6693 | /* always allow read */ |
| 6694 | mask = POLLIN | POLLRDNORM; |
| 6695 | |
| 6696 | if (mi->event != atomic_read(&md_event_count)) |
| 6697 | mask |= POLLERR | POLLPRI; |
| 6698 | return mask; |
| 6699 | } |
| 6700 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6701 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6702 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | .open = md_seq_open, |
| 6704 | .read = seq_read, |
| 6705 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6706 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6707 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | }; |
| 6709 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6710 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6713 | list_add_tail(&p->list, &pers_list); |
| 6714 | 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] | 6715 | spin_unlock(&pers_lock); |
| 6716 | return 0; |
| 6717 | } |
| 6718 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6719 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6721 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6723 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | spin_unlock(&pers_lock); |
| 6725 | return 0; |
| 6726 | } |
| 6727 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6728 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | { |
| 6730 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6732 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | |
| 6734 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6735 | rcu_read_lock(); |
| 6736 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6738 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6739 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6740 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6741 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6742 | * as sync_io is counted when a request starts, and |
| 6743 | * disk_stats is counted when it completes. |
| 6744 | * So resync activity will cause curr_events to be smaller than |
| 6745 | * when there was no such activity. |
| 6746 | * non-sync IO will cause disk_stat to increase without |
| 6747 | * increasing sync_io so curr_events will (eventually) |
| 6748 | * be larger than it was before. Once it becomes |
| 6749 | * substantially larger, the test below will cause |
| 6750 | * the array to appear non-idle, and resync will slow |
| 6751 | * down. |
| 6752 | * If there is a lot of outstanding resync activity when |
| 6753 | * we set last_event to curr_events, then all that activity |
| 6754 | * completing might cause the array to appear non-idle |
| 6755 | * and resync will be slowed down even though there might |
| 6756 | * not have been non-resync activity. This will only |
| 6757 | * happen once though. 'last_events' will soon reflect |
| 6758 | * the state where there is little or no outstanding |
| 6759 | * resync requests, and further resync activity will |
| 6760 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6761 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6763 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6764 | rdev->last_events = curr_events; |
| 6765 | idle = 0; |
| 6766 | } |
| 6767 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6768 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | return idle; |
| 6770 | } |
| 6771 | |
| 6772 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6773 | { |
| 6774 | /* another "blocks" (512byte) blocks have been synced */ |
| 6775 | atomic_sub(blocks, &mddev->recovery_active); |
| 6776 | wake_up(&mddev->recovery_wait); |
| 6777 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6778 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | md_wakeup_thread(mddev->thread); |
| 6780 | // stop recovery, signal do_sync .... |
| 6781 | } |
| 6782 | } |
| 6783 | |
| 6784 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6785 | /* md_write_start(mddev, bi) |
| 6786 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6787 | * in superblock) before writing, schedule a superblock update |
| 6788 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6789 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6790 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6791 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6792 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6793 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6794 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6795 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6796 | BUG_ON(mddev->ro == 1); |
| 6797 | if (mddev->ro == 2) { |
| 6798 | /* need to switch to read/write */ |
| 6799 | mddev->ro = 0; |
| 6800 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6801 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6802 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6803 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6804 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6805 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6806 | if (mddev->safemode == 1) |
| 6807 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6808 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6809 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6810 | if (mddev->in_sync) { |
| 6811 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6812 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6813 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6814 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6815 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6816 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6817 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6818 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6819 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6820 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6821 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6822 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | void md_write_end(mddev_t *mddev) |
| 6826 | { |
| 6827 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6828 | if (mddev->safemode == 2) |
| 6829 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6830 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6832 | } |
| 6833 | } |
| 6834 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6835 | /* md_allow_write(mddev) |
| 6836 | * Calling this ensures that the array is marked 'active' so that writes |
| 6837 | * may proceed without blocking. It is important to call this before |
| 6838 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6839 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6840 | * |
| 6841 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6842 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6843 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6844 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6845 | { |
| 6846 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6847 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6848 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6849 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6850 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6851 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6852 | |
| 6853 | spin_lock_irq(&mddev->write_lock); |
| 6854 | if (mddev->in_sync) { |
| 6855 | mddev->in_sync = 0; |
| 6856 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6857 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6858 | if (mddev->safemode_delay && |
| 6859 | mddev->safemode == 0) |
| 6860 | mddev->safemode = 1; |
| 6861 | spin_unlock_irq(&mddev->write_lock); |
| 6862 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6863 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6864 | } else |
| 6865 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6866 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6867 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6868 | return -EAGAIN; |
| 6869 | else |
| 6870 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6871 | } |
| 6872 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | #define SYNC_MARKS 10 |
| 6875 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6876 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | { |
| 6878 | mddev_t *mddev2; |
| 6879 | unsigned int currspeed = 0, |
| 6880 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6881 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | unsigned long mark[SYNC_MARKS]; |
| 6883 | sector_t mark_cnt[SYNC_MARKS]; |
| 6884 | int last_mark,m; |
| 6885 | struct list_head *tmp; |
| 6886 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6887 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6888 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6889 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | |
| 6891 | /* just incase thread restarts... */ |
| 6892 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6893 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6894 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6895 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6897 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6898 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6899 | desc = "data-check"; |
| 6900 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6901 | desc = "requested-resync"; |
| 6902 | else |
| 6903 | desc = "resync"; |
| 6904 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6905 | desc = "reshape"; |
| 6906 | else |
| 6907 | desc = "recovery"; |
| 6908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | /* we overload curr_resync somewhat here. |
| 6910 | * 0 == not engaged in resync at all |
| 6911 | * 2 == checking that there is no conflict with another sync |
| 6912 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6913 | * commense |
| 6914 | * other == active in resync - this many blocks |
| 6915 | * |
| 6916 | * Before starting a resync we must have set curr_resync to |
| 6917 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6918 | * less than ours. When we find one that is the same or higher |
| 6919 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6920 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6921 | * This will mean we have to start checking from the beginning again. |
| 6922 | * |
| 6923 | */ |
| 6924 | |
| 6925 | do { |
| 6926 | mddev->curr_resync = 2; |
| 6927 | |
| 6928 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6929 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6930 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6931 | |
| 6932 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6934 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | if (mddev2 == mddev) |
| 6936 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6937 | if (!mddev->parallel_resync |
| 6938 | && mddev2->curr_resync |
| 6939 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | DEFINE_WAIT(wq); |
| 6941 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6942 | /* arbitrarily yield */ |
| 6943 | mddev->curr_resync = 1; |
| 6944 | wake_up(&resync_wait); |
| 6945 | } |
| 6946 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6947 | /* no need to wait here, we can wait the next |
| 6948 | * time 'round when curr_resync == 2 |
| 6949 | */ |
| 6950 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6951 | /* We need to wait 'interruptible' so as not to |
| 6952 | * contribute to the load average, and not to |
| 6953 | * be caught by 'softlockup' |
| 6954 | */ |
| 6955 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6956 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6957 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6958 | printk(KERN_INFO "md: delaying %s of %s" |
| 6959 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6961 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6962 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6963 | if (signal_pending(current)) |
| 6964 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | schedule(); |
| 6966 | finish_wait(&resync_wait, &wq); |
| 6967 | goto try_again; |
| 6968 | } |
| 6969 | finish_wait(&resync_wait, &wq); |
| 6970 | } |
| 6971 | } |
| 6972 | } while (mddev->curr_resync < 2); |
| 6973 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6974 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6975 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6977 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | */ |
| 6979 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6980 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6981 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6982 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6983 | j = mddev->resync_min; |
| 6984 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6985 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6986 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6987 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6988 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6989 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6991 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6992 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6993 | rcu_read_lock(); |
| 6994 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6995 | if (rdev->raid_disk >= 0 && |
| 6996 | !test_bit(Faulty, &rdev->flags) && |
| 6997 | !test_bit(In_sync, &rdev->flags) && |
| 6998 | rdev->recovery_offset < j) |
| 6999 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7000 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7003 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7004 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7005 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7006 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7007 | "(but not more than %d KB/sec) for %s.\n", |
| 7008 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7010 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7011 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7012 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7014 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7015 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | } |
| 7017 | last_mark = 0; |
| 7018 | mddev->resync_mark = mark[last_mark]; |
| 7019 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7020 | |
| 7021 | /* |
| 7022 | * Tune reconstruction: |
| 7023 | */ |
| 7024 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7025 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7026 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7027 | |
| 7028 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | last_check = 0; |
| 7030 | |
| 7031 | if (j>2) { |
| 7032 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7033 | "md: resuming %s of %s from checkpoint.\n", |
| 7034 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | mddev->curr_resync = j; |
| 7036 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7037 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | |
| 7039 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7040 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7042 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7043 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7044 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7045 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7046 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7047 | > (max_sectors >> 4)) || |
| 7048 | (j - mddev->curr_resync_completed)*2 |
| 7049 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7050 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7051 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7052 | wait_event(mddev->recovery_wait, |
| 7053 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7054 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7055 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7056 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7057 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7058 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7059 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 7060 | /* As this condition is controlled by user-space, |
| 7061 | * we can block indefinitely, so use '_interruptible' |
| 7062 | * to avoid triggering warnings. |
| 7063 | */ |
| 7064 | flush_signals(current); /* just in case */ |
| 7065 | wait_event_interruptible(mddev->recovery_wait, |
| 7066 | mddev->resync_max > j |
| 7067 | || kthread_should_stop()); |
| 7068 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7069 | |
| 7070 | if (kthread_should_stop()) |
| 7071 | goto interrupted; |
| 7072 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7073 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7074 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7075 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7076 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | goto out; |
| 7078 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7079 | |
| 7080 | if (!skipped) { /* actual IO requested */ |
| 7081 | io_sectors += sectors; |
| 7082 | atomic_add(sectors, &mddev->recovery_active); |
| 7083 | } |
| 7084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | j += sectors; |
| 7086 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7087 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7088 | if (last_check == 0) |
| 7089 | /* this is the earliers that rebuilt will be |
| 7090 | * visible in /proc/mdstat |
| 7091 | */ |
| 7092 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7093 | |
| 7094 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | continue; |
| 7096 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7097 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7098 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7099 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | break; |
| 7101 | |
| 7102 | repeat: |
| 7103 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7104 | /* step marks */ |
| 7105 | int next = (last_mark+1) % SYNC_MARKS; |
| 7106 | |
| 7107 | mddev->resync_mark = mark[next]; |
| 7108 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7109 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7110 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | last_mark = next; |
| 7112 | } |
| 7113 | |
| 7114 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7115 | if (kthread_should_stop()) |
| 7116 | goto interrupted; |
| 7117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | |
| 7119 | /* |
| 7120 | * this loop exits only if either when we are slower than |
| 7121 | * the 'hard' speed limit, or the system was IO-idle for |
| 7122 | * a jiffy. |
| 7123 | * the system might be non-idle CPU-wise, but we only care |
| 7124 | * about not overloading the IO subsystem. (things like an |
| 7125 | * e2fsck being done on the RAID array should execute fast) |
| 7126 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | cond_resched(); |
| 7128 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7129 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7130 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7132 | if (currspeed > speed_min(mddev)) { |
| 7133 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7134 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7135 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | goto repeat; |
| 7137 | } |
| 7138 | } |
| 7139 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7140 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | /* |
| 7142 | * this also signals 'finished resyncing' to md_stop |
| 7143 | */ |
| 7144 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7146 | |
| 7147 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7148 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7150 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7151 | mddev->curr_resync > 2) { |
| 7152 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7153 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7154 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7155 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7156 | "md: checkpointing %s of %s.\n", |
| 7157 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7158 | mddev->recovery_cp = mddev->curr_resync; |
| 7159 | } |
| 7160 | } else |
| 7161 | mddev->recovery_cp = MaxSector; |
| 7162 | } else { |
| 7163 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7164 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7165 | rcu_read_lock(); |
| 7166 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7167 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7168 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7169 | !test_bit(Faulty, &rdev->flags) && |
| 7170 | !test_bit(In_sync, &rdev->flags) && |
| 7171 | rdev->recovery_offset < mddev->curr_resync) |
| 7172 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7173 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7176 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7178 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7179 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7180 | /* We completed so min/max setting can be forgotten if used. */ |
| 7181 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7182 | mddev->resync_min = 0; |
| 7183 | mddev->resync_max = MaxSector; |
| 7184 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7185 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | mddev->curr_resync = 0; |
| 7187 | wake_up(&resync_wait); |
| 7188 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7189 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7190 | return; |
| 7191 | |
| 7192 | interrupted: |
| 7193 | /* |
| 7194 | * got a signal, exit. |
| 7195 | */ |
| 7196 | printk(KERN_INFO |
| 7197 | "md: md_do_sync() got signal ... exiting\n"); |
| 7198 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7199 | goto out; |
| 7200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7202 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7204 | static int remove_and_add_spares(mddev_t *mddev) |
| 7205 | { |
| 7206 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7207 | int spares = 0; |
| 7208 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7209 | mddev->curr_resync_completed = 0; |
| 7210 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7211 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7212 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7213 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7214 | (test_bit(Faulty, &rdev->flags) || |
| 7215 | ! test_bit(In_sync, &rdev->flags)) && |
| 7216 | atomic_read(&rdev->nr_pending)==0) { |
| 7217 | if (mddev->pers->hot_remove_disk( |
| 7218 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7219 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7220 | rdev->raid_disk = -1; |
| 7221 | } |
| 7222 | } |
| 7223 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7224 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7225 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7226 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7227 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | 4274215 | 2011-06-28 16:59:42 +1000 | [diff] [blame] | 7228 | !test_bit(Faulty, &rdev->flags) && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7229 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7230 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7231 | if (rdev->raid_disk < 0 |
| 7232 | && !test_bit(Faulty, &rdev->flags)) { |
| 7233 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7234 | if (mddev->pers-> |
| 7235 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7236 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7237 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7238 | spares++; |
| 7239 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7240 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7241 | } else |
| 7242 | break; |
| 7243 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7244 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7245 | } |
| 7246 | return spares; |
| 7247 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7248 | |
| 7249 | static void reap_sync_thread(mddev_t *mddev) |
| 7250 | { |
| 7251 | mdk_rdev_t *rdev; |
| 7252 | |
| 7253 | /* resync has finished, collect result */ |
| 7254 | md_unregister_thread(mddev->sync_thread); |
| 7255 | mddev->sync_thread = NULL; |
| 7256 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7257 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7258 | /* success...*/ |
| 7259 | /* activate any spares */ |
| 7260 | if (mddev->pers->spare_active(mddev)) |
| 7261 | sysfs_notify(&mddev->kobj, NULL, |
| 7262 | "degraded"); |
| 7263 | } |
| 7264 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7265 | mddev->pers->finish_reshape) |
| 7266 | mddev->pers->finish_reshape(mddev); |
| 7267 | md_update_sb(mddev, 1); |
| 7268 | |
| 7269 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7270 | * information must be scrapped |
| 7271 | */ |
| 7272 | if (!mddev->degraded) |
| 7273 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7274 | rdev->saved_raid_disk = -1; |
| 7275 | |
| 7276 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7277 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7278 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7279 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7280 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7281 | /* flag recovery needed just to double check */ |
| 7282 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7283 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7284 | md_new_event(mddev); |
Jonathan Brassow | 768e587 | 2011-07-27 11:00:37 +1000 | [diff] [blame] | 7285 | if (mddev->event_work.func) |
| 7286 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7287 | } |
| 7288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | /* |
| 7290 | * This routine is regularly called by all per-raid-array threads to |
| 7291 | * deal with generic issues like resync and super-block update. |
| 7292 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7293 | * need this as they never do any recovery or update the superblock. |
| 7294 | * |
| 7295 | * It does not do any resync itself, but rather "forks" off other threads |
| 7296 | * to do that as needed. |
| 7297 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7298 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7299 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | * and wakeups up this thread which will reap the thread and finish up. |
| 7301 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7302 | * |
| 7303 | * The overall approach is: |
| 7304 | * 1/ if the superblock needs updating, update it. |
| 7305 | * 2/ If a recovery thread is running, don't do anything else. |
| 7306 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7307 | * 4/ If there are any faulty devices, remove them. |
| 7308 | * 5/ If array is degraded, try to add spares devices |
| 7309 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7310 | */ |
| 7311 | void md_check_recovery(mddev_t *mddev) |
| 7312 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7313 | if (mddev->suspended) |
| 7314 | return; |
| 7315 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7316 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7317 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7319 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7320 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7321 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7322 | mdname(mddev)); |
| 7323 | mddev->safemode = 2; |
| 7324 | } |
| 7325 | flush_signals(current); |
| 7326 | } |
| 7327 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7328 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7329 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7330 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7331 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7333 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7334 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7335 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7336 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | )) |
| 7338 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7339 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7340 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7341 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7342 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7343 | if (mddev->ro) { |
| 7344 | /* Only thing we do on a ro array is remove |
| 7345 | * failed devices. |
| 7346 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7347 | mdk_rdev_t *rdev; |
| 7348 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7349 | if (rdev->raid_disk >= 0 && |
| 7350 | !test_bit(Blocked, &rdev->flags) && |
| 7351 | test_bit(Faulty, &rdev->flags) && |
| 7352 | atomic_read(&rdev->nr_pending)==0) { |
| 7353 | if (mddev->pers->hot_remove_disk( |
| 7354 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7355 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7356 | rdev->raid_disk = -1; |
| 7357 | } |
| 7358 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7359 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7360 | goto unlock; |
| 7361 | } |
| 7362 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7363 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7364 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7365 | spin_lock_irq(&mddev->write_lock); |
| 7366 | if (mddev->safemode && |
| 7367 | !atomic_read(&mddev->writes_pending) && |
| 7368 | !mddev->in_sync && |
| 7369 | mddev->recovery_cp == MaxSector) { |
| 7370 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7371 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7372 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7373 | } |
| 7374 | if (mddev->safemode == 1) |
| 7375 | mddev->safemode = 0; |
| 7376 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7377 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7378 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7379 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7380 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7381 | if (mddev->flags) |
| 7382 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7385 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7386 | /* resync/recovery still happening */ |
| 7387 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7388 | goto unlock; |
| 7389 | } |
| 7390 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7391 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | goto unlock; |
| 7393 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7394 | /* Set RUNNING before clearing NEEDED to avoid |
| 7395 | * any transients in the value of "sync_action". |
| 7396 | */ |
| 7397 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7398 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7399 | /* Clear some bits that don't mean anything, but |
| 7400 | * might be left set |
| 7401 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7402 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7403 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7404 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7405 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7406 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | /* no recovery is running. |
| 7408 | * remove any failed drives, then |
| 7409 | * add spares if possible. |
| 7410 | * Spare are also removed and re-added, to allow |
| 7411 | * the personality to fail the re-add. |
| 7412 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7414 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7415 | if (mddev->pers->check_reshape == NULL || |
| 7416 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7417 | /* Cannot proceed */ |
| 7418 | goto unlock; |
| 7419 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7420 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7421 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7422 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7423 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7424 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7425 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7426 | } else if (mddev->recovery_cp < MaxSector) { |
| 7427 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7428 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7429 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7430 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7434 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7435 | /* We are adding a device or devices to an array |
| 7436 | * which has the bitmap stored on all devices. |
| 7437 | * So make sure all bitmap pages get written |
| 7438 | */ |
| 7439 | bitmap_write_all(mddev->bitmap); |
| 7440 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7442 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7443 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | if (!mddev->sync_thread) { |
| 7445 | printk(KERN_ERR "%s: could not start resync" |
| 7446 | " thread...\n", |
| 7447 | mdname(mddev)); |
| 7448 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7449 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7450 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7451 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7452 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7453 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7454 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7456 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7457 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | } |
| 7459 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7460 | if (!mddev->sync_thread) { |
| 7461 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7462 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7463 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7464 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7465 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | mddev_unlock(mddev); |
| 7468 | } |
| 7469 | } |
| 7470 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7471 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7472 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7473 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7474 | wait_event_timeout(rdev->blocked_wait, |
| 7475 | !test_bit(Blocked, &rdev->flags), |
| 7476 | msecs_to_jiffies(5000)); |
| 7477 | rdev_dec_pending(rdev, mddev); |
| 7478 | } |
| 7479 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7480 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7481 | |
| 7482 | /* Bad block management. |
| 7483 | * We can record which blocks on each device are 'bad' and so just |
| 7484 | * fail those blocks, or that stripe, rather than the whole device. |
| 7485 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7486 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7487 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7488 | * A 'shift' can be set so that larger blocks are tracked and |
| 7489 | * consequently larger devices can be covered. |
| 7490 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7491 | * |
| 7492 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7493 | * might need to retry if it is very unlucky. |
| 7494 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7495 | * so we use the write_seqlock_irq variant. |
| 7496 | * |
| 7497 | * When looking for a bad block we specify a range and want to |
| 7498 | * know if any block in the range is bad. So we binary-search |
| 7499 | * to the last range that starts at-or-before the given endpoint, |
| 7500 | * (or "before the sector after the target range") |
| 7501 | * then see if it ends after the given start. |
| 7502 | * We return |
| 7503 | * 0 if there are no known bad blocks in the range |
| 7504 | * 1 if there are known bad block which are all acknowledged |
| 7505 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7506 | * plus the start/length of the first bad section we overlap. |
| 7507 | */ |
| 7508 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7509 | sector_t *first_bad, int *bad_sectors) |
| 7510 | { |
| 7511 | int hi; |
| 7512 | int lo = 0; |
| 7513 | u64 *p = bb->page; |
| 7514 | int rv = 0; |
| 7515 | sector_t target = s + sectors; |
| 7516 | unsigned seq; |
| 7517 | |
| 7518 | if (bb->shift > 0) { |
| 7519 | /* round the start down, and the end up */ |
| 7520 | s >>= bb->shift; |
| 7521 | target += (1<<bb->shift) - 1; |
| 7522 | target >>= bb->shift; |
| 7523 | sectors = target - s; |
| 7524 | } |
| 7525 | /* 'target' is now the first block after the bad range */ |
| 7526 | |
| 7527 | retry: |
| 7528 | seq = read_seqbegin(&bb->lock); |
| 7529 | |
| 7530 | hi = bb->count; |
| 7531 | |
| 7532 | /* Binary search between lo and hi for 'target' |
| 7533 | * i.e. for the last range that starts before 'target' |
| 7534 | */ |
| 7535 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7536 | * are known not to be the last range before target. |
| 7537 | * VARIANT: hi-lo is the number of possible |
| 7538 | * ranges, and decreases until it reaches 1 |
| 7539 | */ |
| 7540 | while (hi - lo > 1) { |
| 7541 | int mid = (lo + hi) / 2; |
| 7542 | sector_t a = BB_OFFSET(p[mid]); |
| 7543 | if (a < target) |
| 7544 | /* This could still be the one, earlier ranges |
| 7545 | * could not. */ |
| 7546 | lo = mid; |
| 7547 | else |
| 7548 | /* This and later ranges are definitely out. */ |
| 7549 | hi = mid; |
| 7550 | } |
| 7551 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7552 | if (hi > lo) { |
| 7553 | /* need to check all range that end after 's' to see if |
| 7554 | * any are unacknowledged. |
| 7555 | */ |
| 7556 | while (lo >= 0 && |
| 7557 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7558 | if (BB_OFFSET(p[lo]) < target) { |
| 7559 | /* starts before the end, and finishes after |
| 7560 | * the start, so they must overlap |
| 7561 | */ |
| 7562 | if (rv != -1 && BB_ACK(p[lo])) |
| 7563 | rv = 1; |
| 7564 | else |
| 7565 | rv = -1; |
| 7566 | *first_bad = BB_OFFSET(p[lo]); |
| 7567 | *bad_sectors = BB_LEN(p[lo]); |
| 7568 | } |
| 7569 | lo--; |
| 7570 | } |
| 7571 | } |
| 7572 | |
| 7573 | if (read_seqretry(&bb->lock, seq)) |
| 7574 | goto retry; |
| 7575 | |
| 7576 | return rv; |
| 7577 | } |
| 7578 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7579 | |
| 7580 | /* |
| 7581 | * Add a range of bad blocks to the table. |
| 7582 | * This might extend the table, or might contract it |
| 7583 | * if two adjacent ranges can be merged. |
| 7584 | * We binary-search to find the 'insertion' point, then |
| 7585 | * decide how best to handle it. |
| 7586 | */ |
| 7587 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7588 | int acknowledged) |
| 7589 | { |
| 7590 | u64 *p; |
| 7591 | int lo, hi; |
| 7592 | int rv = 1; |
| 7593 | |
| 7594 | if (bb->shift < 0) |
| 7595 | /* badblocks are disabled */ |
| 7596 | return 0; |
| 7597 | |
| 7598 | if (bb->shift) { |
| 7599 | /* round the start down, and the end up */ |
| 7600 | sector_t next = s + sectors; |
| 7601 | s >>= bb->shift; |
| 7602 | next += (1<<bb->shift) - 1; |
| 7603 | next >>= bb->shift; |
| 7604 | sectors = next - s; |
| 7605 | } |
| 7606 | |
| 7607 | write_seqlock_irq(&bb->lock); |
| 7608 | |
| 7609 | p = bb->page; |
| 7610 | lo = 0; |
| 7611 | hi = bb->count; |
| 7612 | /* Find the last range that starts at-or-before 's' */ |
| 7613 | while (hi - lo > 1) { |
| 7614 | int mid = (lo + hi) / 2; |
| 7615 | sector_t a = BB_OFFSET(p[mid]); |
| 7616 | if (a <= s) |
| 7617 | lo = mid; |
| 7618 | else |
| 7619 | hi = mid; |
| 7620 | } |
| 7621 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7622 | hi = lo; |
| 7623 | |
| 7624 | if (hi > lo) { |
| 7625 | /* we found a range that might merge with the start |
| 7626 | * of our new range |
| 7627 | */ |
| 7628 | sector_t a = BB_OFFSET(p[lo]); |
| 7629 | sector_t e = a + BB_LEN(p[lo]); |
| 7630 | int ack = BB_ACK(p[lo]); |
| 7631 | if (e >= s) { |
| 7632 | /* Yes, we can merge with a previous range */ |
| 7633 | if (s == a && s + sectors >= e) |
| 7634 | /* new range covers old */ |
| 7635 | ack = acknowledged; |
| 7636 | else |
| 7637 | ack = ack && acknowledged; |
| 7638 | |
| 7639 | if (e < s + sectors) |
| 7640 | e = s + sectors; |
| 7641 | if (e - a <= BB_MAX_LEN) { |
| 7642 | p[lo] = BB_MAKE(a, e-a, ack); |
| 7643 | s = e; |
| 7644 | } else { |
| 7645 | /* does not all fit in one range, |
| 7646 | * make p[lo] maximal |
| 7647 | */ |
| 7648 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 7649 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7650 | s = a + BB_MAX_LEN; |
| 7651 | } |
| 7652 | sectors = e - s; |
| 7653 | } |
| 7654 | } |
| 7655 | if (sectors && hi < bb->count) { |
| 7656 | /* 'hi' points to the first range that starts after 's'. |
| 7657 | * Maybe we can merge with the start of that range */ |
| 7658 | sector_t a = BB_OFFSET(p[hi]); |
| 7659 | sector_t e = a + BB_LEN(p[hi]); |
| 7660 | int ack = BB_ACK(p[hi]); |
| 7661 | if (a <= s + sectors) { |
| 7662 | /* merging is possible */ |
| 7663 | if (e <= s + sectors) { |
| 7664 | /* full overlap */ |
| 7665 | e = s + sectors; |
| 7666 | ack = acknowledged; |
| 7667 | } else |
| 7668 | ack = ack && acknowledged; |
| 7669 | |
| 7670 | a = s; |
| 7671 | if (e - a <= BB_MAX_LEN) { |
| 7672 | p[hi] = BB_MAKE(a, e-a, ack); |
| 7673 | s = e; |
| 7674 | } else { |
| 7675 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 7676 | s = a + BB_MAX_LEN; |
| 7677 | } |
| 7678 | sectors = e - s; |
| 7679 | lo = hi; |
| 7680 | hi++; |
| 7681 | } |
| 7682 | } |
| 7683 | if (sectors == 0 && hi < bb->count) { |
| 7684 | /* we might be able to combine lo and hi */ |
| 7685 | /* Note: 's' is at the end of 'lo' */ |
| 7686 | sector_t a = BB_OFFSET(p[hi]); |
| 7687 | int lolen = BB_LEN(p[lo]); |
| 7688 | int hilen = BB_LEN(p[hi]); |
| 7689 | int newlen = lolen + hilen - (s - a); |
| 7690 | if (s >= a && newlen < BB_MAX_LEN) { |
| 7691 | /* yes, we can combine them */ |
| 7692 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 7693 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 7694 | memmove(p + hi, p + hi + 1, |
| 7695 | (bb->count - hi - 1) * 8); |
| 7696 | bb->count--; |
| 7697 | } |
| 7698 | } |
| 7699 | while (sectors) { |
| 7700 | /* didn't merge (it all). |
| 7701 | * Need to add a range just before 'hi' */ |
| 7702 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7703 | /* No room for more */ |
| 7704 | rv = 0; |
| 7705 | break; |
| 7706 | } else { |
| 7707 | int this_sectors = sectors; |
| 7708 | memmove(p + hi + 1, p + hi, |
| 7709 | (bb->count - hi) * 8); |
| 7710 | bb->count++; |
| 7711 | |
| 7712 | if (this_sectors > BB_MAX_LEN) |
| 7713 | this_sectors = BB_MAX_LEN; |
| 7714 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 7715 | sectors -= this_sectors; |
| 7716 | s += this_sectors; |
| 7717 | } |
| 7718 | } |
| 7719 | |
| 7720 | bb->changed = 1; |
| 7721 | write_sequnlock_irq(&bb->lock); |
| 7722 | |
| 7723 | return rv; |
| 7724 | } |
| 7725 | |
| 7726 | int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors, |
| 7727 | int acknowledged) |
| 7728 | { |
| 7729 | int rv = md_set_badblocks(&rdev->badblocks, |
| 7730 | s + rdev->data_offset, sectors, acknowledged); |
| 7731 | if (rv) { |
| 7732 | /* Make sure they get written out promptly */ |
| 7733 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 7734 | md_wakeup_thread(rdev->mddev->thread); |
| 7735 | } |
| 7736 | return rv; |
| 7737 | } |
| 7738 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 7739 | |
| 7740 | /* |
| 7741 | * Remove a range of bad blocks from the table. |
| 7742 | * This may involve extending the table if we spilt a region, |
| 7743 | * but it must not fail. So if the table becomes full, we just |
| 7744 | * drop the remove request. |
| 7745 | */ |
| 7746 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 7747 | { |
| 7748 | u64 *p; |
| 7749 | int lo, hi; |
| 7750 | sector_t target = s + sectors; |
| 7751 | int rv = 0; |
| 7752 | |
| 7753 | if (bb->shift > 0) { |
| 7754 | /* When clearing we round the start up and the end down. |
| 7755 | * This should not matter as the shift should align with |
| 7756 | * the block size and no rounding should ever be needed. |
| 7757 | * However it is better the think a block is bad when it |
| 7758 | * isn't than to think a block is not bad when it is. |
| 7759 | */ |
| 7760 | s += (1<<bb->shift) - 1; |
| 7761 | s >>= bb->shift; |
| 7762 | target >>= bb->shift; |
| 7763 | sectors = target - s; |
| 7764 | } |
| 7765 | |
| 7766 | write_seqlock_irq(&bb->lock); |
| 7767 | |
| 7768 | p = bb->page; |
| 7769 | lo = 0; |
| 7770 | hi = bb->count; |
| 7771 | /* Find the last range that starts before 'target' */ |
| 7772 | while (hi - lo > 1) { |
| 7773 | int mid = (lo + hi) / 2; |
| 7774 | sector_t a = BB_OFFSET(p[mid]); |
| 7775 | if (a < target) |
| 7776 | lo = mid; |
| 7777 | else |
| 7778 | hi = mid; |
| 7779 | } |
| 7780 | if (hi > lo) { |
| 7781 | /* p[lo] is the last range that could overlap the |
| 7782 | * current range. Earlier ranges could also overlap, |
| 7783 | * but only this one can overlap the end of the range. |
| 7784 | */ |
| 7785 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 7786 | /* Partial overlap, leave the tail of this range */ |
| 7787 | int ack = BB_ACK(p[lo]); |
| 7788 | sector_t a = BB_OFFSET(p[lo]); |
| 7789 | sector_t end = a + BB_LEN(p[lo]); |
| 7790 | |
| 7791 | if (a < s) { |
| 7792 | /* we need to split this range */ |
| 7793 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 7794 | rv = 0; |
| 7795 | goto out; |
| 7796 | } |
| 7797 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 7798 | bb->count++; |
| 7799 | p[lo] = BB_MAKE(a, s-a, ack); |
| 7800 | lo++; |
| 7801 | } |
| 7802 | p[lo] = BB_MAKE(target, end - target, ack); |
| 7803 | /* there is no longer an overlap */ |
| 7804 | hi = lo; |
| 7805 | lo--; |
| 7806 | } |
| 7807 | while (lo >= 0 && |
| 7808 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7809 | /* This range does overlap */ |
| 7810 | if (BB_OFFSET(p[lo]) < s) { |
| 7811 | /* Keep the early parts of this range. */ |
| 7812 | int ack = BB_ACK(p[lo]); |
| 7813 | sector_t start = BB_OFFSET(p[lo]); |
| 7814 | p[lo] = BB_MAKE(start, s - start, ack); |
| 7815 | /* now low doesn't overlap, so.. */ |
| 7816 | break; |
| 7817 | } |
| 7818 | lo--; |
| 7819 | } |
| 7820 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 7821 | * anything between needs to be discarded |
| 7822 | */ |
| 7823 | if (hi - lo > 1) { |
| 7824 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 7825 | bb->count -= (hi - lo - 1); |
| 7826 | } |
| 7827 | } |
| 7828 | |
| 7829 | bb->changed = 1; |
| 7830 | out: |
| 7831 | write_sequnlock_irq(&bb->lock); |
| 7832 | return rv; |
| 7833 | } |
| 7834 | |
| 7835 | int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors) |
| 7836 | { |
| 7837 | return md_clear_badblocks(&rdev->badblocks, |
| 7838 | s + rdev->data_offset, |
| 7839 | sectors); |
| 7840 | } |
| 7841 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 7842 | |
| 7843 | /* |
| 7844 | * Acknowledge all bad blocks in a list. |
| 7845 | * This only succeeds if ->changed is clear. It is used by |
| 7846 | * in-kernel metadata updates |
| 7847 | */ |
| 7848 | void md_ack_all_badblocks(struct badblocks *bb) |
| 7849 | { |
| 7850 | if (bb->page == NULL || bb->changed) |
| 7851 | /* no point even trying */ |
| 7852 | return; |
| 7853 | write_seqlock_irq(&bb->lock); |
| 7854 | |
| 7855 | if (bb->changed == 0) { |
| 7856 | u64 *p = bb->page; |
| 7857 | int i; |
| 7858 | for (i = 0; i < bb->count ; i++) { |
| 7859 | if (!BB_ACK(p[i])) { |
| 7860 | sector_t start = BB_OFFSET(p[i]); |
| 7861 | int len = BB_LEN(p[i]); |
| 7862 | p[i] = BB_MAKE(start, len, 1); |
| 7863 | } |
| 7864 | } |
| 7865 | } |
| 7866 | write_sequnlock_irq(&bb->lock); |
| 7867 | } |
| 7868 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 7869 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 7870 | /* sysfs access to bad-blocks list. |
| 7871 | * We present two files. |
| 7872 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 7873 | * are recorded as bad. The list is truncated to fit within |
| 7874 | * the one-page limit of sysfs. |
| 7875 | * Writing "sector length" to this file adds an acknowledged |
| 7876 | * bad block list. |
| 7877 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 7878 | * been acknowledged. Writing to this file adds bad blocks |
| 7879 | * without acknowledging them. This is largely for testing. |
| 7880 | */ |
| 7881 | |
| 7882 | static ssize_t |
| 7883 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 7884 | { |
| 7885 | size_t len; |
| 7886 | int i; |
| 7887 | u64 *p = bb->page; |
| 7888 | unsigned seq; |
| 7889 | |
| 7890 | if (bb->shift < 0) |
| 7891 | return 0; |
| 7892 | |
| 7893 | retry: |
| 7894 | seq = read_seqbegin(&bb->lock); |
| 7895 | |
| 7896 | len = 0; |
| 7897 | i = 0; |
| 7898 | |
| 7899 | while (len < PAGE_SIZE && i < bb->count) { |
| 7900 | sector_t s = BB_OFFSET(p[i]); |
| 7901 | unsigned int length = BB_LEN(p[i]); |
| 7902 | int ack = BB_ACK(p[i]); |
| 7903 | i++; |
| 7904 | |
| 7905 | if (unack && ack) |
| 7906 | continue; |
| 7907 | |
| 7908 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 7909 | (unsigned long long)s << bb->shift, |
| 7910 | length << bb->shift); |
| 7911 | } |
| 7912 | |
| 7913 | if (read_seqretry(&bb->lock, seq)) |
| 7914 | goto retry; |
| 7915 | |
| 7916 | return len; |
| 7917 | } |
| 7918 | |
| 7919 | #define DO_DEBUG 1 |
| 7920 | |
| 7921 | static ssize_t |
| 7922 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 7923 | { |
| 7924 | unsigned long long sector; |
| 7925 | int length; |
| 7926 | char newline; |
| 7927 | #ifdef DO_DEBUG |
| 7928 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 7929 | * Normally only a successful write may clear a badblock |
| 7930 | */ |
| 7931 | int clear = 0; |
| 7932 | if (page[0] == '-') { |
| 7933 | clear = 1; |
| 7934 | page++; |
| 7935 | } |
| 7936 | #endif /* DO_DEBUG */ |
| 7937 | |
| 7938 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 7939 | case 3: |
| 7940 | if (newline != '\n') |
| 7941 | return -EINVAL; |
| 7942 | case 2: |
| 7943 | if (length <= 0) |
| 7944 | return -EINVAL; |
| 7945 | break; |
| 7946 | default: |
| 7947 | return -EINVAL; |
| 7948 | } |
| 7949 | |
| 7950 | #ifdef DO_DEBUG |
| 7951 | if (clear) { |
| 7952 | md_clear_badblocks(bb, sector, length); |
| 7953 | return len; |
| 7954 | } |
| 7955 | #endif /* DO_DEBUG */ |
| 7956 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 7957 | return len; |
| 7958 | else |
| 7959 | return -ENOSPC; |
| 7960 | } |
| 7961 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7962 | static int md_notify_reboot(struct notifier_block *this, |
| 7963 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7964 | { |
| 7965 | struct list_head *tmp; |
| 7966 | mddev_t *mddev; |
| 7967 | |
| 7968 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7969 | |
| 7970 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7971 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7972 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7973 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7974 | /* Force a switch to readonly even array |
| 7975 | * appears to still be in use. Hence |
| 7976 | * the '100'. |
| 7977 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7978 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7979 | mddev_unlock(mddev); |
| 7980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7981 | /* |
| 7982 | * certain more exotic SCSI devices are known to be |
| 7983 | * volatile wrt too early system reboots. While the |
| 7984 | * right place to handle this issue is the given |
| 7985 | * driver, we do want to have a safe RAID driver ... |
| 7986 | */ |
| 7987 | mdelay(1000*1); |
| 7988 | } |
| 7989 | return NOTIFY_DONE; |
| 7990 | } |
| 7991 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7992 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | .notifier_call = md_notify_reboot, |
| 7994 | .next = NULL, |
| 7995 | .priority = INT_MAX, /* before any real devices */ |
| 7996 | }; |
| 7997 | |
| 7998 | static void md_geninit(void) |
| 7999 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8000 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 8001 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8002 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | } |
| 8004 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8005 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8007 | int ret = -ENOMEM; |
| 8008 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8009 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8010 | if (!md_wq) |
| 8011 | goto err_wq; |
| 8012 | |
| 8013 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8014 | if (!md_misc_wq) |
| 8015 | goto err_misc_wq; |
| 8016 | |
| 8017 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8018 | goto err_md; |
| 8019 | |
| 8020 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8021 | goto err_mdp; |
| 8022 | mdp_major = ret; |
| 8023 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8024 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8025 | md_probe, NULL, NULL); |
| 8026 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8027 | md_probe, NULL, NULL); |
| 8028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8029 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8030 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8031 | |
| 8032 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8033 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8034 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8035 | err_mdp: |
| 8036 | unregister_blkdev(MD_MAJOR, "md"); |
| 8037 | err_md: |
| 8038 | destroy_workqueue(md_misc_wq); |
| 8039 | err_misc_wq: |
| 8040 | destroy_workqueue(md_wq); |
| 8041 | err_wq: |
| 8042 | return ret; |
| 8043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8044 | |
| 8045 | #ifndef MODULE |
| 8046 | |
| 8047 | /* |
| 8048 | * Searches all registered partitions for autorun RAID arrays |
| 8049 | * at boot time. |
| 8050 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8051 | |
| 8052 | static LIST_HEAD(all_detected_devices); |
| 8053 | struct detected_devices_node { |
| 8054 | struct list_head list; |
| 8055 | dev_t dev; |
| 8056 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8057 | |
| 8058 | void md_autodetect_dev(dev_t dev) |
| 8059 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8060 | struct detected_devices_node *node_detected_dev; |
| 8061 | |
| 8062 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8063 | if (node_detected_dev) { |
| 8064 | node_detected_dev->dev = dev; |
| 8065 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8066 | } else { |
| 8067 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8068 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8070 | } |
| 8071 | |
| 8072 | |
| 8073 | static void autostart_arrays(int part) |
| 8074 | { |
| 8075 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8076 | struct detected_devices_node *node_detected_dev; |
| 8077 | dev_t dev; |
| 8078 | int i_scanned, i_passed; |
| 8079 | |
| 8080 | i_scanned = 0; |
| 8081 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8082 | |
| 8083 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8084 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8085 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8086 | i_scanned++; |
| 8087 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8088 | struct detected_devices_node, list); |
| 8089 | list_del(&node_detected_dev->list); |
| 8090 | dev = node_detected_dev->dev; |
| 8091 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8092 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8093 | if (IS_ERR(rdev)) |
| 8094 | continue; |
| 8095 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8096 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8097 | MD_BUG(); |
| 8098 | continue; |
| 8099 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8100 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8101 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8102 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8103 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8104 | |
| 8105 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8106 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8107 | |
| 8108 | autorun_devices(part); |
| 8109 | } |
| 8110 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8111 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8112 | |
| 8113 | static __exit void md_exit(void) |
| 8114 | { |
| 8115 | mddev_t *mddev; |
| 8116 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8117 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8118 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8119 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8120 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8121 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8122 | unregister_blkdev(mdp_major, "mdp"); |
| 8123 | unregister_reboot_notifier(&md_notifier); |
| 8124 | unregister_sysctl_table(raid_table_header); |
| 8125 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8126 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8127 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8128 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8129 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8130 | destroy_workqueue(md_misc_wq); |
| 8131 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | } |
| 8133 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8134 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8135 | module_exit(md_exit) |
| 8136 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8137 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8138 | { |
| 8139 | return sprintf(buffer, "%d", start_readonly); |
| 8140 | } |
| 8141 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8142 | { |
| 8143 | char *e; |
| 8144 | int num = simple_strtoul(val, &e, 10); |
| 8145 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8146 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8147 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8148 | } |
| 8149 | return -EINVAL; |
| 8150 | } |
| 8151 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8152 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8153 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8154 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8155 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8157 | EXPORT_SYMBOL(register_md_personality); |
| 8158 | EXPORT_SYMBOL(unregister_md_personality); |
| 8159 | EXPORT_SYMBOL(md_error); |
| 8160 | EXPORT_SYMBOL(md_done_sync); |
| 8161 | EXPORT_SYMBOL(md_write_start); |
| 8162 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8163 | EXPORT_SYMBOL(md_register_thread); |
| 8164 | EXPORT_SYMBOL(md_unregister_thread); |
| 8165 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8166 | EXPORT_SYMBOL(md_check_recovery); |
| 8167 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8168 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8169 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8170 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |