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; |
| 290 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 291 | if (mddev == NULL || mddev->pers == NULL) { |
| 292 | bio_io_error(bio); |
| 293 | return 0; |
| 294 | } |
| 295 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 296 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 297 | DEFINE_WAIT(__wait); |
| 298 | for (;;) { |
| 299 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 300 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 301 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 302 | break; |
| 303 | rcu_read_unlock(); |
| 304 | schedule(); |
| 305 | rcu_read_lock(); |
| 306 | } |
| 307 | finish_wait(&mddev->sb_wait, &__wait); |
| 308 | } |
| 309 | atomic_inc(&mddev->active_io); |
| 310 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 311 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 312 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 313 | |
| 314 | cpu = part_stat_lock(); |
| 315 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 316 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 317 | bio_sectors(bio)); |
| 318 | part_stat_unlock(); |
| 319 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 321 | wake_up(&mddev->sb_wait); |
| 322 | |
| 323 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 326 | /* mddev_suspend makes sure no new requests are submitted |
| 327 | * to the device, and that any requests that have been submitted |
| 328 | * are completely handled. |
| 329 | * Once ->stop is called and completes, the module will be completely |
| 330 | * unused. |
| 331 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 332 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 333 | { |
| 334 | BUG_ON(mddev->suspended); |
| 335 | mddev->suspended = 1; |
| 336 | synchronize_rcu(); |
| 337 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 338 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 339 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 340 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 341 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 342 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 343 | { |
| 344 | mddev->suspended = 0; |
| 345 | wake_up(&mddev->sb_wait); |
| 346 | mddev->pers->quiesce(mddev, 0); |
| 347 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 349 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 350 | int mddev_congested(mddev_t *mddev, int bits) |
| 351 | { |
| 352 | return mddev->suspended; |
| 353 | } |
| 354 | EXPORT_SYMBOL(mddev_congested); |
| 355 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 356 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 357 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 358 | */ |
| 359 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 360 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 361 | { |
| 362 | mdk_rdev_t *rdev = bio->bi_private; |
| 363 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 364 | |
| 365 | rdev_dec_pending(rdev, mddev); |
| 366 | |
| 367 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 368 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 369 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 370 | } |
| 371 | bio_put(bio); |
| 372 | } |
| 373 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 374 | static void submit_flushes(mddev_t *mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 375 | { |
| 376 | mdk_rdev_t *rdev; |
| 377 | |
| 378 | rcu_read_lock(); |
| 379 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 380 | if (rdev->raid_disk >= 0 && |
| 381 | !test_bit(Faulty, &rdev->flags)) { |
| 382 | /* Take two references, one is dropped |
| 383 | * when request finishes, one after |
| 384 | * we reclaim rcu_read_lock |
| 385 | */ |
| 386 | struct bio *bi; |
| 387 | atomic_inc(&rdev->nr_pending); |
| 388 | atomic_inc(&rdev->nr_pending); |
| 389 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 390 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 391 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 392 | bi->bi_private = rdev; |
| 393 | bi->bi_bdev = rdev->bdev; |
| 394 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 395 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 396 | rcu_read_lock(); |
| 397 | rdev_dec_pending(rdev, mddev); |
| 398 | } |
| 399 | rcu_read_unlock(); |
| 400 | } |
| 401 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 402 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 403 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 404 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 405 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 406 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 407 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | /* an empty barrier - all done */ |
| 409 | bio_endio(bio, 0); |
| 410 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 411 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 412 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame^] | 415 | |
| 416 | mddev->flush_bio = NULL; |
| 417 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | } |
| 419 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 420 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | { |
| 422 | spin_lock_irq(&mddev->write_lock); |
| 423 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 426 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | spin_unlock_irq(&mddev->write_lock); |
| 428 | |
| 429 | atomic_set(&mddev->flush_pending, 1); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 430 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 432 | submit_flushes(mddev); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 433 | |
| 434 | if (atomic_dec_and_test(&mddev->flush_pending)) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 435 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 436 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 437 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 438 | |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 439 | /* Support for plugging. |
| 440 | * This mirrors the plugging support in request_queue, but does not |
| 441 | * require having a whole queue |
| 442 | */ |
| 443 | static void plugger_work(struct work_struct *work) |
| 444 | { |
| 445 | struct plug_handle *plug = |
| 446 | container_of(work, struct plug_handle, unplug_work); |
| 447 | plug->unplug_fn(plug); |
| 448 | } |
| 449 | static void plugger_timeout(unsigned long data) |
| 450 | { |
| 451 | struct plug_handle *plug = (void *)data; |
| 452 | kblockd_schedule_work(NULL, &plug->unplug_work); |
| 453 | } |
| 454 | void plugger_init(struct plug_handle *plug, |
| 455 | void (*unplug_fn)(struct plug_handle *)) |
| 456 | { |
| 457 | plug->unplug_flag = 0; |
| 458 | plug->unplug_fn = unplug_fn; |
| 459 | init_timer(&plug->unplug_timer); |
| 460 | plug->unplug_timer.function = plugger_timeout; |
| 461 | plug->unplug_timer.data = (unsigned long)plug; |
| 462 | INIT_WORK(&plug->unplug_work, plugger_work); |
| 463 | } |
| 464 | EXPORT_SYMBOL_GPL(plugger_init); |
| 465 | |
| 466 | void plugger_set_plug(struct plug_handle *plug) |
| 467 | { |
| 468 | if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) |
| 469 | mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(plugger_set_plug); |
| 472 | |
| 473 | int plugger_remove_plug(struct plug_handle *plug) |
| 474 | { |
| 475 | if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { |
| 476 | del_timer(&plug->unplug_timer); |
| 477 | return 1; |
| 478 | } else |
| 479 | return 0; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(plugger_remove_plug); |
| 482 | |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 485 | { |
| 486 | atomic_inc(&mddev->active); |
| 487 | return mddev; |
| 488 | } |
| 489 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 490 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | static void mddev_put(mddev_t *mddev) |
| 493 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 494 | struct bio_set *bs = NULL; |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 497 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 498 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 499 | mddev->ctime == 0 && !mddev->hold_active) { |
| 500 | /* Array is not configured at all, and not held active, |
| 501 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 503 | bs = mddev->bio_set; |
| 504 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 505 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 506 | /* We did a probe so need to clean up. Call |
| 507 | * queue_work inside the spinlock so that |
| 508 | * flush_workqueue() after mddev_find will |
| 509 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 510 | */ |
| 511 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 512 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 513 | } else |
| 514 | kfree(mddev); |
| 515 | } |
| 516 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 517 | if (bs) |
| 518 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 521 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 522 | { |
| 523 | mutex_init(&mddev->open_mutex); |
| 524 | mutex_init(&mddev->reconfig_mutex); |
| 525 | mutex_init(&mddev->bitmap_info.mutex); |
| 526 | INIT_LIST_HEAD(&mddev->disks); |
| 527 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 528 | init_timer(&mddev->safemode_timer); |
| 529 | atomic_set(&mddev->active, 1); |
| 530 | atomic_set(&mddev->openers, 0); |
| 531 | atomic_set(&mddev->active_io, 0); |
| 532 | spin_lock_init(&mddev->write_lock); |
| 533 | atomic_set(&mddev->flush_pending, 0); |
| 534 | init_waitqueue_head(&mddev->sb_wait); |
| 535 | init_waitqueue_head(&mddev->recovery_wait); |
| 536 | mddev->reshape_position = MaxSector; |
| 537 | mddev->resync_min = 0; |
| 538 | mddev->resync_max = MaxSector; |
| 539 | mddev->level = LEVEL_NONE; |
| 540 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 541 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | static mddev_t * mddev_find(dev_t unit) |
| 544 | { |
| 545 | mddev_t *mddev, *new = NULL; |
| 546 | |
| 547 | retry: |
| 548 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 550 | if (unit) { |
| 551 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 552 | if (mddev->unit == unit) { |
| 553 | mddev_get(mddev); |
| 554 | spin_unlock(&all_mddevs_lock); |
| 555 | kfree(new); |
| 556 | return mddev; |
| 557 | } |
| 558 | |
| 559 | if (new) { |
| 560 | list_add(&new->all_mddevs, &all_mddevs); |
| 561 | spin_unlock(&all_mddevs_lock); |
| 562 | new->hold_active = UNTIL_IOCTL; |
| 563 | return new; |
| 564 | } |
| 565 | } else if (new) { |
| 566 | /* find an unused unit number */ |
| 567 | static int next_minor = 512; |
| 568 | int start = next_minor; |
| 569 | int is_free = 0; |
| 570 | int dev = 0; |
| 571 | while (!is_free) { |
| 572 | dev = MKDEV(MD_MAJOR, next_minor); |
| 573 | next_minor++; |
| 574 | if (next_minor > MINORMASK) |
| 575 | next_minor = 0; |
| 576 | if (next_minor == start) { |
| 577 | /* Oh dear, all in use. */ |
| 578 | spin_unlock(&all_mddevs_lock); |
| 579 | kfree(new); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | is_free = 1; |
| 584 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 585 | if (mddev->unit == dev) { |
| 586 | is_free = 0; |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | new->unit = dev; |
| 591 | new->md_minor = MINOR(dev); |
| 592 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | list_add(&new->all_mddevs, &all_mddevs); |
| 594 | spin_unlock(&all_mddevs_lock); |
| 595 | return new; |
| 596 | } |
| 597 | spin_unlock(&all_mddevs_lock); |
| 598 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 599 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (!new) |
| 601 | return NULL; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | new->unit = unit; |
| 604 | if (MAJOR(unit) == MD_MAJOR) |
| 605 | new->md_minor = MINOR(unit); |
| 606 | else |
| 607 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 608 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 609 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | goto retry; |
| 612 | } |
| 613 | |
| 614 | static inline int mddev_lock(mddev_t * mddev) |
| 615 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 616 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 619 | static inline int mddev_is_locked(mddev_t *mddev) |
| 620 | { |
| 621 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 622 | } |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | static inline int mddev_trylock(mddev_t * mddev) |
| 625 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 626 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 629 | static struct attribute_group md_redundancy_group; |
| 630 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 631 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 633 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 634 | /* These cannot be removed under reconfig_mutex as |
| 635 | * an access to the files will try to take reconfig_mutex |
| 636 | * while holding the file unremovable, which leads to |
| 637 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 638 | * So hold set sysfs_active while the remove in happeing, |
| 639 | * and anything else which might set ->to_remove or my |
| 640 | * otherwise change the sysfs namespace will fail with |
| 641 | * -EBUSY if sysfs_active is still set. |
| 642 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 643 | * test it under the same mutex to ensure its correct value |
| 644 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 645 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 646 | struct attribute_group *to_remove = mddev->to_remove; |
| 647 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 648 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 649 | mutex_unlock(&mddev->reconfig_mutex); |
| 650 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 651 | if (mddev->kobj.sd) { |
| 652 | if (to_remove != &md_redundancy_group) |
| 653 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 654 | if (mddev->pers == NULL || |
| 655 | mddev->pers->sync_request == NULL) { |
| 656 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 657 | if (mddev->sysfs_action) |
| 658 | sysfs_put(mddev->sysfs_action); |
| 659 | mddev->sysfs_action = NULL; |
| 660 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 661 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 662 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 663 | } else |
| 664 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 666 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 669 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 671 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 673 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (rdev->desc_nr == nr) |
| 675 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return NULL; |
| 678 | } |
| 679 | |
| 680 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 681 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | mdk_rdev_t *rdev; |
| 683 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 684 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (rdev->bdev->bd_dev == dev) |
| 686 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return NULL; |
| 689 | } |
| 690 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 691 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 692 | { |
| 693 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 694 | list_for_each_entry(pers, &pers_list, list) { |
| 695 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 696 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 697 | if (strcmp(pers->name, clevel)==0) |
| 698 | return pers; |
| 699 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
| 702 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 703 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 704 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 706 | sector_t num_sectors = i_size_read(bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 707 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 711 | { |
| 712 | if (rdev->sb_page) |
| 713 | MD_BUG(); |
| 714 | |
| 715 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 716 | if (!rdev->sb_page) { |
| 717 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 718 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 725 | { |
| 726 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 727 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | rdev->sb_loaded = 0; |
| 729 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 730 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 731 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
| 735 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 736 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 737 | { |
| 738 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 739 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 741 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 742 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 743 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 744 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 745 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 746 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 747 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 748 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 749 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 750 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 754 | sector_t sector, int size, struct page *page) |
| 755 | { |
| 756 | /* write first size bytes of page to sector of rdev |
| 757 | * Increment mddev->pending_writes before returning |
| 758 | * and decrement it on completion, waking up sb_wait |
| 759 | * if zero is reached. |
| 760 | * If an error occurred, call md_error |
| 761 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 762 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 763 | |
| 764 | bio->bi_bdev = rdev->bdev; |
| 765 | bio->bi_sector = sector; |
| 766 | bio_add_page(bio, page, size, 0); |
| 767 | bio->bi_private = rdev; |
| 768 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 769 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 770 | atomic_inc(&mddev->pending_writes); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 771 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA, |
| 772 | bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | void md_super_wait(mddev_t *mddev) |
| 776 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 777 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 778 | DEFINE_WAIT(wq); |
| 779 | for(;;) { |
| 780 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 781 | if (atomic_read(&mddev->pending_writes)==0) |
| 782 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 783 | schedule(); |
| 784 | } |
| 785 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 786 | } |
| 787 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 788 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 793 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
| 794 | struct page *page, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 796 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | struct completion event; |
| 798 | int ret; |
| 799 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 800 | rw |= REQ_SYNC | REQ_UNPLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 802 | bio->bi_bdev = rdev->bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | bio->bi_sector = sector; |
| 804 | bio_add_page(bio, page, size, 0); |
| 805 | init_completion(&event); |
| 806 | bio->bi_private = &event; |
| 807 | bio->bi_end_io = bi_complete; |
| 808 | submit_bio(rw, bio); |
| 809 | wait_for_completion(&event); |
| 810 | |
| 811 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 812 | bio_put(bio); |
| 813 | return ret; |
| 814 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 815 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 817 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
| 819 | char b[BDEVNAME_SIZE]; |
| 820 | if (!rdev->sb_page) { |
| 821 | MD_BUG(); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | if (rdev->sb_loaded) |
| 825 | return 0; |
| 826 | |
| 827 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 828 | if (!sync_page_io(rdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | goto fail; |
| 830 | rdev->sb_loaded = 1; |
| 831 | return 0; |
| 832 | |
| 833 | fail: |
| 834 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 835 | bdevname(rdev->bdev,b)); |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | |
| 839 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 840 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 841 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 842 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 843 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 844 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 848 | { |
| 849 | int ret; |
| 850 | mdp_super_t *tmp1, *tmp2; |
| 851 | |
| 852 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 853 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 854 | |
| 855 | if (!tmp1 || !tmp2) { |
| 856 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 857 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | goto abort; |
| 859 | } |
| 860 | |
| 861 | *tmp1 = *sb1; |
| 862 | *tmp2 = *sb2; |
| 863 | |
| 864 | /* |
| 865 | * nr_disks is not constant |
| 866 | */ |
| 867 | tmp1->nr_disks = 0; |
| 868 | tmp2->nr_disks = 0; |
| 869 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 870 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 872 | kfree(tmp1); |
| 873 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return ret; |
| 875 | } |
| 876 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 877 | |
| 878 | static u32 md_csum_fold(u32 csum) |
| 879 | { |
| 880 | csum = (csum & 0xffff) + (csum >> 16); |
| 881 | return (csum & 0xffff) + (csum >> 16); |
| 882 | } |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 885 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 886 | u64 newcsum = 0; |
| 887 | u32 *sb32 = (u32*)sb; |
| 888 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | unsigned int disk_csum, csum; |
| 890 | |
| 891 | disk_csum = sb->sb_csum; |
| 892 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 893 | |
| 894 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 895 | newcsum += sb32[i]; |
| 896 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 897 | |
| 898 | |
| 899 | #ifdef CONFIG_ALPHA |
| 900 | /* This used to use csum_partial, which was wrong for several |
| 901 | * reasons including that different results are returned on |
| 902 | * different architectures. It isn't critical that we get exactly |
| 903 | * the same return value as before (we always csum_fold before |
| 904 | * testing, and that removes any differences). However as we |
| 905 | * know that csum_partial always returned a 16bit value on |
| 906 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 907 | */ |
| 908 | sb->sb_csum = md_csum_fold(disk_csum); |
| 909 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 911 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | return csum; |
| 913 | } |
| 914 | |
| 915 | |
| 916 | /* |
| 917 | * Handle superblock details. |
| 918 | * We want to be able to handle multiple superblock formats |
| 919 | * so we have a common interface to them all, and an array of |
| 920 | * different handlers. |
| 921 | * We rely on user-space to write the initial superblock, and support |
| 922 | * reading and updating of superblocks. |
| 923 | * Interface methods are: |
| 924 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 925 | * loads and validates a superblock on dev. |
| 926 | * if refdev != NULL, compare superblocks on both devices |
| 927 | * Return: |
| 928 | * 0 - dev has a superblock that is compatible with refdev |
| 929 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 930 | * so dev should be used as the refdev in future |
| 931 | * -EINVAL superblock incompatible or invalid |
| 932 | * -othererror e.g. -EIO |
| 933 | * |
| 934 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 935 | * Verify that dev is acceptable into mddev. |
| 936 | * The first time, mddev->raid_disks will be 0, and data from |
| 937 | * dev should be merged in. Subsequent calls check that dev |
| 938 | * is new enough. Return 0 or -EINVAL |
| 939 | * |
| 940 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 941 | * Update the superblock for rdev with data in mddev |
| 942 | * This does not write to disc. |
| 943 | * |
| 944 | */ |
| 945 | |
| 946 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 947 | char *name; |
| 948 | struct module *owner; |
| 949 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 950 | int minor_version); |
| 951 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 952 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 953 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 954 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | }; |
| 956 | |
| 957 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 958 | * Check that the given mddev has no bitmap. |
| 959 | * |
| 960 | * This function is called from the run method of all personalities that do not |
| 961 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 962 | * has a bitmap. Otherwise, it returns 0. |
| 963 | * |
| 964 | */ |
| 965 | int md_check_no_bitmap(mddev_t *mddev) |
| 966 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 967 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 968 | return 0; |
| 969 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 970 | mdname(mddev), mddev->pers->name); |
| 971 | return 1; |
| 972 | } |
| 973 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 974 | |
| 975 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | * load_super for 0.90.0 |
| 977 | */ |
| 978 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 979 | { |
| 980 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 981 | mdp_super_t *sb; |
| 982 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
| 984 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 985 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | * it's at the end of the disk. |
| 987 | * |
| 988 | * It also happens to be a multiple of 4Kb. |
| 989 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 990 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 992 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | if (ret) return ret; |
| 994 | |
| 995 | ret = -EINVAL; |
| 996 | |
| 997 | bdevname(rdev->bdev, b); |
| 998 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 999 | |
| 1000 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1001 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1002 | b); |
| 1003 | goto abort; |
| 1004 | } |
| 1005 | |
| 1006 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1007 | sb->minor_version < 90 || |
| 1008 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1010 | sb->major_version, sb->minor_version, |
| 1011 | b); |
| 1012 | goto abort; |
| 1013 | } |
| 1014 | |
| 1015 | if (sb->raid_disks <= 0) |
| 1016 | goto abort; |
| 1017 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1018 | 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] | 1019 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1020 | b); |
| 1021 | goto abort; |
| 1022 | } |
| 1023 | |
| 1024 | rdev->preferred_minor = sb->md_minor; |
| 1025 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1026 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
| 1028 | if (sb->level == LEVEL_MULTIPATH) |
| 1029 | rdev->desc_nr = -1; |
| 1030 | else |
| 1031 | rdev->desc_nr = sb->this_disk.number; |
| 1032 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1033 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1035 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | __u64 ev1, ev2; |
| 1037 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1038 | if (!uuid_equal(refsb, sb)) { |
| 1039 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1040 | b, bdevname(refdev->bdev,b2)); |
| 1041 | goto abort; |
| 1042 | } |
| 1043 | if (!sb_equal(refsb, sb)) { |
| 1044 | printk(KERN_WARNING "md: %s has same UUID" |
| 1045 | " but different superblock to %s\n", |
| 1046 | b, bdevname(refdev->bdev, b2)); |
| 1047 | goto abort; |
| 1048 | } |
| 1049 | ev1 = md_event(sb); |
| 1050 | ev2 = md_event(refsb); |
| 1051 | if (ev1 > ev2) |
| 1052 | ret = 1; |
| 1053 | else |
| 1054 | ret = 0; |
| 1055 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1056 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1058 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1059 | /* "this cannot possibly happen" ... */ |
| 1060 | ret = -EINVAL; |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | abort: |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * validate_super for 0.90.0 |
| 1068 | */ |
| 1069 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1070 | { |
| 1071 | mdp_disk_t *desc; |
| 1072 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1073 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1075 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1076 | clear_bit(Faulty, &rdev->flags); |
| 1077 | clear_bit(In_sync, &rdev->flags); |
| 1078 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | if (mddev->raid_disks == 0) { |
| 1081 | mddev->major_version = 0; |
| 1082 | mddev->minor_version = sb->minor_version; |
| 1083 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1084 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1085 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | mddev->ctime = sb->ctime; |
| 1087 | mddev->utime = sb->utime; |
| 1088 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1089 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | mddev->layout = sb->layout; |
| 1091 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1092 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1093 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1094 | mddev->bitmap_info.offset = 0; |
| 1095 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1097 | if (mddev->minor_version >= 91) { |
| 1098 | mddev->reshape_position = sb->reshape_position; |
| 1099 | mddev->delta_disks = sb->delta_disks; |
| 1100 | mddev->new_level = sb->new_level; |
| 1101 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1102 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1103 | } else { |
| 1104 | mddev->reshape_position = MaxSector; |
| 1105 | mddev->delta_disks = 0; |
| 1106 | mddev->new_level = mddev->level; |
| 1107 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1108 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1112 | mddev->recovery_cp = MaxSector; |
| 1113 | else { |
| 1114 | if (sb->events_hi == sb->cp_events_hi && |
| 1115 | sb->events_lo == sb->cp_events_lo) { |
| 1116 | mddev->recovery_cp = sb->recovery_cp; |
| 1117 | } else |
| 1118 | mddev->recovery_cp = 0; |
| 1119 | } |
| 1120 | |
| 1121 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1122 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1123 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1124 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1125 | |
| 1126 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1129 | mddev->bitmap_info.file == NULL) |
| 1130 | mddev->bitmap_info.offset = |
| 1131 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1132 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1133 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1134 | /* Insist on good event counter while assembling, except |
| 1135 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1137 | if (sb->disks[rdev->desc_nr].state & ( |
| 1138 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1139 | if (ev1 < mddev->events) |
| 1140 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1141 | } else if (mddev->bitmap) { |
| 1142 | /* if adding to array with a bitmap, then we can accept an |
| 1143 | * older device ... but not too old. |
| 1144 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1145 | if (ev1 < mddev->bitmap->events_cleared) |
| 1146 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1147 | } else { |
| 1148 | if (ev1 < mddev->events) |
| 1149 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1150 | return 0; |
| 1151 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | desc = sb->disks + rdev->desc_nr; |
| 1155 | |
| 1156 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1157 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1158 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1159 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1160 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1162 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1163 | /* active but not in sync implies recovery up to |
| 1164 | * reshape position. We don't know exactly where |
| 1165 | * that is, so set to zero for now */ |
| 1166 | if (mddev->minor_version >= 91) { |
| 1167 | rdev->recovery_offset = 0; |
| 1168 | rdev->raid_disk = desc->raid_disk; |
| 1169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1171 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1172 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1173 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1174 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * sync_super for 0.90.0 |
| 1180 | */ |
| 1181 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1182 | { |
| 1183 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | mdk_rdev_t *rdev2; |
| 1185 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* make rdev->sb match mddev data.. |
| 1189 | * |
| 1190 | * 1/ zero out disks |
| 1191 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1192 | * 3/ any empty disks < next_spare become removed |
| 1193 | * |
| 1194 | * disks[0] gets initialised to REMOVED because |
| 1195 | * we cannot be sure from other fields if it has |
| 1196 | * been initialised or not. |
| 1197 | */ |
| 1198 | int i; |
| 1199 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1200 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1201 | rdev->sb_size = MD_SB_BYTES; |
| 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1204 | |
| 1205 | memset(sb, 0, sizeof(*sb)); |
| 1206 | |
| 1207 | sb->md_magic = MD_SB_MAGIC; |
| 1208 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | sb->patch_version = mddev->patch_version; |
| 1210 | sb->gvalid_words = 0; /* ignored */ |
| 1211 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1212 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1213 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1214 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1215 | |
| 1216 | sb->ctime = mddev->ctime; |
| 1217 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1218 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | sb->raid_disks = mddev->raid_disks; |
| 1220 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1221 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | sb->utime = mddev->utime; |
| 1223 | sb->state = 0; |
| 1224 | sb->events_hi = (mddev->events>>32); |
| 1225 | sb->events_lo = (u32)mddev->events; |
| 1226 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1227 | if (mddev->reshape_position == MaxSector) |
| 1228 | sb->minor_version = 90; |
| 1229 | else { |
| 1230 | sb->minor_version = 91; |
| 1231 | sb->reshape_position = mddev->reshape_position; |
| 1232 | sb->new_level = mddev->new_level; |
| 1233 | sb->delta_disks = mddev->delta_disks; |
| 1234 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1235 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1236 | } |
| 1237 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | if (mddev->in_sync) |
| 1239 | { |
| 1240 | sb->recovery_cp = mddev->recovery_cp; |
| 1241 | sb->cp_events_hi = (mddev->events>>32); |
| 1242 | sb->cp_events_lo = (u32)mddev->events; |
| 1243 | if (mddev->recovery_cp == MaxSector) |
| 1244 | sb->state = (1<< MD_SB_CLEAN); |
| 1245 | } else |
| 1246 | sb->recovery_cp = 0; |
| 1247 | |
| 1248 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1249 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1251 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1252 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1255 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1257 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1258 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1259 | |
| 1260 | if (rdev2->raid_disk >= 0 && |
| 1261 | sb->minor_version >= 91) |
| 1262 | /* we have nowhere to store the recovery_offset, |
| 1263 | * but if it is not below the reshape_position, |
| 1264 | * we can piggy-back on that. |
| 1265 | */ |
| 1266 | is_active = 1; |
| 1267 | if (rdev2->raid_disk < 0 || |
| 1268 | test_bit(Faulty, &rdev2->flags)) |
| 1269 | is_active = 0; |
| 1270 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1271 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1273 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1274 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | d = &sb->disks[rdev2->desc_nr]; |
| 1276 | nr_disks++; |
| 1277 | d->number = rdev2->desc_nr; |
| 1278 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1279 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1280 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | d->raid_disk = rdev2->raid_disk; |
| 1282 | else |
| 1283 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1284 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1286 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1288 | if (test_bit(In_sync, &rdev2->flags)) |
| 1289 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | active++; |
| 1291 | working++; |
| 1292 | } else { |
| 1293 | d->state = 0; |
| 1294 | spare++; |
| 1295 | working++; |
| 1296 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1297 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1298 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1301 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1302 | mdp_disk_t *d = &sb->disks[i]; |
| 1303 | if (d->state == 0 && d->number == 0) { |
| 1304 | d->number = i; |
| 1305 | d->raid_disk = i; |
| 1306 | d->state = (1<<MD_DISK_REMOVED); |
| 1307 | d->state |= (1<<MD_DISK_FAULTY); |
| 1308 | failed++; |
| 1309 | } |
| 1310 | } |
| 1311 | sb->nr_disks = nr_disks; |
| 1312 | sb->active_disks = active; |
| 1313 | sb->working_disks = working; |
| 1314 | sb->failed_disks = failed; |
| 1315 | sb->spare_disks = spare; |
| 1316 | |
| 1317 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1318 | sb->sb_csum = calc_sb_csum(sb); |
| 1319 | } |
| 1320 | |
| 1321 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1322 | * rdev_size_change for 0.90.0 |
| 1323 | */ |
| 1324 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1325 | 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] | 1326 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1327 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1328 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1329 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1330 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1331 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1332 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1333 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1334 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1335 | rdev->sb_page); |
| 1336 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1337 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | * version 1 superblock |
| 1343 | */ |
| 1344 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1345 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1347 | __le32 disk_csum; |
| 1348 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | unsigned long long newcsum; |
| 1350 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1351 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | int i; |
| 1353 | |
| 1354 | disk_csum = sb->sb_csum; |
| 1355 | sb->sb_csum = 0; |
| 1356 | newcsum = 0; |
| 1357 | for (i=0; size>=4; size -= 4 ) |
| 1358 | newcsum += le32_to_cpu(*isuper++); |
| 1359 | |
| 1360 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1361 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1364 | sb->sb_csum = disk_csum; |
| 1365 | return cpu_to_le32(csum); |
| 1366 | } |
| 1367 | |
| 1368 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1369 | { |
| 1370 | struct mdp_superblock_1 *sb; |
| 1371 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1372 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1374 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1377 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | * It is always aligned to a 4K boundary and |
| 1379 | * depeding on minor_version, it can be: |
| 1380 | * 0: At least 8K, but less than 12K, from end of device |
| 1381 | * 1: At start of device |
| 1382 | * 2: 4K from start of device. |
| 1383 | */ |
| 1384 | switch(minor_version) { |
| 1385 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1386 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1387 | sb_start -= 8*2; |
| 1388 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | break; |
| 1390 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1391 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | break; |
| 1393 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1394 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | break; |
| 1396 | default: |
| 1397 | return -EINVAL; |
| 1398 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1399 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1401 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1402 | * and it is safe to read 4k, so we do that |
| 1403 | */ |
| 1404 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | if (ret) return ret; |
| 1406 | |
| 1407 | |
| 1408 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1409 | |
| 1410 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1411 | sb->major_version != cpu_to_le32(1) || |
| 1412 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1413 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1414 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | return -EINVAL; |
| 1416 | |
| 1417 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1418 | printk("md: invalid superblock checksum on %s\n", |
| 1419 | bdevname(rdev->bdev,b)); |
| 1420 | return -EINVAL; |
| 1421 | } |
| 1422 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1423 | printk("md: data_size too small on %s\n", |
| 1424 | bdevname(rdev->bdev,b)); |
| 1425 | return -EINVAL; |
| 1426 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | rdev->preferred_minor = 0xffff; |
| 1429 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1430 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1432 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1433 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1434 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1435 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1436 | |
| 1437 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1438 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1439 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1440 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1441 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1442 | rdev->desc_nr = -1; |
| 1443 | else |
| 1444 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1445 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1446 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1447 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1448 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | __u64 ev1, ev2; |
| 1450 | struct mdp_superblock_1 *refsb = |
| 1451 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1452 | |
| 1453 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1454 | sb->level != refsb->level || |
| 1455 | sb->layout != refsb->layout || |
| 1456 | sb->chunksize != refsb->chunksize) { |
| 1457 | printk(KERN_WARNING "md: %s has strangely different" |
| 1458 | " superblock to %s\n", |
| 1459 | bdevname(rdev->bdev,b), |
| 1460 | bdevname(refdev->bdev,b2)); |
| 1461 | return -EINVAL; |
| 1462 | } |
| 1463 | ev1 = le64_to_cpu(sb->events); |
| 1464 | ev2 = le64_to_cpu(refsb->events); |
| 1465 | |
| 1466 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1467 | ret = 1; |
| 1468 | else |
| 1469 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1471 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1472 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1473 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1475 | rdev->sectors = rdev->sb_start; |
| 1476 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1478 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1479 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1480 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1481 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1485 | { |
| 1486 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1487 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1489 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1490 | clear_bit(Faulty, &rdev->flags); |
| 1491 | clear_bit(In_sync, &rdev->flags); |
| 1492 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | if (mddev->raid_disks == 0) { |
| 1495 | mddev->major_version = 1; |
| 1496 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1497 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1498 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1500 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1501 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1502 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | mddev->layout = le32_to_cpu(sb->layout); |
| 1504 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1505 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1506 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1507 | mddev->bitmap_info.offset = 0; |
| 1508 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
| 1510 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1511 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1512 | |
| 1513 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1514 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1515 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1516 | mddev->bitmap_info.file == NULL ) |
| 1517 | mddev->bitmap_info.offset = |
| 1518 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1519 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1520 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1521 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1522 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1523 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1524 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1525 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1526 | } else { |
| 1527 | mddev->reshape_position = MaxSector; |
| 1528 | mddev->delta_disks = 0; |
| 1529 | mddev->new_level = mddev->level; |
| 1530 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1531 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1534 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1535 | /* Insist of good event counter while assembling, except for |
| 1536 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1538 | if (rdev->desc_nr >= 0 && |
| 1539 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1540 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1541 | if (ev1 < mddev->events) |
| 1542 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1543 | } else if (mddev->bitmap) { |
| 1544 | /* If adding to array with a bitmap, then we can accept an |
| 1545 | * older device, but not too old. |
| 1546 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1547 | if (ev1 < mddev->bitmap->events_cleared) |
| 1548 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1549 | } else { |
| 1550 | if (ev1 < mddev->events) |
| 1551 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1552 | return 0; |
| 1553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1555 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1556 | if (rdev->desc_nr < 0 || |
| 1557 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1558 | role = 0xffff; |
| 1559 | rdev->desc_nr = -1; |
| 1560 | } else |
| 1561 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | switch(role) { |
| 1563 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | break; |
| 1565 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1566 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | break; |
| 1568 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1569 | if ((le32_to_cpu(sb->feature_map) & |
| 1570 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1571 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1572 | else |
| 1573 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | rdev->raid_disk = role; |
| 1575 | break; |
| 1576 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1577 | if (sb->devflags & WriteMostly1) |
| 1578 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1579 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1580 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1586 | { |
| 1587 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | mdk_rdev_t *rdev2; |
| 1589 | int max_dev, i; |
| 1590 | /* make rdev->sb match mddev and rdev data. */ |
| 1591 | |
| 1592 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1593 | |
| 1594 | sb->feature_map = 0; |
| 1595 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1596 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1598 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1599 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1600 | |
| 1601 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1602 | sb->events = cpu_to_le64(mddev->events); |
| 1603 | if (mddev->in_sync) |
| 1604 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1605 | else |
| 1606 | sb->resync_offset = cpu_to_le64(0); |
| 1607 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1608 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1609 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1610 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1611 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1612 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1613 | sb->level = cpu_to_le32(mddev->level); |
| 1614 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1615 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1616 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1617 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1618 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1619 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1620 | |
| 1621 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1622 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1623 | sb->feature_map |= |
| 1624 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1625 | sb->recovery_offset = |
| 1626 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1629 | if (mddev->reshape_position != MaxSector) { |
| 1630 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1631 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1632 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1633 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1634 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1635 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1636 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1639 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | if (rdev2->desc_nr+1 > max_dev) |
| 1641 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1642 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1643 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1644 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1645 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1646 | rdev->sb_size = max_dev * 2 + 256; |
| 1647 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1648 | if (rdev->sb_size & bmask) |
| 1649 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1650 | } else |
| 1651 | max_dev = le32_to_cpu(sb->max_dev); |
| 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | for (i=0; i<max_dev;i++) |
| 1654 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1655 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1656 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1658 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1660 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1662 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1663 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | else |
| 1665 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1666 | } |
| 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1669 | } |
| 1670 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1671 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1672 | 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] | 1673 | { |
| 1674 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1675 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1676 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1677 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1678 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1679 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1680 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1681 | max_sectors -= rdev->data_offset; |
| 1682 | if (!num_sectors || num_sectors > max_sectors) |
| 1683 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1684 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1685 | /* minor version 0 with bitmap we can't move */ |
| 1686 | return 0; |
| 1687 | } else { |
| 1688 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1689 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1690 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1691 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1692 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1693 | if (!num_sectors || num_sectors > max_sectors) |
| 1694 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1695 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1696 | } |
| 1697 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1698 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1699 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1700 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1701 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1702 | rdev->sb_page); |
| 1703 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1704 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1707 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | [0] = { |
| 1709 | .name = "0.90.0", |
| 1710 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1711 | .load_super = super_90_load, |
| 1712 | .validate_super = super_90_validate, |
| 1713 | .sync_super = super_90_sync, |
| 1714 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | }, |
| 1716 | [1] = { |
| 1717 | .name = "md-1", |
| 1718 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1719 | .load_super = super_1_load, |
| 1720 | .validate_super = super_1_validate, |
| 1721 | .sync_super = super_1_sync, |
| 1722 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | }, |
| 1724 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1727 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1728 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1730 | rcu_read_lock(); |
| 1731 | rdev_for_each_rcu(rdev, mddev1) |
| 1732 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1733 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1734 | rdev2->bdev->bd_contains) { |
| 1735 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1736 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1737 | } |
| 1738 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static LIST_HEAD(pending_raid_disks); |
| 1743 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1744 | /* |
| 1745 | * Try to register data integrity profile for an mddev |
| 1746 | * |
| 1747 | * This is called when an array is started and after a disk has been kicked |
| 1748 | * from the array. It only succeeds if all working and active component devices |
| 1749 | * are integrity capable with matching profiles. |
| 1750 | */ |
| 1751 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1752 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1753 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1754 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1755 | if (list_empty(&mddev->disks)) |
| 1756 | return 0; /* nothing to do */ |
| 1757 | if (blk_get_integrity(mddev->gendisk)) |
| 1758 | return 0; /* already registered */ |
| 1759 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1760 | /* skip spares and non-functional disks */ |
| 1761 | if (test_bit(Faulty, &rdev->flags)) |
| 1762 | continue; |
| 1763 | if (rdev->raid_disk < 0) |
| 1764 | continue; |
| 1765 | /* |
| 1766 | * If at least one rdev is not integrity capable, we can not |
| 1767 | * enable data integrity for the md device. |
| 1768 | */ |
| 1769 | if (!bdev_get_integrity(rdev->bdev)) |
| 1770 | return -EINVAL; |
| 1771 | if (!reference) { |
| 1772 | /* Use the first rdev as the reference */ |
| 1773 | reference = rdev; |
| 1774 | continue; |
| 1775 | } |
| 1776 | /* does this rdev's profile match the reference profile? */ |
| 1777 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1778 | rdev->bdev->bd_disk) < 0) |
| 1779 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1780 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1781 | /* |
| 1782 | * All component devices are integrity capable and have matching |
| 1783 | * profiles, register the common profile for the md device. |
| 1784 | */ |
| 1785 | if (blk_integrity_register(mddev->gendisk, |
| 1786 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1787 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1788 | mdname(mddev)); |
| 1789 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1790 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1791 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1792 | mdname(mddev)); |
| 1793 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1794 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1795 | EXPORT_SYMBOL(md_integrity_register); |
| 1796 | |
| 1797 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1798 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1799 | { |
| 1800 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1801 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1802 | |
| 1803 | if (!bi_mddev) /* nothing to do */ |
| 1804 | return; |
| 1805 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1806 | return; |
| 1807 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1808 | rdev->bdev->bd_disk) >= 0) |
| 1809 | return; |
| 1810 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1811 | blk_integrity_unregister(mddev->gendisk); |
| 1812 | } |
| 1813 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1816 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1817 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1818 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1819 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1820 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | |
| 1822 | if (rdev->mddev) { |
| 1823 | MD_BUG(); |
| 1824 | return -EINVAL; |
| 1825 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1826 | |
| 1827 | /* prevent duplicates */ |
| 1828 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1829 | return -EEXIST; |
| 1830 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1831 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1832 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1833 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1834 | if (mddev->pers) { |
| 1835 | /* Cannot change size, so fail |
| 1836 | * If mddev->level <= 0, then we don't care |
| 1837 | * about aligning sizes (e.g. linear) |
| 1838 | */ |
| 1839 | if (mddev->level > 0) |
| 1840 | return -ENOSPC; |
| 1841 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1842 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
| 1845 | /* Verify rdev->desc_nr is unique. |
| 1846 | * If it is -1, assign a free number, else |
| 1847 | * check number is not in use |
| 1848 | */ |
| 1849 | if (rdev->desc_nr < 0) { |
| 1850 | int choice = 0; |
| 1851 | if (mddev->pers) choice = mddev->raid_disks; |
| 1852 | while (find_rdev_nr(mddev, choice)) |
| 1853 | choice++; |
| 1854 | rdev->desc_nr = choice; |
| 1855 | } else { |
| 1856 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1857 | return -EBUSY; |
| 1858 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1859 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1860 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1861 | mdname(mddev), mddev->max_disks); |
| 1862 | return -EBUSY; |
| 1863 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1864 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1865 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1866 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1869 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1870 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1871 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1872 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1873 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1874 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1875 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1876 | /* failure here is OK */; |
| 1877 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1878 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1879 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1880 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1881 | |
| 1882 | /* May as well allow recovery to be retried once */ |
| 1883 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1886 | |
| 1887 | fail: |
| 1888 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1889 | b, mdname(mddev)); |
| 1890 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1893 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1894 | { |
| 1895 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1896 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1897 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1901 | { |
| 1902 | char b[BDEVNAME_SIZE]; |
| 1903 | if (!rdev->mddev) { |
| 1904 | MD_BUG(); |
| 1905 | return; |
| 1906 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1907 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1908 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1910 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1911 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1912 | sysfs_put(rdev->sysfs_state); |
| 1913 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1914 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1915 | * writing to 'remove' to "dev/state". We also need |
| 1916 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1917 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1918 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1919 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1920 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1921 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * prevent the device from being mounted, repartitioned or |
| 1926 | * otherwise reused by a RAID array (or any other kernel |
| 1927 | * subsystem), by bd_claiming the device. |
| 1928 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1929 | 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] | 1930 | { |
| 1931 | int err = 0; |
| 1932 | struct block_device *bdev; |
| 1933 | char b[BDEVNAME_SIZE]; |
| 1934 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1935 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | if (IS_ERR(bdev)) { |
| 1937 | printk(KERN_ERR "md: could not open %s.\n", |
| 1938 | __bdevname(dev, b)); |
| 1939 | return PTR_ERR(bdev); |
| 1940 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1941 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | if (err) { |
| 1943 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1944 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1945 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | return err; |
| 1947 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1948 | if (!shared) |
| 1949 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | rdev->bdev = bdev; |
| 1951 | return err; |
| 1952 | } |
| 1953 | |
| 1954 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1955 | { |
| 1956 | struct block_device *bdev = rdev->bdev; |
| 1957 | rdev->bdev = NULL; |
| 1958 | if (!bdev) |
| 1959 | MD_BUG(); |
| 1960 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1961 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | void md_autodetect_dev(dev_t dev); |
| 1965 | |
| 1966 | static void export_rdev(mdk_rdev_t * rdev) |
| 1967 | { |
| 1968 | char b[BDEVNAME_SIZE]; |
| 1969 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1970 | bdevname(rdev->bdev,b)); |
| 1971 | if (rdev->mddev) |
| 1972 | MD_BUG(); |
| 1973 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1975 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1976 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | #endif |
| 1978 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1979 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1983 | { |
| 1984 | unbind_rdev_from_array(rdev); |
| 1985 | export_rdev(rdev); |
| 1986 | } |
| 1987 | |
| 1988 | static void export_array(mddev_t *mddev) |
| 1989 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1990 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1992 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | if (!rdev->mddev) { |
| 1994 | MD_BUG(); |
| 1995 | continue; |
| 1996 | } |
| 1997 | kick_rdev_from_array(rdev); |
| 1998 | } |
| 1999 | if (!list_empty(&mddev->disks)) |
| 2000 | MD_BUG(); |
| 2001 | mddev->raid_disks = 0; |
| 2002 | mddev->major_version = 0; |
| 2003 | } |
| 2004 | |
| 2005 | static void print_desc(mdp_disk_t *desc) |
| 2006 | { |
| 2007 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2008 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2009 | } |
| 2010 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2011 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | { |
| 2013 | int i; |
| 2014 | |
| 2015 | printk(KERN_INFO |
| 2016 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2017 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2018 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2019 | sb->ctime); |
| 2020 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2021 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2022 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2023 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2024 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2025 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2026 | sb->failed_disks, sb->spare_disks, |
| 2027 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2028 | |
| 2029 | printk(KERN_INFO); |
| 2030 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2031 | mdp_disk_t *desc; |
| 2032 | |
| 2033 | desc = sb->disks + i; |
| 2034 | if (desc->number || desc->major || desc->minor || |
| 2035 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2036 | printk(" D %2d: ", i); |
| 2037 | print_desc(desc); |
| 2038 | } |
| 2039 | } |
| 2040 | printk(KERN_INFO "md: THIS: "); |
| 2041 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2044 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2045 | { |
| 2046 | __u8 *uuid; |
| 2047 | |
| 2048 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2049 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2050 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2051 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2052 | le32_to_cpu(sb->major_version), |
| 2053 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2054 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2055 | sb->set_name, |
| 2056 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2057 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2058 | |
| 2059 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2060 | printk(KERN_INFO |
| 2061 | "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] | 2062 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2063 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2064 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2065 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2066 | le32_to_cpu(sb->level), |
| 2067 | (unsigned long long)le64_to_cpu(sb->size), |
| 2068 | le32_to_cpu(sb->raid_disks), |
| 2069 | le32_to_cpu(sb->layout), |
| 2070 | le32_to_cpu(sb->chunksize), |
| 2071 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2072 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2073 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2074 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2075 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2076 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2077 | sb->devflags, |
| 2078 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2079 | (unsigned long long)le64_to_cpu(sb->events), |
| 2080 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2081 | le32_to_cpu(sb->sb_csum), |
| 2082 | le32_to_cpu(sb->max_dev) |
| 2083 | ); |
| 2084 | } |
| 2085 | |
| 2086 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | { |
| 2088 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2089 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2090 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2091 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2092 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2094 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2095 | switch (major_version) { |
| 2096 | case 0: |
| 2097 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2098 | break; |
| 2099 | case 1: |
| 2100 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2101 | break; |
| 2102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | } else |
| 2104 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2105 | } |
| 2106 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2107 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2109 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | mdk_rdev_t *rdev; |
| 2111 | mddev_t *mddev; |
| 2112 | char b[BDEVNAME_SIZE]; |
| 2113 | |
| 2114 | printk("\n"); |
| 2115 | printk("md: **********************************\n"); |
| 2116 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2117 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2118 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2120 | if (mddev->bitmap) |
| 2121 | bitmap_print_sb(mddev->bitmap); |
| 2122 | else |
| 2123 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2124 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2126 | printk("\n"); |
| 2127 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2128 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2129 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
| 2131 | printk("md: **********************************\n"); |
| 2132 | printk("\n"); |
| 2133 | } |
| 2134 | |
| 2135 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2136 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2138 | /* Update each superblock (in-memory image), but |
| 2139 | * if we are allowed to, skip spares which already |
| 2140 | * have the right event counter, or have one earlier |
| 2141 | * (which would mean they aren't being marked as dirty |
| 2142 | * with the rest of the array) |
| 2143 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2145 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2146 | if (rdev->sb_events == mddev->events || |
| 2147 | (nospares && |
| 2148 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2149 | rdev->sb_events+1 == mddev->events)) { |
| 2150 | /* Don't update this superblock */ |
| 2151 | rdev->sb_loaded = 2; |
| 2152 | } else { |
| 2153 | super_types[mddev->major_version]. |
| 2154 | sync_super(mddev, rdev); |
| 2155 | rdev->sb_loaded = 1; |
| 2156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2160 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2163 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2164 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2167 | /* First make sure individual recovery_offsets are correct */ |
| 2168 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2169 | if (rdev->raid_disk >= 0 && |
| 2170 | mddev->delta_disks >= 0 && |
| 2171 | !test_bit(In_sync, &rdev->flags) && |
| 2172 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2173 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2174 | |
| 2175 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2176 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2177 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2178 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41dc | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2179 | if (!mddev->external) |
| 2180 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2181 | wake_up(&mddev->sb_wait); |
| 2182 | return; |
| 2183 | } |
| 2184 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2185 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2186 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2187 | mddev->utime = get_seconds(); |
| 2188 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2189 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2190 | force_change = 1; |
| 2191 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2192 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2193 | * though if events isn't the right even/odd, we will have to do |
| 2194 | * spares after all |
| 2195 | */ |
| 2196 | nospares = 1; |
| 2197 | if (force_change) |
| 2198 | nospares = 0; |
| 2199 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2200 | /* If the array is degraded, then skipping spares is both |
| 2201 | * dangerous and fairly pointless. |
| 2202 | * Dangerous because a device that was removed from the array |
| 2203 | * might have a event_count that still looks up-to-date, |
| 2204 | * so it can be re-added without a resync. |
| 2205 | * Pointless because if there are any spares to skip, |
| 2206 | * then a recovery will happen and soon that array won't |
| 2207 | * be degraded any more and the spare can go back to sleep then. |
| 2208 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2209 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2210 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2211 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2212 | |
| 2213 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2214 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2215 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2216 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2217 | && mddev->can_decrease_events |
| 2218 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2219 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2220 | mddev->can_decrease_events = 0; |
| 2221 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2222 | /* otherwise we have to go forward and ... */ |
| 2223 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2224 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | |
| 2227 | if (!mddev->events) { |
| 2228 | /* |
| 2229 | * oops, this 64-bit counter should never wrap. |
| 2230 | * Either we are in around ~1 trillion A.C., assuming |
| 2231 | * 1 reboot per second, or we have a bug: |
| 2232 | */ |
| 2233 | MD_BUG(); |
| 2234 | mddev->events --; |
| 2235 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2236 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2237 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
| 2239 | dprintk(KERN_INFO |
| 2240 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2241 | mdname(mddev),mddev->in_sync); |
| 2242 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2243 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2244 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | char b[BDEVNAME_SIZE]; |
| 2246 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2247 | if (rdev->sb_loaded != 1) |
| 2248 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2249 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | dprintk("(skipping faulty "); |
| 2251 | |
| 2252 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2253 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2254 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2255 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2256 | rdev->sb_page); |
| 2257 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2258 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2259 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2260 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } else |
| 2263 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2264 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | /* only need to write one superblock... */ |
| 2266 | break; |
| 2267 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2268 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2269 | /* 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] | 2270 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2271 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2272 | if (mddev->in_sync != sync_req || |
| 2273 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2274 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2275 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2276 | goto repeat; |
| 2277 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2278 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2279 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2280 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2281 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2282 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2286 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2287 | * We want to accept with case. For this we use cmd_match. |
| 2288 | */ |
| 2289 | static int cmd_match(const char *cmd, const char *str) |
| 2290 | { |
| 2291 | /* See if cmd, written into a sysfs file, matches |
| 2292 | * str. They must either be the same, or cmd can |
| 2293 | * have a trailing newline |
| 2294 | */ |
| 2295 | while (*cmd && *str && *cmd == *str) { |
| 2296 | cmd++; |
| 2297 | str++; |
| 2298 | } |
| 2299 | if (*cmd == '\n') |
| 2300 | cmd++; |
| 2301 | if (*str || *cmd) |
| 2302 | return 0; |
| 2303 | return 1; |
| 2304 | } |
| 2305 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2306 | struct rdev_sysfs_entry { |
| 2307 | struct attribute attr; |
| 2308 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2309 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2310 | }; |
| 2311 | |
| 2312 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2313 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2314 | { |
| 2315 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2316 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2317 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2318 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2319 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2320 | sep = ","; |
| 2321 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2322 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2323 | len += sprintf(page+len, "%sin_sync",sep); |
| 2324 | sep = ","; |
| 2325 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2326 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2327 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2328 | sep = ","; |
| 2329 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2330 | if (test_bit(Blocked, &rdev->flags)) { |
| 2331 | len += sprintf(page+len, "%sblocked", sep); |
| 2332 | sep = ","; |
| 2333 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2334 | if (!test_bit(Faulty, &rdev->flags) && |
| 2335 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2336 | len += sprintf(page+len, "%sspare", sep); |
| 2337 | sep = ","; |
| 2338 | } |
| 2339 | return len+sprintf(page+len, "\n"); |
| 2340 | } |
| 2341 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2342 | static ssize_t |
| 2343 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2344 | { |
| 2345 | /* can write |
| 2346 | * faulty - simulates and error |
| 2347 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2348 | * writemostly - sets write_mostly |
| 2349 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2350 | * blocked - sets the Blocked flag |
| 2351 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2352 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2353 | */ |
| 2354 | int err = -EINVAL; |
| 2355 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2356 | md_error(rdev->mddev, rdev); |
| 2357 | err = 0; |
| 2358 | } else if (cmd_match(buf, "remove")) { |
| 2359 | if (rdev->raid_disk >= 0) |
| 2360 | err = -EBUSY; |
| 2361 | else { |
| 2362 | mddev_t *mddev = rdev->mddev; |
| 2363 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2364 | if (mddev->pers) |
| 2365 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2366 | md_new_event(mddev); |
| 2367 | err = 0; |
| 2368 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2369 | } else if (cmd_match(buf, "writemostly")) { |
| 2370 | set_bit(WriteMostly, &rdev->flags); |
| 2371 | err = 0; |
| 2372 | } else if (cmd_match(buf, "-writemostly")) { |
| 2373 | clear_bit(WriteMostly, &rdev->flags); |
| 2374 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2375 | } else if (cmd_match(buf, "blocked")) { |
| 2376 | set_bit(Blocked, &rdev->flags); |
| 2377 | err = 0; |
| 2378 | } else if (cmd_match(buf, "-blocked")) { |
| 2379 | clear_bit(Blocked, &rdev->flags); |
| 2380 | wake_up(&rdev->blocked_wait); |
| 2381 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2382 | md_wakeup_thread(rdev->mddev->thread); |
| 2383 | |
| 2384 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2385 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2386 | set_bit(In_sync, &rdev->flags); |
| 2387 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2388 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2389 | if (!err) |
| 2390 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2391 | return err ? err : len; |
| 2392 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2393 | static struct rdev_sysfs_entry rdev_state = |
| 2394 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2395 | |
| 2396 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2397 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2398 | { |
| 2399 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2400 | } |
| 2401 | |
| 2402 | static ssize_t |
| 2403 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2404 | { |
| 2405 | char *e; |
| 2406 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2407 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2408 | atomic_set(&rdev->corrected_errors, n); |
| 2409 | return len; |
| 2410 | } |
| 2411 | return -EINVAL; |
| 2412 | } |
| 2413 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2414 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2415 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2416 | static ssize_t |
| 2417 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2418 | { |
| 2419 | if (rdev->raid_disk < 0) |
| 2420 | return sprintf(page, "none\n"); |
| 2421 | else |
| 2422 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2423 | } |
| 2424 | |
| 2425 | static ssize_t |
| 2426 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2427 | { |
| 2428 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2429 | int err; |
| 2430 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2431 | int slot = simple_strtoul(buf, &e, 10); |
| 2432 | if (strncmp(buf, "none", 4)==0) |
| 2433 | slot = -1; |
| 2434 | else if (e==buf || (*e && *e!= '\n')) |
| 2435 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2436 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2437 | /* Setting 'slot' on an active array requires also |
| 2438 | * updating the 'rd%d' link, and communicating |
| 2439 | * with the personality with ->hot_*_disk. |
| 2440 | * For now we only support removing |
| 2441 | * failed/spare devices. This normally happens automatically, |
| 2442 | * but not when the metadata is externally managed. |
| 2443 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2444 | if (rdev->raid_disk == -1) |
| 2445 | return -EEXIST; |
| 2446 | /* personality does all needed checks */ |
| 2447 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2448 | return -EINVAL; |
| 2449 | err = rdev->mddev->pers-> |
| 2450 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2451 | if (err) |
| 2452 | return err; |
| 2453 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2454 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2455 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2456 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2457 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2458 | } else if (rdev->mddev->pers) { |
| 2459 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2460 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2461 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2462 | */ |
| 2463 | |
| 2464 | if (rdev->raid_disk != -1) |
| 2465 | return -EBUSY; |
| 2466 | |
| 2467 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2468 | return -EINVAL; |
| 2469 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2470 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2471 | if (rdev2->raid_disk == slot) |
| 2472 | return -EEXIST; |
| 2473 | |
| 2474 | rdev->raid_disk = slot; |
| 2475 | if (test_bit(In_sync, &rdev->flags)) |
| 2476 | rdev->saved_raid_disk = slot; |
| 2477 | else |
| 2478 | rdev->saved_raid_disk = -1; |
| 2479 | err = rdev->mddev->pers-> |
| 2480 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2481 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2482 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2483 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2484 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2485 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2486 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2487 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2488 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2489 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2490 | } else { |
| 2491 | if (slot >= rdev->mddev->raid_disks) |
| 2492 | return -ENOSPC; |
| 2493 | rdev->raid_disk = slot; |
| 2494 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2495 | clear_bit(Faulty, &rdev->flags); |
| 2496 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2497 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2498 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2499 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2500 | return len; |
| 2501 | } |
| 2502 | |
| 2503 | |
| 2504 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2505 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2506 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2507 | static ssize_t |
| 2508 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2509 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2510 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | static ssize_t |
| 2514 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2515 | { |
| 2516 | char *e; |
| 2517 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2518 | if (e==buf || (*e && *e != '\n')) |
| 2519 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2520 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2521 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2522 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2523 | /* Must set offset before size, so overlap checks |
| 2524 | * can be sane */ |
| 2525 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2526 | rdev->data_offset = offset; |
| 2527 | return len; |
| 2528 | } |
| 2529 | |
| 2530 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2531 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2532 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2533 | static ssize_t |
| 2534 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2535 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2536 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2537 | } |
| 2538 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2539 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2540 | { |
| 2541 | /* check if two start/length pairs overlap */ |
| 2542 | if (s1+l1 <= s2) |
| 2543 | return 0; |
| 2544 | if (s2+l2 <= s1) |
| 2545 | return 0; |
| 2546 | return 1; |
| 2547 | } |
| 2548 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2549 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2550 | { |
| 2551 | unsigned long long blocks; |
| 2552 | sector_t new; |
| 2553 | |
| 2554 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2555 | return -EINVAL; |
| 2556 | |
| 2557 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2558 | return -EINVAL; /* sector conversion overflow */ |
| 2559 | |
| 2560 | new = blocks * 2; |
| 2561 | if (new != blocks * 2) |
| 2562 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2563 | |
| 2564 | *sectors = new; |
| 2565 | return 0; |
| 2566 | } |
| 2567 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2568 | static ssize_t |
| 2569 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2570 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2571 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2572 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2573 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2574 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2575 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2576 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2577 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2578 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2579 | sectors = super_types[my_mddev->major_version]. |
| 2580 | rdev_size_change(rdev, sectors); |
| 2581 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2582 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2583 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2584 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2585 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2586 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2587 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2588 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2589 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2590 | rdev->sectors = sectors; |
| 2591 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2592 | /* need to check that all other rdevs with the same ->bdev |
| 2593 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2594 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2595 | * we have to change it back, we will have the lock again. |
| 2596 | */ |
| 2597 | mddev_t *mddev; |
| 2598 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2599 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2600 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2601 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2602 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2603 | mdk_rdev_t *rdev2; |
| 2604 | |
| 2605 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2606 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2607 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2608 | (rdev->bdev == rdev2->bdev && |
| 2609 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2610 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2611 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2612 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2613 | overlap = 1; |
| 2614 | break; |
| 2615 | } |
| 2616 | mddev_unlock(mddev); |
| 2617 | if (overlap) { |
| 2618 | mddev_put(mddev); |
| 2619 | break; |
| 2620 | } |
| 2621 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2622 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2623 | if (overlap) { |
| 2624 | /* Someone else could have slipped in a size |
| 2625 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2626 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2627 | * safe, and trust userspace not to race with |
| 2628 | * itself |
| 2629 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2630 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2631 | return -EBUSY; |
| 2632 | } |
| 2633 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2634 | return len; |
| 2635 | } |
| 2636 | |
| 2637 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2638 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2639 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2640 | |
| 2641 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2642 | { |
| 2643 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2644 | |
| 2645 | if (test_bit(In_sync, &rdev->flags) || |
| 2646 | recovery_start == MaxSector) |
| 2647 | return sprintf(page, "none\n"); |
| 2648 | |
| 2649 | return sprintf(page, "%llu\n", recovery_start); |
| 2650 | } |
| 2651 | |
| 2652 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2653 | { |
| 2654 | unsigned long long recovery_start; |
| 2655 | |
| 2656 | if (cmd_match(buf, "none")) |
| 2657 | recovery_start = MaxSector; |
| 2658 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2659 | return -EINVAL; |
| 2660 | |
| 2661 | if (rdev->mddev->pers && |
| 2662 | rdev->raid_disk >= 0) |
| 2663 | return -EBUSY; |
| 2664 | |
| 2665 | rdev->recovery_offset = recovery_start; |
| 2666 | if (recovery_start == MaxSector) |
| 2667 | set_bit(In_sync, &rdev->flags); |
| 2668 | else |
| 2669 | clear_bit(In_sync, &rdev->flags); |
| 2670 | return len; |
| 2671 | } |
| 2672 | |
| 2673 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2674 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2675 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2676 | static struct attribute *rdev_default_attrs[] = { |
| 2677 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2678 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2679 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2680 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2681 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2682 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2683 | NULL, |
| 2684 | }; |
| 2685 | static ssize_t |
| 2686 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2687 | { |
| 2688 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2689 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2690 | mddev_t *mddev = rdev->mddev; |
| 2691 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2692 | |
| 2693 | if (!entry->show) |
| 2694 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2695 | |
| 2696 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2697 | if (!rv) { |
| 2698 | if (rdev->mddev == NULL) |
| 2699 | rv = -EBUSY; |
| 2700 | else |
| 2701 | rv = entry->show(rdev, page); |
| 2702 | mddev_unlock(mddev); |
| 2703 | } |
| 2704 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | static ssize_t |
| 2708 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2709 | const char *page, size_t length) |
| 2710 | { |
| 2711 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2712 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2713 | ssize_t rv; |
| 2714 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2715 | |
| 2716 | if (!entry->store) |
| 2717 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2718 | if (!capable(CAP_SYS_ADMIN)) |
| 2719 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2720 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2721 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2722 | if (rdev->mddev == NULL) |
| 2723 | rv = -EBUSY; |
| 2724 | else |
| 2725 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2726 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2727 | } |
| 2728 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | static void rdev_free(struct kobject *ko) |
| 2732 | { |
| 2733 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2734 | kfree(rdev); |
| 2735 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2736 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2737 | .show = rdev_attr_show, |
| 2738 | .store = rdev_attr_store, |
| 2739 | }; |
| 2740 | static struct kobj_type rdev_ktype = { |
| 2741 | .release = rdev_free, |
| 2742 | .sysfs_ops = &rdev_sysfs_ops, |
| 2743 | .default_attrs = rdev_default_attrs, |
| 2744 | }; |
| 2745 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2746 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2747 | { |
| 2748 | rdev->desc_nr = -1; |
| 2749 | rdev->saved_raid_disk = -1; |
| 2750 | rdev->raid_disk = -1; |
| 2751 | rdev->flags = 0; |
| 2752 | rdev->data_offset = 0; |
| 2753 | rdev->sb_events = 0; |
| 2754 | rdev->last_read_error.tv_sec = 0; |
| 2755 | rdev->last_read_error.tv_nsec = 0; |
| 2756 | atomic_set(&rdev->nr_pending, 0); |
| 2757 | atomic_set(&rdev->read_errors, 0); |
| 2758 | atomic_set(&rdev->corrected_errors, 0); |
| 2759 | |
| 2760 | INIT_LIST_HEAD(&rdev->same_set); |
| 2761 | init_waitqueue_head(&rdev->blocked_wait); |
| 2762 | } |
| 2763 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | /* |
| 2765 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2766 | * |
| 2767 | * mark the device faulty if: |
| 2768 | * |
| 2769 | * - the device is nonexistent (zero size) |
| 2770 | * - the device has no valid superblock |
| 2771 | * |
| 2772 | * a faulty rdev _never_ has rdev->sb set. |
| 2773 | */ |
| 2774 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2775 | { |
| 2776 | char b[BDEVNAME_SIZE]; |
| 2777 | int err; |
| 2778 | mdk_rdev_t *rdev; |
| 2779 | sector_t size; |
| 2780 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2781 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | if (!rdev) { |
| 2783 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2784 | return ERR_PTR(-ENOMEM); |
| 2785 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2787 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | if ((err = alloc_disk_sb(rdev))) |
| 2789 | goto abort_free; |
| 2790 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2791 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | if (err) |
| 2793 | goto abort_free; |
| 2794 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2795 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2796 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2797 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | if (!size) { |
| 2799 | printk(KERN_WARNING |
| 2800 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2801 | bdevname(rdev->bdev,b)); |
| 2802 | err = -EINVAL; |
| 2803 | goto abort_free; |
| 2804 | } |
| 2805 | |
| 2806 | if (super_format >= 0) { |
| 2807 | err = super_types[super_format]. |
| 2808 | load_super(rdev, NULL, super_minor); |
| 2809 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2810 | printk(KERN_WARNING |
| 2811 | "md: %s does not have a valid v%d.%d " |
| 2812 | "superblock, not importing!\n", |
| 2813 | bdevname(rdev->bdev,b), |
| 2814 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | goto abort_free; |
| 2816 | } |
| 2817 | if (err < 0) { |
| 2818 | printk(KERN_WARNING |
| 2819 | "md: could not read %s's sb, not importing!\n", |
| 2820 | bdevname(rdev->bdev,b)); |
| 2821 | goto abort_free; |
| 2822 | } |
| 2823 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | return rdev; |
| 2826 | |
| 2827 | abort_free: |
| 2828 | if (rdev->sb_page) { |
| 2829 | if (rdev->bdev) |
| 2830 | unlock_rdev(rdev); |
| 2831 | free_disk_sb(rdev); |
| 2832 | } |
| 2833 | kfree(rdev); |
| 2834 | return ERR_PTR(err); |
| 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * Check a full RAID array for plausibility |
| 2839 | */ |
| 2840 | |
| 2841 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2842 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | { |
| 2844 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2845 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | char b[BDEVNAME_SIZE]; |
| 2847 | |
| 2848 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2849 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | switch (super_types[mddev->major_version]. |
| 2851 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2852 | case 1: |
| 2853 | freshest = rdev; |
| 2854 | break; |
| 2855 | case 0: |
| 2856 | break; |
| 2857 | default: |
| 2858 | printk( KERN_ERR \ |
| 2859 | "md: fatal superblock inconsistency in %s" |
| 2860 | " -- removing from array\n", |
| 2861 | bdevname(rdev->bdev,b)); |
| 2862 | kick_rdev_from_array(rdev); |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | super_types[mddev->major_version]. |
| 2867 | validate_super(mddev, freshest); |
| 2868 | |
| 2869 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2870 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2871 | if (mddev->max_disks && |
| 2872 | (rdev->desc_nr >= mddev->max_disks || |
| 2873 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2874 | printk(KERN_WARNING |
| 2875 | "md: %s: %s: only %d devices permitted\n", |
| 2876 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2877 | mddev->max_disks); |
| 2878 | kick_rdev_from_array(rdev); |
| 2879 | continue; |
| 2880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | if (rdev != freshest) |
| 2882 | if (super_types[mddev->major_version]. |
| 2883 | validate_super(mddev, rdev)) { |
| 2884 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2885 | " from array!\n", |
| 2886 | bdevname(rdev->bdev,b)); |
| 2887 | kick_rdev_from_array(rdev); |
| 2888 | continue; |
| 2889 | } |
| 2890 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2891 | rdev->desc_nr = i++; |
| 2892 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2893 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2894 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2895 | rdev->raid_disk = -1; |
| 2896 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | } |
| 2898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2901 | /* Read a fixed-point number. |
| 2902 | * Numbers in sysfs attributes should be in "standard" units where |
| 2903 | * possible, so time should be in seconds. |
| 2904 | * However we internally use a a much smaller unit such as |
| 2905 | * milliseconds or jiffies. |
| 2906 | * This function takes a decimal number with a possible fractional |
| 2907 | * component, and produces an integer which is the result of |
| 2908 | * multiplying that number by 10^'scale'. |
| 2909 | * all without any floating-point arithmetic. |
| 2910 | */ |
| 2911 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2912 | { |
| 2913 | unsigned long result = 0; |
| 2914 | long decimals = -1; |
| 2915 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2916 | if (*cp == '.') |
| 2917 | decimals = 0; |
| 2918 | else if (decimals < scale) { |
| 2919 | unsigned int value; |
| 2920 | value = *cp - '0'; |
| 2921 | result = result * 10 + value; |
| 2922 | if (decimals >= 0) |
| 2923 | decimals++; |
| 2924 | } |
| 2925 | cp++; |
| 2926 | } |
| 2927 | if (*cp == '\n') |
| 2928 | cp++; |
| 2929 | if (*cp) |
| 2930 | return -EINVAL; |
| 2931 | if (decimals < 0) |
| 2932 | decimals = 0; |
| 2933 | while (decimals < scale) { |
| 2934 | result *= 10; |
| 2935 | decimals ++; |
| 2936 | } |
| 2937 | *res = result; |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2942 | static void md_safemode_timeout(unsigned long data); |
| 2943 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2944 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2945 | safe_delay_show(mddev_t *mddev, char *page) |
| 2946 | { |
| 2947 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2948 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2949 | } |
| 2950 | static ssize_t |
| 2951 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2952 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2953 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2954 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2955 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2956 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2957 | if (msec == 0) |
| 2958 | mddev->safemode_delay = 0; |
| 2959 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2960 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2961 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2962 | if (mddev->safemode_delay == 0) |
| 2963 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2964 | if (mddev->safemode_delay < old_delay) |
| 2965 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2966 | } |
| 2967 | return len; |
| 2968 | } |
| 2969 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2970 | __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] | 2971 | |
| 2972 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2973 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2974 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2975 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2976 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2977 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2978 | else if (mddev->clevel[0]) |
| 2979 | return sprintf(page, "%s\n", mddev->clevel); |
| 2980 | else if (mddev->level != LEVEL_NONE) |
| 2981 | return sprintf(page, "%d\n", mddev->level); |
| 2982 | else |
| 2983 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2984 | } |
| 2985 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2986 | static ssize_t |
| 2987 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2988 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2989 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2990 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2991 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2992 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2993 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2994 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2995 | |
| 2996 | if (mddev->pers == NULL) { |
| 2997 | if (len == 0) |
| 2998 | return 0; |
| 2999 | if (len >= sizeof(mddev->clevel)) |
| 3000 | return -ENOSPC; |
| 3001 | strncpy(mddev->clevel, buf, len); |
| 3002 | if (mddev->clevel[len-1] == '\n') |
| 3003 | len--; |
| 3004 | mddev->clevel[len] = 0; |
| 3005 | mddev->level = LEVEL_NONE; |
| 3006 | return rv; |
| 3007 | } |
| 3008 | |
| 3009 | /* request to change the personality. Need to ensure: |
| 3010 | * - array is not engaged in resync/recovery/reshape |
| 3011 | * - old personality can be suspended |
| 3012 | * - new personality will access other array. |
| 3013 | */ |
| 3014 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3015 | if (mddev->sync_thread || |
| 3016 | mddev->reshape_position != MaxSector || |
| 3017 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3018 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3019 | |
| 3020 | if (!mddev->pers->quiesce) { |
| 3021 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3022 | mdname(mddev), mddev->pers->name); |
| 3023 | return -EINVAL; |
| 3024 | } |
| 3025 | |
| 3026 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3027 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3028 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3029 | strncpy(clevel, buf, len); |
| 3030 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3031 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3032 | clevel[len] = 0; |
| 3033 | if (strict_strtol(clevel, 10, &level)) |
| 3034 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3035 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3036 | if (request_module("md-%s", clevel) != 0) |
| 3037 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3038 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3039 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3040 | if (!pers || !try_module_get(pers->owner)) { |
| 3041 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3042 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3043 | return -EINVAL; |
| 3044 | } |
| 3045 | spin_unlock(&pers_lock); |
| 3046 | |
| 3047 | if (pers == mddev->pers) { |
| 3048 | /* Nothing to do! */ |
| 3049 | module_put(pers->owner); |
| 3050 | return rv; |
| 3051 | } |
| 3052 | if (!pers->takeover) { |
| 3053 | module_put(pers->owner); |
| 3054 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3055 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3056 | return -EINVAL; |
| 3057 | } |
| 3058 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3059 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3060 | rdev->new_raid_disk = rdev->raid_disk; |
| 3061 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3062 | /* ->takeover must set new_* and/or delta_disks |
| 3063 | * if it succeeds, and may set them when it fails. |
| 3064 | */ |
| 3065 | priv = pers->takeover(mddev); |
| 3066 | if (IS_ERR(priv)) { |
| 3067 | mddev->new_level = mddev->level; |
| 3068 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3069 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3070 | mddev->raid_disks -= mddev->delta_disks; |
| 3071 | mddev->delta_disks = 0; |
| 3072 | module_put(pers->owner); |
| 3073 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3074 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3075 | return PTR_ERR(priv); |
| 3076 | } |
| 3077 | |
| 3078 | /* Looks like we have a winner */ |
| 3079 | mddev_suspend(mddev); |
| 3080 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3081 | |
| 3082 | if (mddev->pers->sync_request == NULL && |
| 3083 | pers->sync_request != NULL) { |
| 3084 | /* need to add the md_redundancy_group */ |
| 3085 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3086 | printk(KERN_WARNING |
| 3087 | "md: cannot register extra attributes for %s\n", |
| 3088 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3089 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3090 | } |
| 3091 | if (mddev->pers->sync_request != NULL && |
| 3092 | pers->sync_request == NULL) { |
| 3093 | /* need to remove the md_redundancy_group */ |
| 3094 | if (mddev->to_remove == NULL) |
| 3095 | mddev->to_remove = &md_redundancy_group; |
| 3096 | } |
| 3097 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3098 | if (mddev->pers->sync_request == NULL && |
| 3099 | mddev->external) { |
| 3100 | /* We are converting from a no-redundancy array |
| 3101 | * to a redundancy array and metadata is managed |
| 3102 | * externally so we need to be sure that writes |
| 3103 | * won't block due to a need to transition |
| 3104 | * clean->dirty |
| 3105 | * until external management is started. |
| 3106 | */ |
| 3107 | mddev->in_sync = 0; |
| 3108 | mddev->safemode_delay = 0; |
| 3109 | mddev->safemode = 0; |
| 3110 | } |
| 3111 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3112 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3113 | char nm[20]; |
| 3114 | if (rdev->raid_disk < 0) |
| 3115 | continue; |
| 3116 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3117 | rdev->new_raid_disk = -1; |
| 3118 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3119 | continue; |
| 3120 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3121 | sysfs_remove_link(&mddev->kobj, nm); |
| 3122 | } |
| 3123 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3124 | if (rdev->raid_disk < 0) |
| 3125 | continue; |
| 3126 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3127 | continue; |
| 3128 | rdev->raid_disk = rdev->new_raid_disk; |
| 3129 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3130 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3131 | else { |
| 3132 | char nm[20]; |
| 3133 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3134 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3135 | printk("md: cannot register %s for %s after level change\n", |
| 3136 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3137 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3141 | mddev->pers = pers; |
| 3142 | mddev->private = priv; |
| 3143 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3144 | mddev->level = mddev->new_level; |
| 3145 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3146 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3147 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3148 | if (mddev->pers->sync_request == NULL) { |
| 3149 | /* this is now an array without redundancy, so |
| 3150 | * it must always be in_sync |
| 3151 | */ |
| 3152 | mddev->in_sync = 1; |
| 3153 | del_timer_sync(&mddev->safemode_timer); |
| 3154 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3155 | pers->run(mddev); |
| 3156 | mddev_resume(mddev); |
| 3157 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3158 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3159 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3160 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3161 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3162 | return rv; |
| 3163 | } |
| 3164 | |
| 3165 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3166 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3167 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3168 | |
| 3169 | static ssize_t |
| 3170 | layout_show(mddev_t *mddev, char *page) |
| 3171 | { |
| 3172 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3173 | if (mddev->reshape_position != MaxSector && |
| 3174 | mddev->layout != mddev->new_layout) |
| 3175 | return sprintf(page, "%d (%d)\n", |
| 3176 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3177 | return sprintf(page, "%d\n", mddev->layout); |
| 3178 | } |
| 3179 | |
| 3180 | static ssize_t |
| 3181 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3182 | { |
| 3183 | char *e; |
| 3184 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3185 | |
| 3186 | if (!*buf || (*e && *e != '\n')) |
| 3187 | return -EINVAL; |
| 3188 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3189 | if (mddev->pers) { |
| 3190 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3191 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3192 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3193 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3194 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3195 | if (err) { |
| 3196 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3197 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3198 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3199 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3200 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3201 | if (mddev->reshape_position == MaxSector) |
| 3202 | mddev->layout = n; |
| 3203 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3204 | return len; |
| 3205 | } |
| 3206 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3207 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3208 | |
| 3209 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3210 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3211 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3212 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3213 | if (mddev->raid_disks == 0) |
| 3214 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3215 | if (mddev->reshape_position != MaxSector && |
| 3216 | mddev->delta_disks != 0) |
| 3217 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3218 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3219 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3220 | } |
| 3221 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3222 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3223 | |
| 3224 | static ssize_t |
| 3225 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3226 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3227 | char *e; |
| 3228 | int rv = 0; |
| 3229 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3230 | |
| 3231 | if (!*buf || (*e && *e != '\n')) |
| 3232 | return -EINVAL; |
| 3233 | |
| 3234 | if (mddev->pers) |
| 3235 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3236 | else if (mddev->reshape_position != MaxSector) { |
| 3237 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3238 | mddev->delta_disks = n - olddisks; |
| 3239 | mddev->raid_disks = n; |
| 3240 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3241 | mddev->raid_disks = n; |
| 3242 | return rv ? rv : len; |
| 3243 | } |
| 3244 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3245 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3246 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3247 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3248 | chunk_size_show(mddev_t *mddev, char *page) |
| 3249 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3250 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3251 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3252 | return sprintf(page, "%d (%d)\n", |
| 3253 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3254 | mddev->chunk_sectors << 9); |
| 3255 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | static ssize_t |
| 3259 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3260 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3261 | char *e; |
| 3262 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3263 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3264 | if (!*buf || (*e && *e != '\n')) |
| 3265 | return -EINVAL; |
| 3266 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3267 | if (mddev->pers) { |
| 3268 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3269 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3270 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3271 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3272 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3273 | if (err) { |
| 3274 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3275 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3276 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3277 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3278 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3279 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3280 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3281 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3282 | return len; |
| 3283 | } |
| 3284 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3285 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3286 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3287 | static ssize_t |
| 3288 | resync_start_show(mddev_t *mddev, char *page) |
| 3289 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3290 | if (mddev->recovery_cp == MaxSector) |
| 3291 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3292 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3293 | } |
| 3294 | |
| 3295 | static ssize_t |
| 3296 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3297 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3298 | char *e; |
| 3299 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3300 | |
| 3301 | if (mddev->pers) |
| 3302 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3303 | if (cmd_match(buf, "none")) |
| 3304 | n = MaxSector; |
| 3305 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3306 | return -EINVAL; |
| 3307 | |
| 3308 | mddev->recovery_cp = n; |
| 3309 | return len; |
| 3310 | } |
| 3311 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3312 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3313 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3314 | /* |
| 3315 | * The array state can be: |
| 3316 | * |
| 3317 | * clear |
| 3318 | * No devices, no size, no level |
| 3319 | * Equivalent to STOP_ARRAY ioctl |
| 3320 | * inactive |
| 3321 | * May have some settings, but array is not active |
| 3322 | * all IO results in error |
| 3323 | * When written, doesn't tear down array, but just stops it |
| 3324 | * suspended (not supported yet) |
| 3325 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3326 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3327 | * readonly |
| 3328 | * no resync can happen. no superblocks get written. |
| 3329 | * write requests fail |
| 3330 | * read-auto |
| 3331 | * like readonly, but behaves like 'clean' on a write request. |
| 3332 | * |
| 3333 | * clean - no pending writes, but otherwise active. |
| 3334 | * When written to inactive array, starts without resync |
| 3335 | * If a write request arrives then |
| 3336 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3337 | * if not known, block and switch to write-pending |
| 3338 | * If written to an active array that has pending writes, then fails. |
| 3339 | * active |
| 3340 | * fully active: IO and resync can be happening. |
| 3341 | * When written to inactive array, starts with resync |
| 3342 | * |
| 3343 | * write-pending |
| 3344 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3345 | * |
| 3346 | * active-idle |
| 3347 | * like active, but no writes have been seen for a while (100msec). |
| 3348 | * |
| 3349 | */ |
| 3350 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3351 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3352 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3353 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3354 | "write-pending", "active-idle", NULL }; |
| 3355 | |
| 3356 | static int match_word(const char *word, char **list) |
| 3357 | { |
| 3358 | int n; |
| 3359 | for (n=0; list[n]; n++) |
| 3360 | if (cmd_match(word, list[n])) |
| 3361 | break; |
| 3362 | return n; |
| 3363 | } |
| 3364 | |
| 3365 | static ssize_t |
| 3366 | array_state_show(mddev_t *mddev, char *page) |
| 3367 | { |
| 3368 | enum array_state st = inactive; |
| 3369 | |
| 3370 | if (mddev->pers) |
| 3371 | switch(mddev->ro) { |
| 3372 | case 1: |
| 3373 | st = readonly; |
| 3374 | break; |
| 3375 | case 2: |
| 3376 | st = read_auto; |
| 3377 | break; |
| 3378 | case 0: |
| 3379 | if (mddev->in_sync) |
| 3380 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3381 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3382 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3383 | else if (mddev->safemode) |
| 3384 | st = active_idle; |
| 3385 | else |
| 3386 | st = active; |
| 3387 | } |
| 3388 | else { |
| 3389 | if (list_empty(&mddev->disks) && |
| 3390 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3391 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3392 | st = clear; |
| 3393 | else |
| 3394 | st = inactive; |
| 3395 | } |
| 3396 | return sprintf(page, "%s\n", array_states[st]); |
| 3397 | } |
| 3398 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3399 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3400 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3401 | static int do_md_run(mddev_t * mddev); |
| 3402 | static int restart_array(mddev_t *mddev); |
| 3403 | |
| 3404 | static ssize_t |
| 3405 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3406 | { |
| 3407 | int err = -EINVAL; |
| 3408 | enum array_state st = match_word(buf, array_states); |
| 3409 | switch(st) { |
| 3410 | case bad_word: |
| 3411 | break; |
| 3412 | case clear: |
| 3413 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3414 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3415 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3416 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3417 | break; |
| 3418 | case inactive: |
| 3419 | /* stopping an active array */ |
| 3420 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3421 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3422 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3423 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3424 | } else |
| 3425 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3426 | break; |
| 3427 | case suspended: |
| 3428 | break; /* not supported yet */ |
| 3429 | case readonly: |
| 3430 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3431 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3432 | else { |
| 3433 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3434 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3435 | err = do_md_run(mddev); |
| 3436 | } |
| 3437 | break; |
| 3438 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3439 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3440 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3441 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3442 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3443 | err = restart_array(mddev); |
| 3444 | if (err == 0) { |
| 3445 | mddev->ro = 2; |
| 3446 | set_disk_ro(mddev->gendisk, 0); |
| 3447 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3448 | } else { |
| 3449 | mddev->ro = 2; |
| 3450 | err = do_md_run(mddev); |
| 3451 | } |
| 3452 | break; |
| 3453 | case clean: |
| 3454 | if (mddev->pers) { |
| 3455 | restart_array(mddev); |
| 3456 | spin_lock_irq(&mddev->write_lock); |
| 3457 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3458 | if (mddev->in_sync == 0) { |
| 3459 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3460 | if (mddev->safemode == 1) |
| 3461 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3462 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3463 | } |
| 3464 | err = 0; |
| 3465 | } else |
| 3466 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3467 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3468 | } else |
| 3469 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3470 | break; |
| 3471 | case active: |
| 3472 | if (mddev->pers) { |
| 3473 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3474 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3475 | wake_up(&mddev->sb_wait); |
| 3476 | err = 0; |
| 3477 | } else { |
| 3478 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3479 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3480 | err = do_md_run(mddev); |
| 3481 | } |
| 3482 | break; |
| 3483 | case write_pending: |
| 3484 | case active_idle: |
| 3485 | /* these cannot be set */ |
| 3486 | break; |
| 3487 | } |
| 3488 | if (err) |
| 3489 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3490 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3491 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3492 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3493 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3494 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3495 | static struct md_sysfs_entry md_array_state = |
| 3496 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3497 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3498 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3499 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3500 | return sprintf(page, "%d\n", |
| 3501 | atomic_read(&mddev->max_corr_read_errors)); |
| 3502 | } |
| 3503 | |
| 3504 | static ssize_t |
| 3505 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3506 | { |
| 3507 | char *e; |
| 3508 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3509 | |
| 3510 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3511 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3512 | return len; |
| 3513 | } |
| 3514 | return -EINVAL; |
| 3515 | } |
| 3516 | |
| 3517 | static struct md_sysfs_entry max_corr_read_errors = |
| 3518 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3519 | max_corrected_read_errors_store); |
| 3520 | |
| 3521 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3522 | null_show(mddev_t *mddev, char *page) |
| 3523 | { |
| 3524 | return -EINVAL; |
| 3525 | } |
| 3526 | |
| 3527 | static ssize_t |
| 3528 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3529 | { |
| 3530 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3531 | /* The new device is added to the array. |
| 3532 | * If the array has a persistent superblock, we read the |
| 3533 | * superblock to initialise info and check validity. |
| 3534 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3535 | * which mainly checks size. |
| 3536 | */ |
| 3537 | char *e; |
| 3538 | int major = simple_strtoul(buf, &e, 10); |
| 3539 | int minor; |
| 3540 | dev_t dev; |
| 3541 | mdk_rdev_t *rdev; |
| 3542 | int err; |
| 3543 | |
| 3544 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3545 | return -EINVAL; |
| 3546 | minor = simple_strtoul(e+1, &e, 10); |
| 3547 | if (*e && *e != '\n') |
| 3548 | return -EINVAL; |
| 3549 | dev = MKDEV(major, minor); |
| 3550 | if (major != MAJOR(dev) || |
| 3551 | minor != MINOR(dev)) |
| 3552 | return -EOVERFLOW; |
| 3553 | |
| 3554 | |
| 3555 | if (mddev->persistent) { |
| 3556 | rdev = md_import_device(dev, mddev->major_version, |
| 3557 | mddev->minor_version); |
| 3558 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3559 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3560 | mdk_rdev_t, same_set); |
| 3561 | err = super_types[mddev->major_version] |
| 3562 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3563 | if (err < 0) |
| 3564 | goto out; |
| 3565 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3566 | } else if (mddev->external) |
| 3567 | rdev = md_import_device(dev, -2, -1); |
| 3568 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3569 | rdev = md_import_device(dev, -1, -1); |
| 3570 | |
| 3571 | if (IS_ERR(rdev)) |
| 3572 | return PTR_ERR(rdev); |
| 3573 | err = bind_rdev_to_array(rdev, mddev); |
| 3574 | out: |
| 3575 | if (err) |
| 3576 | export_rdev(rdev); |
| 3577 | return err ? err : len; |
| 3578 | } |
| 3579 | |
| 3580 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3581 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3582 | |
| 3583 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3584 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3585 | { |
| 3586 | char *end; |
| 3587 | unsigned long chunk, end_chunk; |
| 3588 | |
| 3589 | if (!mddev->bitmap) |
| 3590 | goto out; |
| 3591 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3592 | while (*buf) { |
| 3593 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3594 | if (buf == end) break; |
| 3595 | if (*end == '-') { /* range */ |
| 3596 | buf = end + 1; |
| 3597 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3598 | if (buf == end) break; |
| 3599 | } |
| 3600 | if (*end && !isspace(*end)) break; |
| 3601 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3602 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3603 | } |
| 3604 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3605 | out: |
| 3606 | return len; |
| 3607 | } |
| 3608 | |
| 3609 | static struct md_sysfs_entry md_bitmap = |
| 3610 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3611 | |
| 3612 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3613 | size_show(mddev_t *mddev, char *page) |
| 3614 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3615 | return sprintf(page, "%llu\n", |
| 3616 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3619 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3620 | |
| 3621 | static ssize_t |
| 3622 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3623 | { |
| 3624 | /* If array is inactive, we can reduce the component size, but |
| 3625 | * not increase it (except from 0). |
| 3626 | * If array is active, we can try an on-line resize |
| 3627 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3628 | sector_t sectors; |
| 3629 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3630 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3631 | if (err < 0) |
| 3632 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3633 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3634 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3635 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3636 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3637 | if (mddev->dev_sectors == 0 || |
| 3638 | mddev->dev_sectors > sectors) |
| 3639 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3640 | else |
| 3641 | err = -ENOSPC; |
| 3642 | } |
| 3643 | return err ? err : len; |
| 3644 | } |
| 3645 | |
| 3646 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3647 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3648 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3649 | |
| 3650 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3651 | * This is one of |
| 3652 | * 'none' for arrays with no metadata (good luck...) |
| 3653 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3654 | * or N.M for internally known formats |
| 3655 | */ |
| 3656 | static ssize_t |
| 3657 | metadata_show(mddev_t *mddev, char *page) |
| 3658 | { |
| 3659 | if (mddev->persistent) |
| 3660 | return sprintf(page, "%d.%d\n", |
| 3661 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3662 | else if (mddev->external) |
| 3663 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3664 | else |
| 3665 | return sprintf(page, "none\n"); |
| 3666 | } |
| 3667 | |
| 3668 | static ssize_t |
| 3669 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3670 | { |
| 3671 | int major, minor; |
| 3672 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3673 | /* Changing the details of 'external' metadata is |
| 3674 | * always permitted. Otherwise there must be |
| 3675 | * no devices attached to the array. |
| 3676 | */ |
| 3677 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3678 | ; |
| 3679 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3680 | return -EBUSY; |
| 3681 | |
| 3682 | if (cmd_match(buf, "none")) { |
| 3683 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3684 | mddev->external = 0; |
| 3685 | mddev->major_version = 0; |
| 3686 | mddev->minor_version = 90; |
| 3687 | return len; |
| 3688 | } |
| 3689 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3690 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3691 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3692 | namelen = sizeof(mddev->metadata_type)-1; |
| 3693 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3694 | mddev->metadata_type[namelen] = 0; |
| 3695 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3696 | mddev->metadata_type[--namelen] = 0; |
| 3697 | mddev->persistent = 0; |
| 3698 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3699 | mddev->major_version = 0; |
| 3700 | mddev->minor_version = 90; |
| 3701 | return len; |
| 3702 | } |
| 3703 | major = simple_strtoul(buf, &e, 10); |
| 3704 | if (e==buf || *e != '.') |
| 3705 | return -EINVAL; |
| 3706 | buf = e+1; |
| 3707 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3708 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3709 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3710 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3711 | return -ENOENT; |
| 3712 | mddev->major_version = major; |
| 3713 | mddev->minor_version = minor; |
| 3714 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3715 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3716 | return len; |
| 3717 | } |
| 3718 | |
| 3719 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3720 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3721 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3722 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3723 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3724 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3725 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3726 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3727 | type = "frozen"; |
| 3728 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3729 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3730 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3731 | type = "reshape"; |
| 3732 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3733 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3734 | type = "resync"; |
| 3735 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3736 | type = "check"; |
| 3737 | else |
| 3738 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3739 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3740 | type = "recover"; |
| 3741 | } |
| 3742 | return sprintf(page, "%s\n", type); |
| 3743 | } |
| 3744 | |
| 3745 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3746 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3747 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3748 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3749 | return -EINVAL; |
| 3750 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3751 | if (cmd_match(page, "frozen")) |
| 3752 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3753 | else |
| 3754 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3755 | |
| 3756 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3757 | if (mddev->sync_thread) { |
| 3758 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3759 | md_unregister_thread(mddev->sync_thread); |
| 3760 | mddev->sync_thread = NULL; |
| 3761 | mddev->recovery = 0; |
| 3762 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3763 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3764 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3765 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3766 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3767 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3768 | else if (cmd_match(page, "recover")) { |
| 3769 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3770 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3771 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3772 | int err; |
| 3773 | if (mddev->pers->start_reshape == NULL) |
| 3774 | return -EINVAL; |
| 3775 | err = mddev->pers->start_reshape(mddev); |
| 3776 | if (err) |
| 3777 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3778 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3779 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3780 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3781 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3782 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3783 | return -EINVAL; |
| 3784 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3785 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3786 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3787 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3788 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3789 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3790 | return len; |
| 3791 | } |
| 3792 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3793 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3794 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3795 | { |
| 3796 | return sprintf(page, "%llu\n", |
| 3797 | (unsigned long long) mddev->resync_mismatches); |
| 3798 | } |
| 3799 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3800 | static struct md_sysfs_entry md_scan_mode = |
| 3801 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3802 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3803 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3804 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3805 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3806 | static ssize_t |
| 3807 | sync_min_show(mddev_t *mddev, char *page) |
| 3808 | { |
| 3809 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3810 | mddev->sync_speed_min ? "local": "system"); |
| 3811 | } |
| 3812 | |
| 3813 | static ssize_t |
| 3814 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3815 | { |
| 3816 | int min; |
| 3817 | char *e; |
| 3818 | if (strncmp(buf, "system", 6)==0) { |
| 3819 | mddev->sync_speed_min = 0; |
| 3820 | return len; |
| 3821 | } |
| 3822 | min = simple_strtoul(buf, &e, 10); |
| 3823 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3824 | return -EINVAL; |
| 3825 | mddev->sync_speed_min = min; |
| 3826 | return len; |
| 3827 | } |
| 3828 | |
| 3829 | static struct md_sysfs_entry md_sync_min = |
| 3830 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3831 | |
| 3832 | static ssize_t |
| 3833 | sync_max_show(mddev_t *mddev, char *page) |
| 3834 | { |
| 3835 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3836 | mddev->sync_speed_max ? "local": "system"); |
| 3837 | } |
| 3838 | |
| 3839 | static ssize_t |
| 3840 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3841 | { |
| 3842 | int max; |
| 3843 | char *e; |
| 3844 | if (strncmp(buf, "system", 6)==0) { |
| 3845 | mddev->sync_speed_max = 0; |
| 3846 | return len; |
| 3847 | } |
| 3848 | max = simple_strtoul(buf, &e, 10); |
| 3849 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3850 | return -EINVAL; |
| 3851 | mddev->sync_speed_max = max; |
| 3852 | return len; |
| 3853 | } |
| 3854 | |
| 3855 | static struct md_sysfs_entry md_sync_max = |
| 3856 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3857 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3858 | static ssize_t |
| 3859 | degraded_show(mddev_t *mddev, char *page) |
| 3860 | { |
| 3861 | return sprintf(page, "%d\n", mddev->degraded); |
| 3862 | } |
| 3863 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3864 | |
| 3865 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3866 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3867 | { |
| 3868 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3869 | } |
| 3870 | |
| 3871 | static ssize_t |
| 3872 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3873 | { |
| 3874 | long n; |
| 3875 | |
| 3876 | if (strict_strtol(buf, 10, &n)) |
| 3877 | return -EINVAL; |
| 3878 | |
| 3879 | if (n != 0 && n != 1) |
| 3880 | return -EINVAL; |
| 3881 | |
| 3882 | mddev->parallel_resync = n; |
| 3883 | |
| 3884 | if (mddev->sync_thread) |
| 3885 | wake_up(&resync_wait); |
| 3886 | |
| 3887 | return len; |
| 3888 | } |
| 3889 | |
| 3890 | /* force parallel resync, even with shared block devices */ |
| 3891 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3892 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3893 | sync_force_parallel_show, sync_force_parallel_store); |
| 3894 | |
| 3895 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3896 | sync_speed_show(mddev_t *mddev, char *page) |
| 3897 | { |
| 3898 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3899 | if (mddev->curr_resync == 0) |
| 3900 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3901 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3902 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3903 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3904 | db = resync - mddev->resync_mark_cnt; |
| 3905 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3906 | } |
| 3907 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3908 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3909 | |
| 3910 | static ssize_t |
| 3911 | sync_completed_show(mddev_t *mddev, char *page) |
| 3912 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3913 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3914 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3915 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3916 | return sprintf(page, "none\n"); |
| 3917 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3918 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3919 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3920 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3921 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3922 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3923 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3924 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3925 | } |
| 3926 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3927 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3928 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3929 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3930 | min_sync_show(mddev_t *mddev, char *page) |
| 3931 | { |
| 3932 | return sprintf(page, "%llu\n", |
| 3933 | (unsigned long long)mddev->resync_min); |
| 3934 | } |
| 3935 | static ssize_t |
| 3936 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3937 | { |
| 3938 | unsigned long long min; |
| 3939 | if (strict_strtoull(buf, 10, &min)) |
| 3940 | return -EINVAL; |
| 3941 | if (min > mddev->resync_max) |
| 3942 | return -EINVAL; |
| 3943 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3944 | return -EBUSY; |
| 3945 | |
| 3946 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3947 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3948 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3949 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3950 | return -EINVAL; |
| 3951 | } |
| 3952 | mddev->resync_min = min; |
| 3953 | |
| 3954 | return len; |
| 3955 | } |
| 3956 | |
| 3957 | static struct md_sysfs_entry md_min_sync = |
| 3958 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3959 | |
| 3960 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3961 | max_sync_show(mddev_t *mddev, char *page) |
| 3962 | { |
| 3963 | if (mddev->resync_max == MaxSector) |
| 3964 | return sprintf(page, "max\n"); |
| 3965 | else |
| 3966 | return sprintf(page, "%llu\n", |
| 3967 | (unsigned long long)mddev->resync_max); |
| 3968 | } |
| 3969 | static ssize_t |
| 3970 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3971 | { |
| 3972 | if (strncmp(buf, "max", 3) == 0) |
| 3973 | mddev->resync_max = MaxSector; |
| 3974 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3975 | unsigned long long max; |
| 3976 | if (strict_strtoull(buf, 10, &max)) |
| 3977 | return -EINVAL; |
| 3978 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3979 | return -EINVAL; |
| 3980 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3981 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3982 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3983 | return -EBUSY; |
| 3984 | |
| 3985 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3986 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3987 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3988 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3989 | return -EINVAL; |
| 3990 | } |
| 3991 | mddev->resync_max = max; |
| 3992 | } |
| 3993 | wake_up(&mddev->recovery_wait); |
| 3994 | return len; |
| 3995 | } |
| 3996 | |
| 3997 | static struct md_sysfs_entry md_max_sync = |
| 3998 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3999 | |
| 4000 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4001 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4002 | { |
| 4003 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4004 | } |
| 4005 | |
| 4006 | static ssize_t |
| 4007 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4008 | { |
| 4009 | char *e; |
| 4010 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4011 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4012 | if (mddev->pers == NULL || |
| 4013 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4014 | return -EINVAL; |
| 4015 | if (buf == e || (*e && *e != '\n')) |
| 4016 | return -EINVAL; |
| 4017 | if (new >= mddev->suspend_hi || |
| 4018 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 4019 | mddev->suspend_lo = new; |
| 4020 | mddev->pers->quiesce(mddev, 2); |
| 4021 | return len; |
| 4022 | } else |
| 4023 | return -EINVAL; |
| 4024 | } |
| 4025 | static struct md_sysfs_entry md_suspend_lo = |
| 4026 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4027 | |
| 4028 | |
| 4029 | static ssize_t |
| 4030 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4031 | { |
| 4032 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4033 | } |
| 4034 | |
| 4035 | static ssize_t |
| 4036 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4037 | { |
| 4038 | char *e; |
| 4039 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4040 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4041 | if (mddev->pers == NULL || |
| 4042 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4043 | return -EINVAL; |
| 4044 | if (buf == e || (*e && *e != '\n')) |
| 4045 | return -EINVAL; |
| 4046 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 4047 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 4048 | mddev->suspend_hi = new; |
| 4049 | mddev->pers->quiesce(mddev, 1); |
| 4050 | mddev->pers->quiesce(mddev, 0); |
| 4051 | return len; |
| 4052 | } else |
| 4053 | return -EINVAL; |
| 4054 | } |
| 4055 | static struct md_sysfs_entry md_suspend_hi = |
| 4056 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4057 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4058 | static ssize_t |
| 4059 | reshape_position_show(mddev_t *mddev, char *page) |
| 4060 | { |
| 4061 | if (mddev->reshape_position != MaxSector) |
| 4062 | return sprintf(page, "%llu\n", |
| 4063 | (unsigned long long)mddev->reshape_position); |
| 4064 | strcpy(page, "none\n"); |
| 4065 | return 5; |
| 4066 | } |
| 4067 | |
| 4068 | static ssize_t |
| 4069 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4070 | { |
| 4071 | char *e; |
| 4072 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4073 | if (mddev->pers) |
| 4074 | return -EBUSY; |
| 4075 | if (buf == e || (*e && *e != '\n')) |
| 4076 | return -EINVAL; |
| 4077 | mddev->reshape_position = new; |
| 4078 | mddev->delta_disks = 0; |
| 4079 | mddev->new_level = mddev->level; |
| 4080 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4081 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4082 | return len; |
| 4083 | } |
| 4084 | |
| 4085 | static struct md_sysfs_entry md_reshape_position = |
| 4086 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4087 | reshape_position_store); |
| 4088 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4089 | static ssize_t |
| 4090 | array_size_show(mddev_t *mddev, char *page) |
| 4091 | { |
| 4092 | if (mddev->external_size) |
| 4093 | return sprintf(page, "%llu\n", |
| 4094 | (unsigned long long)mddev->array_sectors/2); |
| 4095 | else |
| 4096 | return sprintf(page, "default\n"); |
| 4097 | } |
| 4098 | |
| 4099 | static ssize_t |
| 4100 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4101 | { |
| 4102 | sector_t sectors; |
| 4103 | |
| 4104 | if (strncmp(buf, "default", 7) == 0) { |
| 4105 | if (mddev->pers) |
| 4106 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4107 | else |
| 4108 | sectors = mddev->array_sectors; |
| 4109 | |
| 4110 | mddev->external_size = 0; |
| 4111 | } else { |
| 4112 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4113 | return -EINVAL; |
| 4114 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4115 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4116 | |
| 4117 | mddev->external_size = 1; |
| 4118 | } |
| 4119 | |
| 4120 | mddev->array_sectors = sectors; |
| 4121 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4122 | if (mddev->pers) |
| 4123 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4124 | |
| 4125 | return len; |
| 4126 | } |
| 4127 | |
| 4128 | static struct md_sysfs_entry md_array_size = |
| 4129 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4130 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4131 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4132 | static struct attribute *md_default_attrs[] = { |
| 4133 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4134 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4135 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4136 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4137 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4138 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4139 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4140 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4141 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4142 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4143 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4144 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4145 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4146 | NULL, |
| 4147 | }; |
| 4148 | |
| 4149 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4150 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4151 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4152 | &md_sync_min.attr, |
| 4153 | &md_sync_max.attr, |
| 4154 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4155 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4156 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4157 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4158 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4159 | &md_suspend_lo.attr, |
| 4160 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4161 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4162 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4163 | NULL, |
| 4164 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4165 | static struct attribute_group md_redundancy_group = { |
| 4166 | .name = NULL, |
| 4167 | .attrs = md_redundancy_attrs, |
| 4168 | }; |
| 4169 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4170 | |
| 4171 | static ssize_t |
| 4172 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4173 | { |
| 4174 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4175 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4176 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4177 | |
| 4178 | if (!entry->show) |
| 4179 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4180 | rv = mddev_lock(mddev); |
| 4181 | if (!rv) { |
| 4182 | rv = entry->show(mddev, page); |
| 4183 | mddev_unlock(mddev); |
| 4184 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4185 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | static ssize_t |
| 4189 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4190 | const char *page, size_t length) |
| 4191 | { |
| 4192 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4193 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4194 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4195 | |
| 4196 | if (!entry->store) |
| 4197 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4198 | if (!capable(CAP_SYS_ADMIN)) |
| 4199 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4200 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4201 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4202 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4203 | if (!rv) { |
| 4204 | rv = entry->store(mddev, page, length); |
| 4205 | mddev_unlock(mddev); |
| 4206 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4207 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | static void md_free(struct kobject *ko) |
| 4211 | { |
| 4212 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4213 | |
| 4214 | if (mddev->sysfs_state) |
| 4215 | sysfs_put(mddev->sysfs_state); |
| 4216 | |
| 4217 | if (mddev->gendisk) { |
| 4218 | del_gendisk(mddev->gendisk); |
| 4219 | put_disk(mddev->gendisk); |
| 4220 | } |
| 4221 | if (mddev->queue) |
| 4222 | blk_cleanup_queue(mddev->queue); |
| 4223 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4224 | kfree(mddev); |
| 4225 | } |
| 4226 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4227 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4228 | .show = md_attr_show, |
| 4229 | .store = md_attr_store, |
| 4230 | }; |
| 4231 | static struct kobj_type md_ktype = { |
| 4232 | .release = md_free, |
| 4233 | .sysfs_ops = &md_sysfs_ops, |
| 4234 | .default_attrs = md_default_attrs, |
| 4235 | }; |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | int mdp_major = 0; |
| 4238 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4239 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4240 | { |
| 4241 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4242 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4243 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4244 | kobject_del(&mddev->kobj); |
| 4245 | kobject_put(&mddev->kobj); |
| 4246 | } |
| 4247 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4248 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4250 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | mddev_t *mddev = mddev_find(dev); |
| 4252 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4253 | int partitioned; |
| 4254 | int shift; |
| 4255 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4256 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
| 4258 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4259 | return -ENODEV; |
| 4260 | |
| 4261 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4262 | shift = partitioned ? MdpMinorShift : 0; |
| 4263 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4265 | /* wait for any previous instance of this device to be |
| 4266 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4267 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4268 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4269 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4270 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4271 | error = -EEXIST; |
| 4272 | if (mddev->gendisk) |
| 4273 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4274 | |
| 4275 | if (name) { |
| 4276 | /* Need to ensure that 'name' is not a duplicate. |
| 4277 | */ |
| 4278 | mddev_t *mddev2; |
| 4279 | spin_lock(&all_mddevs_lock); |
| 4280 | |
| 4281 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4282 | if (mddev2->gendisk && |
| 4283 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4284 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4285 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4286 | } |
| 4287 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4289 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4290 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4291 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4292 | if (!mddev->queue) |
| 4293 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4294 | mddev->queue->queuedata = mddev; |
| 4295 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4296 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4297 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4298 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4299 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | disk = alloc_disk(1 << shift); |
| 4302 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4303 | blk_cleanup_queue(mddev->queue); |
| 4304 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4305 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4307 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4309 | if (name) |
| 4310 | strcpy(disk->disk_name, name); |
| 4311 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4313 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | disk->fops = &md_fops; |
| 4316 | disk->private_data = mddev; |
| 4317 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4318 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4319 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4320 | * remove it now. |
| 4321 | */ |
| 4322 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | add_disk(disk); |
| 4324 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4325 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4326 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4327 | if (error) { |
| 4328 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4329 | * __must_check, we must do something with the result |
| 4330 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4331 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4332 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4333 | error = 0; |
| 4334 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4335 | if (mddev->kobj.sd && |
| 4336 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4337 | printk(KERN_DEBUG "pointless warning\n"); |
Darrick J. Wong | be20e6c | 2010-11-24 16:40:33 +1100 | [diff] [blame] | 4338 | |
| 4339 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4340 | abort: |
| 4341 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4342 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4343 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4344 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4345 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4346 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4347 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4351 | { |
| 4352 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | return NULL; |
| 4354 | } |
| 4355 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4356 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4357 | { |
| 4358 | /* val must be "md_*" where * is not all digits. |
| 4359 | * We allocate an array with a large free minor number, and |
| 4360 | * set the name to val. val must not already be an active name. |
| 4361 | */ |
| 4362 | int len = strlen(val); |
| 4363 | char buf[DISK_NAME_LEN]; |
| 4364 | |
| 4365 | while (len && val[len-1] == '\n') |
| 4366 | len--; |
| 4367 | if (len >= DISK_NAME_LEN) |
| 4368 | return -E2BIG; |
| 4369 | strlcpy(buf, val, len+1); |
| 4370 | if (strncmp(buf, "md_", 3) != 0) |
| 4371 | return -EINVAL; |
| 4372 | return md_alloc(0, buf); |
| 4373 | } |
| 4374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | static void md_safemode_timeout(unsigned long data) |
| 4376 | { |
| 4377 | mddev_t *mddev = (mddev_t *) data; |
| 4378 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4379 | if (!atomic_read(&mddev->writes_pending)) { |
| 4380 | mddev->safemode = 1; |
| 4381 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4382 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | md_wakeup_thread(mddev->thread); |
| 4385 | } |
| 4386 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4387 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4389 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4391 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4393 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4395 | if (list_empty(&mddev->disks)) |
| 4396 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | |
| 4399 | if (mddev->pers) |
| 4400 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4401 | /* Cannot run until previous stop completes properly */ |
| 4402 | if (mddev->sysfs_active) |
| 4403 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | /* |
| 4406 | * Analyze all RAID superblock(s) |
| 4407 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4408 | if (!mddev->raid_disks) { |
| 4409 | if (!mddev->persistent) |
| 4410 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4411 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4414 | if (mddev->level != LEVEL_NONE) |
| 4415 | request_module("md-level-%d", mddev->level); |
| 4416 | else if (mddev->clevel[0]) |
| 4417 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * Drop all container device buffers, from now on |
| 4421 | * the only valid external interface is through the md |
| 4422 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4424 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4425 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | continue; |
| 4427 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4428 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4429 | |
| 4430 | /* perform some consistency tests on the device. |
| 4431 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4432 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4433 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4434 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4435 | if (mddev->dev_sectors && |
| 4436 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4437 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4438 | printk("md: %s: data overlaps metadata\n", |
| 4439 | mdname(mddev)); |
| 4440 | return -EINVAL; |
| 4441 | } |
| 4442 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4443 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4444 | > rdev->data_offset) { |
| 4445 | printk("md: %s: metadata overlaps data\n", |
| 4446 | mdname(mddev)); |
| 4447 | return -EINVAL; |
| 4448 | } |
| 4449 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4450 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | } |
| 4452 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4453 | if (mddev->bio_set == NULL) |
| 4454 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4457 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4458 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4460 | if (mddev->level != LEVEL_NONE) |
| 4461 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4462 | mddev->level); |
| 4463 | else |
| 4464 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4465 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | return -EINVAL; |
| 4467 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4468 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4470 | if (mddev->level != pers->level) { |
| 4471 | mddev->level = pers->level; |
| 4472 | mddev->new_level = pers->level; |
| 4473 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4474 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4476 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4477 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4478 | /* This personality cannot handle reshaping... */ |
| 4479 | mddev->pers = NULL; |
| 4480 | module_put(pers->owner); |
| 4481 | return -EINVAL; |
| 4482 | } |
| 4483 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4484 | if (pers->sync_request) { |
| 4485 | /* Warn if this is a potentially silly |
| 4486 | * configuration. |
| 4487 | */ |
| 4488 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4489 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4490 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4491 | |
| 4492 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4493 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4494 | if (rdev < rdev2 && |
| 4495 | rdev->bdev->bd_contains == |
| 4496 | rdev2->bdev->bd_contains) { |
| 4497 | printk(KERN_WARNING |
| 4498 | "%s: WARNING: %s appears to be" |
| 4499 | " on the same physical disk as" |
| 4500 | " %s.\n", |
| 4501 | mdname(mddev), |
| 4502 | bdevname(rdev->bdev,b), |
| 4503 | bdevname(rdev2->bdev,b2)); |
| 4504 | warned = 1; |
| 4505 | } |
| 4506 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4507 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4508 | if (warned) |
| 4509 | printk(KERN_WARNING |
| 4510 | "True protection against single-disk" |
| 4511 | " failure might be compromised.\n"); |
| 4512 | } |
| 4513 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4514 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4515 | /* may be over-ridden by personality */ |
| 4516 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4517 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4518 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4520 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4521 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4522 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4523 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4524 | if (err) |
| 4525 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4526 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4527 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4528 | " but 'external_size' not in effect?\n", __func__); |
| 4529 | printk(KERN_ERR |
| 4530 | "md: invalid array_size %llu > default size %llu\n", |
| 4531 | (unsigned long long)mddev->array_sectors / 2, |
| 4532 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4533 | err = -EINVAL; |
| 4534 | mddev->pers->stop(mddev); |
| 4535 | } |
| 4536 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4537 | err = bitmap_create(mddev); |
| 4538 | if (err) { |
| 4539 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4540 | mdname(mddev), err); |
| 4541 | mddev->pers->stop(mddev); |
| 4542 | } |
| 4543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | module_put(mddev->pers->owner); |
| 4546 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4547 | bitmap_destroy(mddev); |
| 4548 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4550 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4551 | if (mddev->kobj.sd && |
| 4552 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4553 | printk(KERN_WARNING |
| 4554 | "md: cannot register extra attributes for %s\n", |
| 4555 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4556 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4557 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4558 | mddev->ro = 0; |
| 4559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4561 | atomic_set(&mddev->max_corr_read_errors, |
| 4562 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | mddev->safemode = 0; |
| 4564 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4565 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4566 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4568 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4569 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4570 | if (rdev->raid_disk >= 0) { |
| 4571 | char nm[20]; |
| 4572 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4573 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4574 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | |
| 4577 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4578 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4579 | if (mddev->flags) |
| 4580 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4582 | md_wakeup_thread(mddev->thread); |
| 4583 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4584 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4585 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4586 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4587 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4588 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | return 0; |
| 4590 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4591 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4593 | static int do_md_run(mddev_t *mddev) |
| 4594 | { |
| 4595 | int err; |
| 4596 | |
| 4597 | err = md_run(mddev); |
| 4598 | if (err) |
| 4599 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4600 | err = bitmap_load(mddev); |
| 4601 | if (err) { |
| 4602 | bitmap_destroy(mddev); |
| 4603 | goto out; |
| 4604 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4605 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4606 | revalidate_disk(mddev->gendisk); |
| 4607 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4608 | out: |
| 4609 | return err; |
| 4610 | } |
| 4611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | static int restart_array(mddev_t *mddev) |
| 4613 | { |
| 4614 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4616 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4618 | return -ENXIO; |
| 4619 | if (!mddev->pers) |
| 4620 | return -EINVAL; |
| 4621 | if (!mddev->ro) |
| 4622 | return -EBUSY; |
| 4623 | mddev->safemode = 0; |
| 4624 | mddev->ro = 0; |
| 4625 | set_disk_ro(disk, 0); |
| 4626 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4627 | mdname(mddev)); |
| 4628 | /* Kick recovery or resync if necessary */ |
| 4629 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4630 | md_wakeup_thread(mddev->thread); |
| 4631 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4632 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4633 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | } |
| 4635 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4636 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4637 | * to the file ourselves */ |
| 4638 | static int deny_bitmap_write_access(struct file * file) |
| 4639 | { |
| 4640 | struct inode *inode = file->f_mapping->host; |
| 4641 | |
| 4642 | spin_lock(&inode->i_lock); |
| 4643 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4644 | spin_unlock(&inode->i_lock); |
| 4645 | return -ETXTBSY; |
| 4646 | } |
| 4647 | atomic_set(&inode->i_writecount, -1); |
| 4648 | spin_unlock(&inode->i_lock); |
| 4649 | |
| 4650 | return 0; |
| 4651 | } |
| 4652 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4653 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4654 | { |
| 4655 | struct inode *inode = file->f_mapping->host; |
| 4656 | |
| 4657 | spin_lock(&inode->i_lock); |
| 4658 | atomic_set(&inode->i_writecount, 1); |
| 4659 | spin_unlock(&inode->i_lock); |
| 4660 | } |
| 4661 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4662 | static void md_clean(mddev_t *mddev) |
| 4663 | { |
| 4664 | mddev->array_sectors = 0; |
| 4665 | mddev->external_size = 0; |
| 4666 | mddev->dev_sectors = 0; |
| 4667 | mddev->raid_disks = 0; |
| 4668 | mddev->recovery_cp = 0; |
| 4669 | mddev->resync_min = 0; |
| 4670 | mddev->resync_max = MaxSector; |
| 4671 | mddev->reshape_position = MaxSector; |
| 4672 | mddev->external = 0; |
| 4673 | mddev->persistent = 0; |
| 4674 | mddev->level = LEVEL_NONE; |
| 4675 | mddev->clevel[0] = 0; |
| 4676 | mddev->flags = 0; |
| 4677 | mddev->ro = 0; |
| 4678 | mddev->metadata_type[0] = 0; |
| 4679 | mddev->chunk_sectors = 0; |
| 4680 | mddev->ctime = mddev->utime = 0; |
| 4681 | mddev->layout = 0; |
| 4682 | mddev->max_disks = 0; |
| 4683 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4684 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4685 | mddev->delta_disks = 0; |
| 4686 | mddev->new_level = LEVEL_NONE; |
| 4687 | mddev->new_layout = 0; |
| 4688 | mddev->new_chunk_sectors = 0; |
| 4689 | mddev->curr_resync = 0; |
| 4690 | mddev->resync_mismatches = 0; |
| 4691 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4692 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4693 | mddev->recovery = 0; |
| 4694 | mddev->in_sync = 0; |
| 4695 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4696 | mddev->safemode = 0; |
| 4697 | mddev->bitmap_info.offset = 0; |
| 4698 | mddev->bitmap_info.default_offset = 0; |
| 4699 | mddev->bitmap_info.chunksize = 0; |
| 4700 | mddev->bitmap_info.daemon_sleep = 0; |
| 4701 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4702 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4703 | } |
| 4704 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4705 | void md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4706 | { |
| 4707 | if (mddev->sync_thread) { |
| 4708 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4709 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4710 | md_unregister_thread(mddev->sync_thread); |
| 4711 | mddev->sync_thread = NULL; |
| 4712 | } |
| 4713 | |
| 4714 | del_timer_sync(&mddev->safemode_timer); |
| 4715 | |
| 4716 | bitmap_flush(mddev); |
| 4717 | md_super_wait(mddev); |
| 4718 | |
| 4719 | if (!mddev->in_sync || mddev->flags) { |
| 4720 | /* mark array as shutdown cleanly */ |
| 4721 | mddev->in_sync = 1; |
| 4722 | md_update_sb(mddev, 1); |
| 4723 | } |
| 4724 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4725 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4726 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4727 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4728 | { |
| 4729 | mddev->pers->stop(mddev); |
| 4730 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4731 | mddev->to_remove = &md_redundancy_group; |
| 4732 | module_put(mddev->pers->owner); |
| 4733 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4734 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4735 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4736 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4737 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4738 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4739 | { |
| 4740 | int err = 0; |
| 4741 | mutex_lock(&mddev->open_mutex); |
| 4742 | if (atomic_read(&mddev->openers) > is_open) { |
| 4743 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4744 | err = -EBUSY; |
| 4745 | goto out; |
| 4746 | } |
| 4747 | if (mddev->pers) { |
| 4748 | md_stop_writes(mddev); |
| 4749 | |
| 4750 | err = -ENXIO; |
| 4751 | if (mddev->ro==1) |
| 4752 | goto out; |
| 4753 | mddev->ro = 1; |
| 4754 | set_disk_ro(mddev->gendisk, 1); |
| 4755 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4756 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4757 | err = 0; |
| 4758 | } |
| 4759 | out: |
| 4760 | mutex_unlock(&mddev->open_mutex); |
| 4761 | return err; |
| 4762 | } |
| 4763 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4764 | /* mode: |
| 4765 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4766 | * 2 - stop but do not disassemble array |
| 4767 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4768 | 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] | 4769 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4771 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4773 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4774 | if (atomic_read(&mddev->openers) > is_open || |
| 4775 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4776 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4777 | mutex_unlock(&mddev->open_mutex); |
| 4778 | return -EBUSY; |
| 4779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4781 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4782 | if (mddev->ro) |
| 4783 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4784 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4785 | md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4786 | md_stop(mddev); |
| 4787 | mddev->queue->merge_bvec_fn = NULL; |
| 4788 | mddev->queue->unplug_fn = NULL; |
| 4789 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4790 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4791 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4792 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4793 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4794 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4795 | if (rdev->raid_disk >= 0) { |
| 4796 | char nm[20]; |
| 4797 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4798 | sysfs_remove_link(&mddev->kobj, nm); |
| 4799 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4800 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4801 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4802 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4803 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4804 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4805 | if (mddev->ro) |
| 4806 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4807 | } else |
| 4808 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | /* |
| 4810 | * Free resources if final stop |
| 4811 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4812 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4814 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4815 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4816 | if (mddev->bitmap_info.file) { |
| 4817 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4818 | fput(mddev->bitmap_info.file); |
| 4819 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4820 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4821 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | export_array(mddev); |
| 4824 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4825 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4826 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4827 | if (mddev->hold_active == UNTIL_STOP) |
| 4828 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4829 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4830 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4831 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4832 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4833 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4836 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | static void autorun_array(mddev_t *mddev) |
| 4838 | { |
| 4839 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | int err; |
| 4841 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4842 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | |
| 4845 | printk(KERN_INFO "md: running: "); |
| 4846 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4847 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | char b[BDEVNAME_SIZE]; |
| 4849 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4850 | } |
| 4851 | printk("\n"); |
| 4852 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4853 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | if (err) { |
| 4855 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4856 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | /* |
| 4861 | * lets try to run arrays based on all disks that have arrived |
| 4862 | * until now. (those are in pending_raid_disks) |
| 4863 | * |
| 4864 | * the method: pick the first pending disk, collect all disks with |
| 4865 | * the same UUID, remove all from the pending list and put them into |
| 4866 | * the 'same_array' list. Then order this list based on superblock |
| 4867 | * update time (freshest comes first), kick out 'old' disks and |
| 4868 | * compare superblocks. If everything's fine then run it. |
| 4869 | * |
| 4870 | * If "unit" is allocated, then bump its reference count |
| 4871 | */ |
| 4872 | static void autorun_devices(int part) |
| 4873 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4874 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | mddev_t *mddev; |
| 4876 | char b[BDEVNAME_SIZE]; |
| 4877 | |
| 4878 | printk(KERN_INFO "md: autorun ...\n"); |
| 4879 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4880 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4882 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | rdev0 = list_entry(pending_raid_disks.next, |
| 4884 | mdk_rdev_t, same_set); |
| 4885 | |
| 4886 | printk(KERN_INFO "md: considering %s ...\n", |
| 4887 | bdevname(rdev0->bdev,b)); |
| 4888 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4889 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4891 | printk(KERN_INFO "md: adding %s ...\n", |
| 4892 | bdevname(rdev->bdev,b)); |
| 4893 | list_move(&rdev->same_set, &candidates); |
| 4894 | } |
| 4895 | /* |
| 4896 | * now we have a set of devices, with all of them having |
| 4897 | * mostly sane superblocks. It's time to allocate the |
| 4898 | * mddev. |
| 4899 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4900 | if (part) { |
| 4901 | dev = MKDEV(mdp_major, |
| 4902 | rdev0->preferred_minor << MdpMinorShift); |
| 4903 | unit = MINOR(dev) >> MdpMinorShift; |
| 4904 | } else { |
| 4905 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4906 | unit = MINOR(dev); |
| 4907 | } |
| 4908 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4910 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4911 | break; |
| 4912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | |
| 4914 | md_probe(dev, NULL, NULL); |
| 4915 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4916 | if (!mddev || !mddev->gendisk) { |
| 4917 | if (mddev) |
| 4918 | mddev_put(mddev); |
| 4919 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | "md: cannot allocate memory for md drive.\n"); |
| 4921 | break; |
| 4922 | } |
| 4923 | if (mddev_lock(mddev)) |
| 4924 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4925 | mdname(mddev)); |
| 4926 | else if (mddev->raid_disks || mddev->major_version |
| 4927 | || !list_empty(&mddev->disks)) { |
| 4928 | printk(KERN_WARNING |
| 4929 | "md: %s already running, cannot run %s\n", |
| 4930 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4931 | mddev_unlock(mddev); |
| 4932 | } else { |
| 4933 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4934 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4935 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | list_del_init(&rdev->same_set); |
| 4937 | if (bind_rdev_to_array(rdev, mddev)) |
| 4938 | export_rdev(rdev); |
| 4939 | } |
| 4940 | autorun_array(mddev); |
| 4941 | mddev_unlock(mddev); |
| 4942 | } |
| 4943 | /* on success, candidates will be empty, on error |
| 4944 | * it won't... |
| 4945 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4946 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4947 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | mddev_put(mddev); |
| 4951 | } |
| 4952 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4953 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4954 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | static int get_version(void __user * arg) |
| 4957 | { |
| 4958 | mdu_version_t ver; |
| 4959 | |
| 4960 | ver.major = MD_MAJOR_VERSION; |
| 4961 | ver.minor = MD_MINOR_VERSION; |
| 4962 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4963 | |
| 4964 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4965 | return -EFAULT; |
| 4966 | |
| 4967 | return 0; |
| 4968 | } |
| 4969 | |
| 4970 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4971 | { |
| 4972 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4973 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4976 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4977 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4979 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | failed++; |
| 4981 | else { |
| 4982 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4983 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4984 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | else |
| 4986 | spare++; |
| 4987 | } |
| 4988 | } |
| 4989 | |
| 4990 | info.major_version = mddev->major_version; |
| 4991 | info.minor_version = mddev->minor_version; |
| 4992 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4993 | info.ctime = mddev->ctime; |
| 4994 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4995 | info.size = mddev->dev_sectors / 2; |
| 4996 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4997 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | info.nr_disks = nr; |
| 4999 | info.raid_disks = mddev->raid_disks; |
| 5000 | info.md_minor = mddev->md_minor; |
| 5001 | info.not_persistent= !mddev->persistent; |
| 5002 | |
| 5003 | info.utime = mddev->utime; |
| 5004 | info.state = 0; |
| 5005 | if (mddev->in_sync) |
| 5006 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5007 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5008 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5009 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | info.working_disks = working; |
| 5011 | info.failed_disks = failed; |
| 5012 | info.spare_disks = spare; |
| 5013 | |
| 5014 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5015 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | |
| 5017 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5018 | return -EFAULT; |
| 5019 | |
| 5020 | return 0; |
| 5021 | } |
| 5022 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5023 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5024 | { |
| 5025 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5026 | char *ptr, *buf = NULL; |
| 5027 | int err = -ENOMEM; |
| 5028 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5029 | if (md_allow_write(mddev)) |
| 5030 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5031 | else |
| 5032 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5033 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5034 | if (!file) |
| 5035 | goto out; |
| 5036 | |
| 5037 | /* bitmap disabled, zero the first byte and copy out */ |
| 5038 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5039 | file->pathname[0] = '\0'; |
| 5040 | goto copy_out; |
| 5041 | } |
| 5042 | |
| 5043 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5044 | if (!buf) |
| 5045 | goto out; |
| 5046 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5047 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5048 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5049 | goto out; |
| 5050 | |
| 5051 | strcpy(file->pathname, ptr); |
| 5052 | |
| 5053 | copy_out: |
| 5054 | err = 0; |
| 5055 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5056 | err = -EFAULT; |
| 5057 | out: |
| 5058 | kfree(buf); |
| 5059 | kfree(file); |
| 5060 | return err; |
| 5061 | } |
| 5062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5064 | { |
| 5065 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | mdk_rdev_t *rdev; |
| 5067 | |
| 5068 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5069 | return -EFAULT; |
| 5070 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5071 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | if (rdev) { |
| 5073 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5074 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5075 | info.raid_disk = rdev->raid_disk; |
| 5076 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5077 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5079 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5081 | info.state |= (1<<MD_DISK_SYNC); |
| 5082 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5083 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5084 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | } else { |
| 5086 | info.major = info.minor = 0; |
| 5087 | info.raid_disk = -1; |
| 5088 | info.state = (1<<MD_DISK_REMOVED); |
| 5089 | } |
| 5090 | |
| 5091 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5092 | return -EFAULT; |
| 5093 | |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
| 5097 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5098 | { |
| 5099 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5100 | mdk_rdev_t *rdev; |
| 5101 | dev_t dev = MKDEV(info->major,info->minor); |
| 5102 | |
| 5103 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5104 | return -EOVERFLOW; |
| 5105 | |
| 5106 | if (!mddev->raid_disks) { |
| 5107 | int err; |
| 5108 | /* expecting a device which has a superblock */ |
| 5109 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5110 | if (IS_ERR(rdev)) { |
| 5111 | printk(KERN_WARNING |
| 5112 | "md: md_import_device returned %ld\n", |
| 5113 | PTR_ERR(rdev)); |
| 5114 | return PTR_ERR(rdev); |
| 5115 | } |
| 5116 | if (!list_empty(&mddev->disks)) { |
| 5117 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5118 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5119 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5121 | if (err < 0) { |
| 5122 | printk(KERN_WARNING |
| 5123 | "md: %s has different UUID to %s\n", |
| 5124 | bdevname(rdev->bdev,b), |
| 5125 | bdevname(rdev0->bdev,b2)); |
| 5126 | export_rdev(rdev); |
| 5127 | return -EINVAL; |
| 5128 | } |
| 5129 | } |
| 5130 | err = bind_rdev_to_array(rdev, mddev); |
| 5131 | if (err) |
| 5132 | export_rdev(rdev); |
| 5133 | return err; |
| 5134 | } |
| 5135 | |
| 5136 | /* |
| 5137 | * add_new_disk can be used once the array is assembled |
| 5138 | * to add "hot spares". They must already have a superblock |
| 5139 | * written |
| 5140 | */ |
| 5141 | if (mddev->pers) { |
| 5142 | int err; |
| 5143 | if (!mddev->pers->hot_add_disk) { |
| 5144 | printk(KERN_WARNING |
| 5145 | "%s: personality does not support diskops!\n", |
| 5146 | mdname(mddev)); |
| 5147 | return -EINVAL; |
| 5148 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5149 | if (mddev->persistent) |
| 5150 | rdev = md_import_device(dev, mddev->major_version, |
| 5151 | mddev->minor_version); |
| 5152 | else |
| 5153 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | if (IS_ERR(rdev)) { |
| 5155 | printk(KERN_WARNING |
| 5156 | "md: md_import_device returned %ld\n", |
| 5157 | PTR_ERR(rdev)); |
| 5158 | return PTR_ERR(rdev); |
| 5159 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5160 | /* set save_raid_disk if appropriate */ |
| 5161 | if (!mddev->persistent) { |
| 5162 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5163 | info->raid_disk < mddev->raid_disks) |
| 5164 | rdev->raid_disk = info->raid_disk; |
| 5165 | else |
| 5166 | rdev->raid_disk = -1; |
| 5167 | } else |
| 5168 | super_types[mddev->major_version]. |
| 5169 | validate_super(mddev, rdev); |
| 5170 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5171 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5172 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5173 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5174 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5175 | else |
| 5176 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | rdev->raid_disk = -1; |
| 5179 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5180 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5181 | /* If there is hot_add_disk but no hot_remove_disk |
| 5182 | * then added disks for geometry changes, |
| 5183 | * and should be added immediately. |
| 5184 | */ |
| 5185 | super_types[mddev->major_version]. |
| 5186 | validate_super(mddev, rdev); |
| 5187 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5188 | if (err) |
| 5189 | unbind_rdev_from_array(rdev); |
| 5190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | if (err) |
| 5192 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5193 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5194 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5195 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5196 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5197 | if (mddev->degraded) |
| 5198 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5199 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5200 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | return err; |
| 5202 | } |
| 5203 | |
| 5204 | /* otherwise, add_new_disk is only allowed |
| 5205 | * for major_version==0 superblocks |
| 5206 | */ |
| 5207 | if (mddev->major_version != 0) { |
| 5208 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5209 | mdname(mddev)); |
| 5210 | return -EINVAL; |
| 5211 | } |
| 5212 | |
| 5213 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5214 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5215 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | if (IS_ERR(rdev)) { |
| 5217 | printk(KERN_WARNING |
| 5218 | "md: error, md_import_device() returned %ld\n", |
| 5219 | PTR_ERR(rdev)); |
| 5220 | return PTR_ERR(rdev); |
| 5221 | } |
| 5222 | rdev->desc_nr = info->number; |
| 5223 | if (info->raid_disk < mddev->raid_disks) |
| 5224 | rdev->raid_disk = info->raid_disk; |
| 5225 | else |
| 5226 | rdev->raid_disk = -1; |
| 5227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5229 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5230 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5232 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5233 | set_bit(WriteMostly, &rdev->flags); |
| 5234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | if (!mddev->persistent) { |
| 5236 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5237 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5238 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5239 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5240 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5242 | err = bind_rdev_to_array(rdev, mddev); |
| 5243 | if (err) { |
| 5244 | export_rdev(rdev); |
| 5245 | return err; |
| 5246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | } |
| 5248 | |
| 5249 | return 0; |
| 5250 | } |
| 5251 | |
| 5252 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5253 | { |
| 5254 | char b[BDEVNAME_SIZE]; |
| 5255 | mdk_rdev_t *rdev; |
| 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | rdev = find_rdev(mddev, dev); |
| 5258 | if (!rdev) |
| 5259 | return -ENXIO; |
| 5260 | |
| 5261 | if (rdev->raid_disk >= 0) |
| 5262 | goto busy; |
| 5263 | |
| 5264 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5265 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5266 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | |
| 5268 | return 0; |
| 5269 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5270 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5272 | return -EBUSY; |
| 5273 | } |
| 5274 | |
| 5275 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5276 | { |
| 5277 | char b[BDEVNAME_SIZE]; |
| 5278 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | mdk_rdev_t *rdev; |
| 5280 | |
| 5281 | if (!mddev->pers) |
| 5282 | return -ENODEV; |
| 5283 | |
| 5284 | if (mddev->major_version != 0) { |
| 5285 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5286 | " version-0 superblocks.\n", |
| 5287 | mdname(mddev)); |
| 5288 | return -EINVAL; |
| 5289 | } |
| 5290 | if (!mddev->pers->hot_add_disk) { |
| 5291 | printk(KERN_WARNING |
| 5292 | "%s: personality does not support diskops!\n", |
| 5293 | mdname(mddev)); |
| 5294 | return -EINVAL; |
| 5295 | } |
| 5296 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5297 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | if (IS_ERR(rdev)) { |
| 5299 | printk(KERN_WARNING |
| 5300 | "md: error, md_import_device() returned %ld\n", |
| 5301 | PTR_ERR(rdev)); |
| 5302 | return -EINVAL; |
| 5303 | } |
| 5304 | |
| 5305 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5306 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5308 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5310 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5312 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | printk(KERN_WARNING |
| 5314 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5315 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5316 | err = -EINVAL; |
| 5317 | goto abort_export; |
| 5318 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5319 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5321 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5322 | err = bind_rdev_to_array(rdev, mddev); |
| 5323 | if (err) |
| 5324 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | |
| 5326 | /* |
| 5327 | * The rest should better be atomic, we can have disk failures |
| 5328 | * noticed in interrupt contexts ... |
| 5329 | */ |
| 5330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | rdev->raid_disk = -1; |
| 5332 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5333 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | |
| 5335 | /* |
| 5336 | * Kick recovery, maybe this spare has to be added to the |
| 5337 | * array immediately. |
| 5338 | */ |
| 5339 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5340 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5341 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | return 0; |
| 5343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | abort_export: |
| 5345 | export_rdev(rdev); |
| 5346 | return err; |
| 5347 | } |
| 5348 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5349 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5350 | { |
| 5351 | int err; |
| 5352 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5353 | if (mddev->pers) { |
| 5354 | if (!mddev->pers->quiesce) |
| 5355 | return -EBUSY; |
| 5356 | if (mddev->recovery || mddev->sync_thread) |
| 5357 | return -EBUSY; |
| 5358 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5359 | } |
| 5360 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5361 | |
| 5362 | if (fd >= 0) { |
| 5363 | if (mddev->bitmap) |
| 5364 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5365 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5366 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5367 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5368 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5369 | mdname(mddev)); |
| 5370 | return -EBADF; |
| 5371 | } |
| 5372 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5373 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5374 | if (err) { |
| 5375 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5376 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5377 | fput(mddev->bitmap_info.file); |
| 5378 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5379 | return err; |
| 5380 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5381 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5382 | } else if (mddev->bitmap == NULL) |
| 5383 | return -ENOENT; /* cannot remove what isn't there */ |
| 5384 | err = 0; |
| 5385 | if (mddev->pers) { |
| 5386 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5387 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5388 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5389 | if (!err) |
| 5390 | err = bitmap_load(mddev); |
| 5391 | } |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5392 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5393 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5394 | fd = -1; /* make sure to put the file */ |
| 5395 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5396 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5397 | } |
| 5398 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5399 | if (mddev->bitmap_info.file) { |
| 5400 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5401 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5402 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5403 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5406 | return err; |
| 5407 | } |
| 5408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | /* |
| 5410 | * set_array_info is used two different ways |
| 5411 | * The original usage is when creating a new array. |
| 5412 | * In this usage, raid_disks is > 0 and it together with |
| 5413 | * level, size, not_persistent,layout,chunksize determine the |
| 5414 | * shape of the array. |
| 5415 | * This will always create an array with a type-0.90.0 superblock. |
| 5416 | * The newer usage is when assembling an array. |
| 5417 | * In this case raid_disks will be 0, and the major_version field is |
| 5418 | * use to determine which style super-blocks are to be found on the devices. |
| 5419 | * The minor and patch _version numbers are also kept incase the |
| 5420 | * super_block handler wishes to interpret them. |
| 5421 | */ |
| 5422 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5423 | { |
| 5424 | |
| 5425 | if (info->raid_disks == 0) { |
| 5426 | /* just setting version number for superblock loading */ |
| 5427 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5428 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | super_types[info->major_version].name == NULL) { |
| 5430 | /* maybe try to auto-load a module? */ |
| 5431 | printk(KERN_INFO |
| 5432 | "md: superblock version %d not known\n", |
| 5433 | info->major_version); |
| 5434 | return -EINVAL; |
| 5435 | } |
| 5436 | mddev->major_version = info->major_version; |
| 5437 | mddev->minor_version = info->minor_version; |
| 5438 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5439 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5440 | /* ensure mddev_put doesn't delete this now that there |
| 5441 | * is some minimal configuration. |
| 5442 | */ |
| 5443 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | return 0; |
| 5445 | } |
| 5446 | mddev->major_version = MD_MAJOR_VERSION; |
| 5447 | mddev->minor_version = MD_MINOR_VERSION; |
| 5448 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5449 | mddev->ctime = get_seconds(); |
| 5450 | |
| 5451 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5452 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5453 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | mddev->raid_disks = info->raid_disks; |
| 5455 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5456 | * openned |
| 5457 | */ |
| 5458 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5459 | mddev->recovery_cp = MaxSector; |
| 5460 | else |
| 5461 | mddev->recovery_cp = 0; |
| 5462 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5463 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
| 5465 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5466 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
| 5468 | mddev->max_disks = MD_SB_DISKS; |
| 5469 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5470 | if (mddev->persistent) |
| 5471 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5472 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5474 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5475 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5476 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5477 | mddev->reshape_position = MaxSector; |
| 5478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | /* |
| 5480 | * Generate a 128 bit UUID |
| 5481 | */ |
| 5482 | get_random_bytes(mddev->uuid, 16); |
| 5483 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5484 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5485 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5486 | mddev->new_layout = mddev->layout; |
| 5487 | mddev->delta_disks = 0; |
| 5488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | return 0; |
| 5490 | } |
| 5491 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5492 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5493 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5494 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5495 | |
| 5496 | if (mddev->external_size) |
| 5497 | return; |
| 5498 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5499 | mddev->array_sectors = array_sectors; |
| 5500 | } |
| 5501 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5502 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5503 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5504 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5505 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5506 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5507 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5508 | |
| 5509 | if (mddev->pers->resize == NULL) |
| 5510 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5511 | /* The "num_sectors" is the number of sectors of each device that |
| 5512 | * is used. This can only make sense for arrays with redundancy. |
| 5513 | * linear and raid0 always use whatever space is available. We can only |
| 5514 | * consider changing this number if no resync or reconstruction is |
| 5515 | * 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] | 5516 | * 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] | 5517 | * of each device. If num_sectors is zero, we find the largest size |
| 5518 | * that fits. |
| 5519 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5520 | */ |
| 5521 | if (mddev->sync_thread) |
| 5522 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5523 | if (mddev->bitmap) |
| 5524 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5525 | * grow, and re-add. |
| 5526 | */ |
| 5527 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5528 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5529 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5530 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5531 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5532 | num_sectors = avail; |
| 5533 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5534 | return -ENOSPC; |
| 5535 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5536 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5537 | if (!rv) |
| 5538 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5539 | return rv; |
| 5540 | } |
| 5541 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5542 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5543 | { |
| 5544 | int rv; |
| 5545 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5546 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5547 | return -EINVAL; |
| 5548 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5549 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5550 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5551 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5552 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5553 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5554 | |
| 5555 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5556 | return rv; |
| 5557 | } |
| 5558 | |
| 5559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | /* |
| 5561 | * update_array_info is used to change the configuration of an |
| 5562 | * on-line array. |
| 5563 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5564 | * fields in the info are checked against the array. |
| 5565 | * Any differences that cannot be handled will cause an error. |
| 5566 | * Normally, only one change can be managed at a time. |
| 5567 | */ |
| 5568 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5569 | { |
| 5570 | int rv = 0; |
| 5571 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5572 | int state = 0; |
| 5573 | |
| 5574 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5575 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5576 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | |
| 5578 | if (mddev->major_version != info->major_version || |
| 5579 | mddev->minor_version != info->minor_version || |
| 5580 | /* mddev->patch_version != info->patch_version || */ |
| 5581 | mddev->ctime != info->ctime || |
| 5582 | mddev->level != info->level || |
| 5583 | /* mddev->layout != info->layout || */ |
| 5584 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5585 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5586 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5587 | ((state^info->state) & 0xfffffe00) |
| 5588 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | return -EINVAL; |
| 5590 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5591 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5592 | cnt++; |
| 5593 | if (mddev->raid_disks != info->raid_disks) |
| 5594 | cnt++; |
| 5595 | if (mddev->layout != info->layout) |
| 5596 | cnt++; |
| 5597 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5598 | cnt++; |
| 5599 | if (cnt == 0) |
| 5600 | return 0; |
| 5601 | if (cnt > 1) |
| 5602 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | |
| 5604 | if (mddev->layout != info->layout) { |
| 5605 | /* Change layout |
| 5606 | * we don't need to do anything at the md level, the |
| 5607 | * personality will take care of it all. |
| 5608 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5609 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5611 | else { |
| 5612 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5613 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5614 | if (rv) |
| 5615 | mddev->new_layout = mddev->layout; |
| 5616 | return rv; |
| 5617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5619 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5620 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5622 | if (mddev->raid_disks != info->raid_disks) |
| 5623 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5625 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5626 | if (mddev->pers->quiesce == NULL) |
| 5627 | return -EINVAL; |
| 5628 | if (mddev->recovery || mddev->sync_thread) |
| 5629 | return -EBUSY; |
| 5630 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5631 | /* add the bitmap */ |
| 5632 | if (mddev->bitmap) |
| 5633 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5634 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5635 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5636 | mddev->bitmap_info.offset = |
| 5637 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5638 | mddev->pers->quiesce(mddev, 1); |
| 5639 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5640 | if (!rv) |
| 5641 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5642 | if (rv) |
| 5643 | bitmap_destroy(mddev); |
| 5644 | mddev->pers->quiesce(mddev, 0); |
| 5645 | } else { |
| 5646 | /* remove the bitmap */ |
| 5647 | if (!mddev->bitmap) |
| 5648 | return -ENOENT; |
| 5649 | if (mddev->bitmap->file) |
| 5650 | return -EINVAL; |
| 5651 | mddev->pers->quiesce(mddev, 1); |
| 5652 | bitmap_destroy(mddev); |
| 5653 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5654 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5655 | } |
| 5656 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5657 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | return rv; |
| 5659 | } |
| 5660 | |
| 5661 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5662 | { |
| 5663 | mdk_rdev_t *rdev; |
| 5664 | |
| 5665 | if (mddev->pers == NULL) |
| 5666 | return -ENODEV; |
| 5667 | |
| 5668 | rdev = find_rdev(mddev, dev); |
| 5669 | if (!rdev) |
| 5670 | return -ENODEV; |
| 5671 | |
| 5672 | md_error(mddev, rdev); |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5676 | /* |
| 5677 | * We have a problem here : there is no easy way to give a CHS |
| 5678 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5679 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5680 | * dosfs just mad... ;-) |
| 5681 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5682 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5683 | { |
| 5684 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5685 | |
| 5686 | geo->heads = 2; |
| 5687 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5688 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5689 | return 0; |
| 5690 | } |
| 5691 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5692 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | unsigned int cmd, unsigned long arg) |
| 5694 | { |
| 5695 | int err = 0; |
| 5696 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5698 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | |
| 5700 | if (!capable(CAP_SYS_ADMIN)) |
| 5701 | return -EACCES; |
| 5702 | |
| 5703 | /* |
| 5704 | * Commands dealing with the RAID driver but not any |
| 5705 | * particular array: |
| 5706 | */ |
| 5707 | switch (cmd) |
| 5708 | { |
| 5709 | case RAID_VERSION: |
| 5710 | err = get_version(argp); |
| 5711 | goto done; |
| 5712 | |
| 5713 | case PRINT_RAID_DEBUG: |
| 5714 | err = 0; |
| 5715 | md_print_devices(); |
| 5716 | goto done; |
| 5717 | |
| 5718 | #ifndef MODULE |
| 5719 | case RAID_AUTORUN: |
| 5720 | err = 0; |
| 5721 | autostart_arrays(arg); |
| 5722 | goto done; |
| 5723 | #endif |
| 5724 | default:; |
| 5725 | } |
| 5726 | |
| 5727 | /* |
| 5728 | * Commands creating/starting a new array: |
| 5729 | */ |
| 5730 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5731 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | |
| 5733 | if (!mddev) { |
| 5734 | BUG(); |
| 5735 | goto abort; |
| 5736 | } |
| 5737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | err = mddev_lock(mddev); |
| 5739 | if (err) { |
| 5740 | printk(KERN_INFO |
| 5741 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5742 | err, cmd); |
| 5743 | goto abort; |
| 5744 | } |
| 5745 | |
| 5746 | switch (cmd) |
| 5747 | { |
| 5748 | case SET_ARRAY_INFO: |
| 5749 | { |
| 5750 | mdu_array_info_t info; |
| 5751 | if (!arg) |
| 5752 | memset(&info, 0, sizeof(info)); |
| 5753 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5754 | err = -EFAULT; |
| 5755 | goto abort_unlock; |
| 5756 | } |
| 5757 | if (mddev->pers) { |
| 5758 | err = update_array_info(mddev, &info); |
| 5759 | if (err) { |
| 5760 | printk(KERN_WARNING "md: couldn't update" |
| 5761 | " array info. %d\n", err); |
| 5762 | goto abort_unlock; |
| 5763 | } |
| 5764 | goto done_unlock; |
| 5765 | } |
| 5766 | if (!list_empty(&mddev->disks)) { |
| 5767 | printk(KERN_WARNING |
| 5768 | "md: array %s already has disks!\n", |
| 5769 | mdname(mddev)); |
| 5770 | err = -EBUSY; |
| 5771 | goto abort_unlock; |
| 5772 | } |
| 5773 | if (mddev->raid_disks) { |
| 5774 | printk(KERN_WARNING |
| 5775 | "md: array %s already initialised!\n", |
| 5776 | mdname(mddev)); |
| 5777 | err = -EBUSY; |
| 5778 | goto abort_unlock; |
| 5779 | } |
| 5780 | err = set_array_info(mddev, &info); |
| 5781 | if (err) { |
| 5782 | printk(KERN_WARNING "md: couldn't set" |
| 5783 | " array info. %d\n", err); |
| 5784 | goto abort_unlock; |
| 5785 | } |
| 5786 | } |
| 5787 | goto done_unlock; |
| 5788 | |
| 5789 | default:; |
| 5790 | } |
| 5791 | |
| 5792 | /* |
| 5793 | * Commands querying/configuring an existing array: |
| 5794 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5795 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5796 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5797 | if ((!mddev->raid_disks && !mddev->external) |
| 5798 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5799 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5800 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | err = -ENODEV; |
| 5802 | goto abort_unlock; |
| 5803 | } |
| 5804 | |
| 5805 | /* |
| 5806 | * Commands even a read-only array can execute: |
| 5807 | */ |
| 5808 | switch (cmd) |
| 5809 | { |
| 5810 | case GET_ARRAY_INFO: |
| 5811 | err = get_array_info(mddev, argp); |
| 5812 | goto done_unlock; |
| 5813 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5814 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5815 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5816 | goto done_unlock; |
| 5817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | case GET_DISK_INFO: |
| 5819 | err = get_disk_info(mddev, argp); |
| 5820 | goto done_unlock; |
| 5821 | |
| 5822 | case RESTART_ARRAY_RW: |
| 5823 | err = restart_array(mddev); |
| 5824 | goto done_unlock; |
| 5825 | |
| 5826 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5827 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | goto done_unlock; |
| 5829 | |
| 5830 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5831 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | goto done_unlock; |
| 5833 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5834 | case BLKROSET: |
| 5835 | if (get_user(ro, (int __user *)(arg))) { |
| 5836 | err = -EFAULT; |
| 5837 | goto done_unlock; |
| 5838 | } |
| 5839 | err = -EINVAL; |
| 5840 | |
| 5841 | /* if the bdev is going readonly the value of mddev->ro |
| 5842 | * does not matter, no writes are coming |
| 5843 | */ |
| 5844 | if (ro) |
| 5845 | goto done_unlock; |
| 5846 | |
| 5847 | /* are we are already prepared for writes? */ |
| 5848 | if (mddev->ro != 1) |
| 5849 | goto done_unlock; |
| 5850 | |
| 5851 | /* transitioning to readauto need only happen for |
| 5852 | * arrays that call md_write_start |
| 5853 | */ |
| 5854 | if (mddev->pers) { |
| 5855 | err = restart_array(mddev); |
| 5856 | if (err == 0) { |
| 5857 | mddev->ro = 2; |
| 5858 | set_disk_ro(mddev->gendisk, 0); |
| 5859 | } |
| 5860 | } |
| 5861 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | /* |
| 5865 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5866 | * superblock, so we do not allow them on read-only arrays. |
| 5867 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5868 | * here and hit the 'default' below, so only disallow |
| 5869 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5871 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5872 | if (mddev->ro == 2) { |
| 5873 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5874 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5875 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5876 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5877 | } else { |
| 5878 | err = -EROFS; |
| 5879 | goto abort_unlock; |
| 5880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | switch (cmd) |
| 5884 | { |
| 5885 | case ADD_NEW_DISK: |
| 5886 | { |
| 5887 | mdu_disk_info_t info; |
| 5888 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5889 | err = -EFAULT; |
| 5890 | else |
| 5891 | err = add_new_disk(mddev, &info); |
| 5892 | goto done_unlock; |
| 5893 | } |
| 5894 | |
| 5895 | case HOT_REMOVE_DISK: |
| 5896 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5897 | goto done_unlock; |
| 5898 | |
| 5899 | case HOT_ADD_DISK: |
| 5900 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5901 | goto done_unlock; |
| 5902 | |
| 5903 | case SET_DISK_FAULTY: |
| 5904 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5905 | goto done_unlock; |
| 5906 | |
| 5907 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5908 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | goto done_unlock; |
| 5910 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5911 | case SET_BITMAP_FILE: |
| 5912 | err = set_bitmap_file(mddev, (int)arg); |
| 5913 | goto done_unlock; |
| 5914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | err = -EINVAL; |
| 5917 | goto abort_unlock; |
| 5918 | } |
| 5919 | |
| 5920 | done_unlock: |
| 5921 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5922 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5923 | err != -EINVAL) |
| 5924 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | mddev_unlock(mddev); |
| 5926 | |
| 5927 | return err; |
| 5928 | done: |
| 5929 | if (err) |
| 5930 | MD_BUG(); |
| 5931 | abort: |
| 5932 | return err; |
| 5933 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5934 | #ifdef CONFIG_COMPAT |
| 5935 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5936 | unsigned int cmd, unsigned long arg) |
| 5937 | { |
| 5938 | switch (cmd) { |
| 5939 | case HOT_REMOVE_DISK: |
| 5940 | case HOT_ADD_DISK: |
| 5941 | case SET_DISK_FAULTY: |
| 5942 | case SET_BITMAP_FILE: |
| 5943 | /* These take in integer arg, do not convert */ |
| 5944 | break; |
| 5945 | default: |
| 5946 | arg = (unsigned long)compat_ptr(arg); |
| 5947 | break; |
| 5948 | } |
| 5949 | |
| 5950 | return md_ioctl(bdev, mode, cmd, arg); |
| 5951 | } |
| 5952 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5954 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | { |
| 5956 | /* |
| 5957 | * Succeed if we can lock the mddev, which confirms that |
| 5958 | * it isn't being stopped right now. |
| 5959 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5960 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | int err; |
| 5962 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5963 | if (mddev->gendisk != bdev->bd_disk) { |
| 5964 | /* we are racing with mddev_put which is discarding this |
| 5965 | * bd_disk. |
| 5966 | */ |
| 5967 | mddev_put(mddev); |
| 5968 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5969 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5970 | /* Then retry the open from the top */ |
| 5971 | return -ERESTARTSYS; |
| 5972 | } |
| 5973 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5974 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5975 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | goto out; |
| 5977 | |
| 5978 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5979 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5980 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5982 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | out: |
| 5984 | return err; |
| 5985 | } |
| 5986 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5987 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5989 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5991 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5992 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | mddev_put(mddev); |
| 5994 | |
| 5995 | return 0; |
| 5996 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5997 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | { |
| 5999 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6000 | .open = md_open, |
| 6001 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6002 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6003 | #ifdef CONFIG_COMPAT |
| 6004 | .compat_ioctl = md_compat_ioctl, |
| 6005 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6006 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | }; |
| 6008 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6009 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | { |
| 6011 | mdk_thread_t *thread = arg; |
| 6012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | /* |
| 6014 | * md_thread is a 'system-thread', it's priority should be very |
| 6015 | * high. We avoid resource deadlocks individually in each |
| 6016 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6017 | * the very same RT priority as kswapd, thus we will never get |
| 6018 | * into a priority inversion deadlock. |
| 6019 | * |
| 6020 | * we definitely have to have equal or higher priority than |
| 6021 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6022 | * many dirty RAID5 blocks. |
| 6023 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6025 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6026 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6028 | /* We need to wait INTERRUPTIBLE so that |
| 6029 | * we don't add to the load-average. |
| 6030 | * That means we need to be sure no signals are |
| 6031 | * pending |
| 6032 | */ |
| 6033 | if (signal_pending(current)) |
| 6034 | flush_signals(current); |
| 6035 | |
| 6036 | wait_event_interruptible_timeout |
| 6037 | (thread->wqueue, |
| 6038 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6039 | || kthread_should_stop(), |
| 6040 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | |
| 6042 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6043 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6044 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | return 0; |
| 6048 | } |
| 6049 | |
| 6050 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6051 | { |
| 6052 | if (thread) { |
| 6053 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6054 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6055 | wake_up(&thread->wqueue); |
| 6056 | } |
| 6057 | } |
| 6058 | |
| 6059 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6060 | const char *name) |
| 6061 | { |
| 6062 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6064 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | if (!thread) |
| 6066 | return NULL; |
| 6067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | init_waitqueue_head(&thread->wqueue); |
| 6069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | thread->run = run; |
| 6071 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6072 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6073 | thread->tsk = kthread_run(md_thread, thread, |
| 6074 | "%s_%s", |
| 6075 | mdname(thread->mddev), |
| 6076 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6077 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | kfree(thread); |
| 6079 | return NULL; |
| 6080 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | return thread; |
| 6082 | } |
| 6083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | void md_unregister_thread(mdk_thread_t *thread) |
| 6085 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6086 | if (!thread) |
| 6087 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6088 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6089 | |
| 6090 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | kfree(thread); |
| 6092 | } |
| 6093 | |
| 6094 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6095 | { |
| 6096 | if (!mddev) { |
| 6097 | MD_BUG(); |
| 6098 | return; |
| 6099 | } |
| 6100 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6101 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6103 | |
| 6104 | if (mddev->external) |
| 6105 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6108 | mdname(mddev), |
| 6109 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6110 | __builtin_return_address(0),__builtin_return_address(1), |
| 6111 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6112 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6113 | if (!mddev->pers) |
| 6114 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | if (!mddev->pers->error_handler) |
| 6116 | return; |
| 6117 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6118 | if (mddev->degraded) |
| 6119 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6120 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6122 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6123 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6124 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6125 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6126 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | /* seq_file implementation /proc/mdstat */ |
| 6130 | |
| 6131 | static void status_unused(struct seq_file *seq) |
| 6132 | { |
| 6133 | int i = 0; |
| 6134 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | |
| 6136 | seq_printf(seq, "unused devices: "); |
| 6137 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6138 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | char b[BDEVNAME_SIZE]; |
| 6140 | i++; |
| 6141 | seq_printf(seq, "%s ", |
| 6142 | bdevname(rdev->bdev,b)); |
| 6143 | } |
| 6144 | if (!i) |
| 6145 | seq_printf(seq, "<none>"); |
| 6146 | |
| 6147 | seq_printf(seq, "\n"); |
| 6148 | } |
| 6149 | |
| 6150 | |
| 6151 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6152 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6153 | sector_t max_sectors, resync, res; |
| 6154 | unsigned long dt, db; |
| 6155 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6156 | int scale; |
| 6157 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6159 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
| 6161 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6162 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6164 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | |
| 6166 | /* |
| 6167 | * Should not happen. |
| 6168 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6169 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | MD_BUG(); |
| 6171 | return; |
| 6172 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6173 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6174 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6175 | * u32, as those are the requirements for sector_div. |
| 6176 | * Thus 'scale' must be at least 10 |
| 6177 | */ |
| 6178 | scale = 10; |
| 6179 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6180 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6181 | scale++; |
| 6182 | } |
| 6183 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6184 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6185 | |
| 6186 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6188 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | seq_printf(seq, "["); |
| 6190 | for (i = 0; i < x; i++) |
| 6191 | seq_printf(seq, "="); |
| 6192 | seq_printf(seq, ">"); |
| 6193 | for (i = 0; i < y; i++) |
| 6194 | seq_printf(seq, "."); |
| 6195 | seq_printf(seq, "] "); |
| 6196 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6197 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6198 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6199 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6200 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6201 | "check" : |
| 6202 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6203 | "resync" : "recovery"))), |
| 6204 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6205 | (unsigned long long) resync/2, |
| 6206 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | |
| 6208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | * dt: time from mark until now |
| 6210 | * db: blocks written from mark until now |
| 6211 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6212 | * |
| 6213 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6214 | * So we divide before multiply in case it is 32bit and close |
| 6215 | * to the limit. |
| 6216 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6217 | * near the end of resync when the number of remaining sectors |
| 6218 | * is close to 'db'. |
| 6219 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6220 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | */ |
| 6222 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6223 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6224 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6225 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6227 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6228 | sector_div(rt, db/32+1); |
| 6229 | rt *= dt; |
| 6230 | rt >>= 5; |
| 6231 | |
| 6232 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6233 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6235 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6236 | } |
| 6237 | |
| 6238 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6239 | { |
| 6240 | struct list_head *tmp; |
| 6241 | loff_t l = *pos; |
| 6242 | mddev_t *mddev; |
| 6243 | |
| 6244 | if (l >= 0x10000) |
| 6245 | return NULL; |
| 6246 | if (!l--) |
| 6247 | /* header */ |
| 6248 | return (void*)1; |
| 6249 | |
| 6250 | spin_lock(&all_mddevs_lock); |
| 6251 | list_for_each(tmp,&all_mddevs) |
| 6252 | if (!l--) { |
| 6253 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6254 | mddev_get(mddev); |
| 6255 | spin_unlock(&all_mddevs_lock); |
| 6256 | return mddev; |
| 6257 | } |
| 6258 | spin_unlock(&all_mddevs_lock); |
| 6259 | if (!l--) |
| 6260 | return (void*)2;/* tail */ |
| 6261 | return NULL; |
| 6262 | } |
| 6263 | |
| 6264 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6265 | { |
| 6266 | struct list_head *tmp; |
| 6267 | mddev_t *next_mddev, *mddev = v; |
| 6268 | |
| 6269 | ++*pos; |
| 6270 | if (v == (void*)2) |
| 6271 | return NULL; |
| 6272 | |
| 6273 | spin_lock(&all_mddevs_lock); |
| 6274 | if (v == (void*)1) |
| 6275 | tmp = all_mddevs.next; |
| 6276 | else |
| 6277 | tmp = mddev->all_mddevs.next; |
| 6278 | if (tmp != &all_mddevs) |
| 6279 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6280 | else { |
| 6281 | next_mddev = (void*)2; |
| 6282 | *pos = 0x10000; |
| 6283 | } |
| 6284 | spin_unlock(&all_mddevs_lock); |
| 6285 | |
| 6286 | if (v != (void*)1) |
| 6287 | mddev_put(mddev); |
| 6288 | return next_mddev; |
| 6289 | |
| 6290 | } |
| 6291 | |
| 6292 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6293 | { |
| 6294 | mddev_t *mddev = v; |
| 6295 | |
| 6296 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6297 | mddev_put(mddev); |
| 6298 | } |
| 6299 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6300 | struct mdstat_info { |
| 6301 | int event; |
| 6302 | }; |
| 6303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6305 | { |
| 6306 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6307 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6309 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6310 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | |
| 6312 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6313 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | seq_printf(seq, "Personalities : "); |
| 6315 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6316 | list_for_each_entry(pers, &pers_list, list) |
| 6317 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | |
| 6319 | spin_unlock(&pers_lock); |
| 6320 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6321 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | return 0; |
| 6323 | } |
| 6324 | if (v == (void*)2) { |
| 6325 | status_unused(seq); |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6329 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6333 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6334 | mddev->pers ? "" : "in"); |
| 6335 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6336 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6338 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6339 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | seq_printf(seq, " %s", mddev->pers->name); |
| 6341 | } |
| 6342 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6343 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6344 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | char b[BDEVNAME_SIZE]; |
| 6346 | seq_printf(seq, " %s[%d]", |
| 6347 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6348 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6349 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6350 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | seq_printf(seq, "(F)"); |
| 6352 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6353 | } else if (rdev->raid_disk < 0) |
| 6354 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6355 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | if (!list_empty(&mddev->disks)) { |
| 6359 | if (mddev->pers) |
| 6360 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6361 | (unsigned long long) |
| 6362 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | else |
| 6364 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6365 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6367 | if (mddev->persistent) { |
| 6368 | if (mddev->major_version != 0 || |
| 6369 | mddev->minor_version != 90) { |
| 6370 | seq_printf(seq," super %d.%d", |
| 6371 | mddev->major_version, |
| 6372 | mddev->minor_version); |
| 6373 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6374 | } else if (mddev->external) |
| 6375 | seq_printf(seq, " super external:%s", |
| 6376 | mddev->metadata_type); |
| 6377 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6378 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | |
| 6380 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6381 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6383 | if (mddev->pers->sync_request) { |
| 6384 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6385 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6386 | seq_printf(seq, "\n "); |
| 6387 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6388 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6389 | else if (mddev->recovery_cp < MaxSector) |
| 6390 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6391 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6392 | } else |
| 6393 | seq_printf(seq, "\n "); |
| 6394 | |
| 6395 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6396 | unsigned long chunk_kb; |
| 6397 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6398 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6399 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6400 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6401 | "%lu%s chunk", |
| 6402 | bitmap->pages - bitmap->missing_pages, |
| 6403 | bitmap->pages, |
| 6404 | (bitmap->pages - bitmap->missing_pages) |
| 6405 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6406 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6407 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6408 | if (bitmap->file) { |
| 6409 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6410 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6411 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6412 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6413 | seq_printf(seq, "\n"); |
| 6414 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | seq_printf(seq, "\n"); |
| 6418 | } |
| 6419 | mddev_unlock(mddev); |
| 6420 | |
| 6421 | return 0; |
| 6422 | } |
| 6423 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6424 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | .start = md_seq_start, |
| 6426 | .next = md_seq_next, |
| 6427 | .stop = md_seq_stop, |
| 6428 | .show = md_seq_show, |
| 6429 | }; |
| 6430 | |
| 6431 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6432 | { |
| 6433 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6434 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6435 | if (mi == NULL) |
| 6436 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | |
| 6438 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6439 | if (error) |
| 6440 | kfree(mi); |
| 6441 | else { |
| 6442 | struct seq_file *p = file->private_data; |
| 6443 | p->private = mi; |
| 6444 | mi->event = atomic_read(&md_event_count); |
| 6445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | return error; |
| 6447 | } |
| 6448 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6449 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6450 | { |
| 6451 | struct seq_file *m = filp->private_data; |
| 6452 | struct mdstat_info *mi = m->private; |
| 6453 | int mask; |
| 6454 | |
| 6455 | poll_wait(filp, &md_event_waiters, wait); |
| 6456 | |
| 6457 | /* always allow read */ |
| 6458 | mask = POLLIN | POLLRDNORM; |
| 6459 | |
| 6460 | if (mi->event != atomic_read(&md_event_count)) |
| 6461 | mask |= POLLERR | POLLPRI; |
| 6462 | return mask; |
| 6463 | } |
| 6464 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6465 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6466 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | .open = md_seq_open, |
| 6468 | .read = seq_read, |
| 6469 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6470 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6471 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | }; |
| 6473 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6474 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6477 | list_add_tail(&p->list, &pers_list); |
| 6478 | 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] | 6479 | spin_unlock(&pers_lock); |
| 6480 | return 0; |
| 6481 | } |
| 6482 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6483 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6485 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6487 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | spin_unlock(&pers_lock); |
| 6489 | return 0; |
| 6490 | } |
| 6491 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6492 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | { |
| 6494 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6496 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | |
| 6498 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6499 | rcu_read_lock(); |
| 6500 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6502 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6503 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6504 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6505 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6506 | * as sync_io is counted when a request starts, and |
| 6507 | * disk_stats is counted when it completes. |
| 6508 | * So resync activity will cause curr_events to be smaller than |
| 6509 | * when there was no such activity. |
| 6510 | * non-sync IO will cause disk_stat to increase without |
| 6511 | * increasing sync_io so curr_events will (eventually) |
| 6512 | * be larger than it was before. Once it becomes |
| 6513 | * substantially larger, the test below will cause |
| 6514 | * the array to appear non-idle, and resync will slow |
| 6515 | * down. |
| 6516 | * If there is a lot of outstanding resync activity when |
| 6517 | * we set last_event to curr_events, then all that activity |
| 6518 | * completing might cause the array to appear non-idle |
| 6519 | * and resync will be slowed down even though there might |
| 6520 | * not have been non-resync activity. This will only |
| 6521 | * happen once though. 'last_events' will soon reflect |
| 6522 | * the state where there is little or no outstanding |
| 6523 | * resync requests, and further resync activity will |
| 6524 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6525 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6527 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | rdev->last_events = curr_events; |
| 6529 | idle = 0; |
| 6530 | } |
| 6531 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6532 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | return idle; |
| 6534 | } |
| 6535 | |
| 6536 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6537 | { |
| 6538 | /* another "blocks" (512byte) blocks have been synced */ |
| 6539 | atomic_sub(blocks, &mddev->recovery_active); |
| 6540 | wake_up(&mddev->recovery_wait); |
| 6541 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6542 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | md_wakeup_thread(mddev->thread); |
| 6544 | // stop recovery, signal do_sync .... |
| 6545 | } |
| 6546 | } |
| 6547 | |
| 6548 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6549 | /* md_write_start(mddev, bi) |
| 6550 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6551 | * in superblock) before writing, schedule a superblock update |
| 6552 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6553 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6554 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6556 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6557 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6558 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6559 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6560 | BUG_ON(mddev->ro == 1); |
| 6561 | if (mddev->ro == 2) { |
| 6562 | /* need to switch to read/write */ |
| 6563 | mddev->ro = 0; |
| 6564 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6565 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6566 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6567 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6568 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6569 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6570 | if (mddev->safemode == 1) |
| 6571 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6572 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6573 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6574 | if (mddev->in_sync) { |
| 6575 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6576 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6577 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6578 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6579 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6580 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6581 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6582 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6583 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6584 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6585 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6586 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | } |
| 6588 | |
| 6589 | void md_write_end(mddev_t *mddev) |
| 6590 | { |
| 6591 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6592 | if (mddev->safemode == 2) |
| 6593 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6594 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6596 | } |
| 6597 | } |
| 6598 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6599 | /* md_allow_write(mddev) |
| 6600 | * Calling this ensures that the array is marked 'active' so that writes |
| 6601 | * may proceed without blocking. It is important to call this before |
| 6602 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6603 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6604 | * |
| 6605 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6606 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6607 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6608 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6609 | { |
| 6610 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6611 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6612 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6613 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6614 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6615 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6616 | |
| 6617 | spin_lock_irq(&mddev->write_lock); |
| 6618 | if (mddev->in_sync) { |
| 6619 | mddev->in_sync = 0; |
| 6620 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6621 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6622 | if (mddev->safemode_delay && |
| 6623 | mddev->safemode == 0) |
| 6624 | mddev->safemode = 1; |
| 6625 | spin_unlock_irq(&mddev->write_lock); |
| 6626 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6627 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6628 | } else |
| 6629 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6630 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6631 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6632 | return -EAGAIN; |
| 6633 | else |
| 6634 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6635 | } |
| 6636 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6637 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6638 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6639 | { |
| 6640 | if (mddev->queue) |
| 6641 | blk_unplug(mddev->queue); |
| 6642 | if (mddev->plug) |
| 6643 | mddev->plug->unplug_fn(mddev->plug); |
| 6644 | } |
| 6645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | #define SYNC_MARKS 10 |
| 6647 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6648 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | { |
| 6650 | mddev_t *mddev2; |
| 6651 | unsigned int currspeed = 0, |
| 6652 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6653 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | unsigned long mark[SYNC_MARKS]; |
| 6655 | sector_t mark_cnt[SYNC_MARKS]; |
| 6656 | int last_mark,m; |
| 6657 | struct list_head *tmp; |
| 6658 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6659 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6660 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6661 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | |
| 6663 | /* just incase thread restarts... */ |
| 6664 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6665 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6666 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6667 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6669 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6670 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6671 | desc = "data-check"; |
| 6672 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6673 | desc = "requested-resync"; |
| 6674 | else |
| 6675 | desc = "resync"; |
| 6676 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6677 | desc = "reshape"; |
| 6678 | else |
| 6679 | desc = "recovery"; |
| 6680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | /* we overload curr_resync somewhat here. |
| 6682 | * 0 == not engaged in resync at all |
| 6683 | * 2 == checking that there is no conflict with another sync |
| 6684 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6685 | * commense |
| 6686 | * other == active in resync - this many blocks |
| 6687 | * |
| 6688 | * Before starting a resync we must have set curr_resync to |
| 6689 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6690 | * less than ours. When we find one that is the same or higher |
| 6691 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6692 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6693 | * This will mean we have to start checking from the beginning again. |
| 6694 | * |
| 6695 | */ |
| 6696 | |
| 6697 | do { |
| 6698 | mddev->curr_resync = 2; |
| 6699 | |
| 6700 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6701 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6702 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6703 | |
| 6704 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6706 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | if (mddev2 == mddev) |
| 6708 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6709 | if (!mddev->parallel_resync |
| 6710 | && mddev2->curr_resync |
| 6711 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | DEFINE_WAIT(wq); |
| 6713 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6714 | /* arbitrarily yield */ |
| 6715 | mddev->curr_resync = 1; |
| 6716 | wake_up(&resync_wait); |
| 6717 | } |
| 6718 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6719 | /* no need to wait here, we can wait the next |
| 6720 | * time 'round when curr_resync == 2 |
| 6721 | */ |
| 6722 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6723 | /* We need to wait 'interruptible' so as not to |
| 6724 | * contribute to the load average, and not to |
| 6725 | * be caught by 'softlockup' |
| 6726 | */ |
| 6727 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6728 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6729 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6730 | printk(KERN_INFO "md: delaying %s of %s" |
| 6731 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6733 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6735 | if (signal_pending(current)) |
| 6736 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | schedule(); |
| 6738 | finish_wait(&resync_wait, &wq); |
| 6739 | goto try_again; |
| 6740 | } |
| 6741 | finish_wait(&resync_wait, &wq); |
| 6742 | } |
| 6743 | } |
| 6744 | } while (mddev->curr_resync < 2); |
| 6745 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6746 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6747 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6749 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | */ |
| 6751 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6752 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6753 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6754 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6755 | j = mddev->resync_min; |
| 6756 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6757 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6758 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6759 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6760 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6761 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6763 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6764 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6765 | rcu_read_lock(); |
| 6766 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6767 | if (rdev->raid_disk >= 0 && |
| 6768 | !test_bit(Faulty, &rdev->flags) && |
| 6769 | !test_bit(In_sync, &rdev->flags) && |
| 6770 | rdev->recovery_offset < j) |
| 6771 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6772 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6775 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6776 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6777 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6778 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6779 | "(but not more than %d KB/sec) for %s.\n", |
| 6780 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6782 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6783 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6784 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6786 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6787 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | } |
| 6789 | last_mark = 0; |
| 6790 | mddev->resync_mark = mark[last_mark]; |
| 6791 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6792 | |
| 6793 | /* |
| 6794 | * Tune reconstruction: |
| 6795 | */ |
| 6796 | window = 32*(PAGE_SIZE/512); |
| 6797 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6798 | window/2,(unsigned long long) max_sectors/2); |
| 6799 | |
| 6800 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | last_check = 0; |
| 6802 | |
| 6803 | if (j>2) { |
| 6804 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6805 | "md: resuming %s of %s from checkpoint.\n", |
| 6806 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | mddev->curr_resync = j; |
| 6808 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6809 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | |
| 6811 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6812 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6814 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6815 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6816 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6817 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6818 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6819 | > (max_sectors >> 4)) || |
| 6820 | (j - mddev->curr_resync_completed)*2 |
| 6821 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6822 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6823 | /* time to update curr_resync_completed */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6824 | md_unplug(mddev); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6825 | wait_event(mddev->recovery_wait, |
| 6826 | atomic_read(&mddev->recovery_active) == 0); |
| 6827 | mddev->curr_resync_completed = |
| 6828 | mddev->curr_resync; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6829 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6830 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6831 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6832 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6833 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6834 | /* As this condition is controlled by user-space, |
| 6835 | * we can block indefinitely, so use '_interruptible' |
| 6836 | * to avoid triggering warnings. |
| 6837 | */ |
| 6838 | flush_signals(current); /* just in case */ |
| 6839 | wait_event_interruptible(mddev->recovery_wait, |
| 6840 | mddev->resync_max > j |
| 6841 | || kthread_should_stop()); |
| 6842 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6843 | |
| 6844 | if (kthread_should_stop()) |
| 6845 | goto interrupted; |
| 6846 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6847 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6848 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6849 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6850 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | goto out; |
| 6852 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6853 | |
| 6854 | if (!skipped) { /* actual IO requested */ |
| 6855 | io_sectors += sectors; |
| 6856 | atomic_add(sectors, &mddev->recovery_active); |
| 6857 | } |
| 6858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6859 | j += sectors; |
| 6860 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6861 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6862 | if (last_check == 0) |
| 6863 | /* this is the earliers that rebuilt will be |
| 6864 | * visible in /proc/mdstat |
| 6865 | */ |
| 6866 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6867 | |
| 6868 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | continue; |
| 6870 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6871 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6873 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | break; |
| 6875 | |
| 6876 | repeat: |
| 6877 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6878 | /* step marks */ |
| 6879 | int next = (last_mark+1) % SYNC_MARKS; |
| 6880 | |
| 6881 | mddev->resync_mark = mark[next]; |
| 6882 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6883 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6884 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | last_mark = next; |
| 6886 | } |
| 6887 | |
| 6888 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6889 | if (kthread_should_stop()) |
| 6890 | goto interrupted; |
| 6891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | |
| 6893 | /* |
| 6894 | * this loop exits only if either when we are slower than |
| 6895 | * the 'hard' speed limit, or the system was IO-idle for |
| 6896 | * a jiffy. |
| 6897 | * the system might be non-idle CPU-wise, but we only care |
| 6898 | * about not overloading the IO subsystem. (things like an |
| 6899 | * e2fsck being done on the RAID array should execute fast) |
| 6900 | */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6901 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | cond_resched(); |
| 6903 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6904 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6905 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6907 | if (currspeed > speed_min(mddev)) { |
| 6908 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6909 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6910 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | goto repeat; |
| 6912 | } |
| 6913 | } |
| 6914 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6915 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | /* |
| 6917 | * this also signals 'finished resyncing' to md_stop |
| 6918 | */ |
| 6919 | out: |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6920 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
| 6922 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6923 | |
| 6924 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6925 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6927 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6928 | mddev->curr_resync > 2) { |
| 6929 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6930 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6931 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6932 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6933 | "md: checkpointing %s of %s.\n", |
| 6934 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6935 | mddev->recovery_cp = mddev->curr_resync; |
| 6936 | } |
| 6937 | } else |
| 6938 | mddev->recovery_cp = MaxSector; |
| 6939 | } else { |
| 6940 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6941 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6942 | rcu_read_lock(); |
| 6943 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6944 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6945 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6946 | !test_bit(Faulty, &rdev->flags) && |
| 6947 | !test_bit(In_sync, &rdev->flags) && |
| 6948 | rdev->recovery_offset < mddev->curr_resync) |
| 6949 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6950 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6953 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6956 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6957 | /* We completed so min/max setting can be forgotten if used. */ |
| 6958 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6959 | mddev->resync_min = 0; |
| 6960 | mddev->resync_max = MaxSector; |
| 6961 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6962 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6964 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6965 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6966 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | wake_up(&resync_wait); |
| 6968 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6969 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6970 | return; |
| 6971 | |
| 6972 | interrupted: |
| 6973 | /* |
| 6974 | * got a signal, exit. |
| 6975 | */ |
| 6976 | printk(KERN_INFO |
| 6977 | "md: md_do_sync() got signal ... exiting\n"); |
| 6978 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6979 | goto out; |
| 6980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6982 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | |
| 6984 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6985 | static int remove_and_add_spares(mddev_t *mddev) |
| 6986 | { |
| 6987 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6988 | int spares = 0; |
| 6989 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6990 | mddev->curr_resync_completed = 0; |
| 6991 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6992 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6993 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6994 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6995 | (test_bit(Faulty, &rdev->flags) || |
| 6996 | ! test_bit(In_sync, &rdev->flags)) && |
| 6997 | atomic_read(&rdev->nr_pending)==0) { |
| 6998 | if (mddev->pers->hot_remove_disk( |
| 6999 | mddev, rdev->raid_disk)==0) { |
| 7000 | char nm[20]; |
| 7001 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7002 | sysfs_remove_link(&mddev->kobj, nm); |
| 7003 | rdev->raid_disk = -1; |
| 7004 | } |
| 7005 | } |
| 7006 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 7007 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7008 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7009 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7010 | !test_bit(In_sync, &rdev->flags) && |
| 7011 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7012 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7013 | if (rdev->raid_disk < 0 |
| 7014 | && !test_bit(Faulty, &rdev->flags)) { |
| 7015 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7016 | if (mddev->pers-> |
| 7017 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7018 | char nm[20]; |
| 7019 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7020 | if (sysfs_create_link(&mddev->kobj, |
| 7021 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7022 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7023 | spares++; |
| 7024 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7025 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7026 | } else |
| 7027 | break; |
| 7028 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7029 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7030 | } |
| 7031 | return spares; |
| 7032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | /* |
| 7034 | * This routine is regularly called by all per-raid-array threads to |
| 7035 | * deal with generic issues like resync and super-block update. |
| 7036 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7037 | * need this as they never do any recovery or update the superblock. |
| 7038 | * |
| 7039 | * It does not do any resync itself, but rather "forks" off other threads |
| 7040 | * to do that as needed. |
| 7041 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7042 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7043 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | * and wakeups up this thread which will reap the thread and finish up. |
| 7045 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7046 | * |
| 7047 | * The overall approach is: |
| 7048 | * 1/ if the superblock needs updating, update it. |
| 7049 | * 2/ If a recovery thread is running, don't do anything else. |
| 7050 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7051 | * 4/ If there are any faulty devices, remove them. |
| 7052 | * 5/ If array is degraded, try to add spares devices |
| 7053 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7054 | */ |
| 7055 | void md_check_recovery(mddev_t *mddev) |
| 7056 | { |
| 7057 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | |
| 7059 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7060 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7061 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | |
| 7063 | if (mddev->ro) |
| 7064 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7065 | |
| 7066 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7067 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7068 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7069 | mdname(mddev)); |
| 7070 | mddev->safemode = 2; |
| 7071 | } |
| 7072 | flush_signals(current); |
| 7073 | } |
| 7074 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7075 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7076 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7078 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7080 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7081 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7082 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7083 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7084 | )) |
| 7085 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7086 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7087 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7088 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7089 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7090 | if (mddev->ro) { |
| 7091 | /* Only thing we do on a ro array is remove |
| 7092 | * failed devices. |
| 7093 | */ |
| 7094 | remove_and_add_spares(mddev); |
| 7095 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7096 | goto unlock; |
| 7097 | } |
| 7098 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7099 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7100 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7101 | spin_lock_irq(&mddev->write_lock); |
| 7102 | if (mddev->safemode && |
| 7103 | !atomic_read(&mddev->writes_pending) && |
| 7104 | !mddev->in_sync && |
| 7105 | mddev->recovery_cp == MaxSector) { |
| 7106 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7107 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7108 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7109 | } |
| 7110 | if (mddev->safemode == 1) |
| 7111 | mddev->safemode = 0; |
| 7112 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7113 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7114 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7115 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7116 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7117 | if (mddev->flags) |
| 7118 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7121 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7122 | /* resync/recovery still happening */ |
| 7123 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7124 | goto unlock; |
| 7125 | } |
| 7126 | if (mddev->sync_thread) { |
| 7127 | /* resync has finished, collect result */ |
| 7128 | md_unregister_thread(mddev->sync_thread); |
| 7129 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7130 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7131 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | /* success...*/ |
| 7133 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7134 | if (mddev->pers->spare_active(mddev)) |
| 7135 | sysfs_notify(&mddev->kobj, NULL, |
| 7136 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7138 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7139 | mddev->pers->finish_reshape) |
| 7140 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7141 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7142 | |
| 7143 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7144 | * information must be scrapped |
| 7145 | */ |
| 7146 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7147 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7148 | rdev->saved_raid_disk = -1; |
| 7149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | mddev->recovery = 0; |
| 7151 | /* flag recovery needed just to double check */ |
| 7152 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7153 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7154 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | goto unlock; |
| 7156 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7157 | /* Set RUNNING before clearing NEEDED to avoid |
| 7158 | * any transients in the value of "sync_action". |
| 7159 | */ |
| 7160 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7161 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7162 | /* Clear some bits that don't mean anything, but |
| 7163 | * might be left set |
| 7164 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7165 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7166 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7168 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7169 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | /* no recovery is running. |
| 7171 | * remove any failed drives, then |
| 7172 | * add spares if possible. |
| 7173 | * Spare are also removed and re-added, to allow |
| 7174 | * the personality to fail the re-add. |
| 7175 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7176 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7177 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7178 | if (mddev->pers->check_reshape == NULL || |
| 7179 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7180 | /* Cannot proceed */ |
| 7181 | goto unlock; |
| 7182 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7183 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7184 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7185 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7186 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7187 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7188 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7189 | } else if (mddev->recovery_cp < MaxSector) { |
| 7190 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7191 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7192 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7193 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7197 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7198 | /* We are adding a device or devices to an array |
| 7199 | * which has the bitmap stored on all devices. |
| 7200 | * So make sure all bitmap pages get written |
| 7201 | */ |
| 7202 | bitmap_write_all(mddev->bitmap); |
| 7203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7205 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7206 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | if (!mddev->sync_thread) { |
| 7208 | printk(KERN_ERR "%s: could not start resync" |
| 7209 | " thread...\n", |
| 7210 | mdname(mddev)); |
| 7211 | /* leave the spares where they are, it shouldn't hurt */ |
| 7212 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7213 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7215 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7216 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | } |
| 7218 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7219 | if (!mddev->sync_thread) { |
| 7220 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7221 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7222 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7223 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7224 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | mddev_unlock(mddev); |
| 7227 | } |
| 7228 | } |
| 7229 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7230 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7231 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7232 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7233 | wait_event_timeout(rdev->blocked_wait, |
| 7234 | !test_bit(Blocked, &rdev->flags), |
| 7235 | msecs_to_jiffies(5000)); |
| 7236 | rdev_dec_pending(rdev, mddev); |
| 7237 | } |
| 7238 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7239 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7240 | static int md_notify_reboot(struct notifier_block *this, |
| 7241 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | { |
| 7243 | struct list_head *tmp; |
| 7244 | mddev_t *mddev; |
| 7245 | |
| 7246 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7247 | |
| 7248 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7249 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7250 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7251 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7252 | /* Force a switch to readonly even array |
| 7253 | * appears to still be in use. Hence |
| 7254 | * the '100'. |
| 7255 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7256 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7257 | mddev_unlock(mddev); |
| 7258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | /* |
| 7260 | * certain more exotic SCSI devices are known to be |
| 7261 | * volatile wrt too early system reboots. While the |
| 7262 | * right place to handle this issue is the given |
| 7263 | * driver, we do want to have a safe RAID driver ... |
| 7264 | */ |
| 7265 | mdelay(1000*1); |
| 7266 | } |
| 7267 | return NOTIFY_DONE; |
| 7268 | } |
| 7269 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7270 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7271 | .notifier_call = md_notify_reboot, |
| 7272 | .next = NULL, |
| 7273 | .priority = INT_MAX, /* before any real devices */ |
| 7274 | }; |
| 7275 | |
| 7276 | static void md_geninit(void) |
| 7277 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7278 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7279 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7280 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | } |
| 7282 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7283 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7284 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7285 | int ret = -ENOMEM; |
| 7286 | |
| 7287 | md_wq = alloc_workqueue("md", WQ_RESCUER, 0); |
| 7288 | if (!md_wq) |
| 7289 | goto err_wq; |
| 7290 | |
| 7291 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7292 | if (!md_misc_wq) |
| 7293 | goto err_misc_wq; |
| 7294 | |
| 7295 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7296 | goto err_md; |
| 7297 | |
| 7298 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7299 | goto err_mdp; |
| 7300 | mdp_major = ret; |
| 7301 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7302 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7303 | md_probe, NULL, NULL); |
| 7304 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | md_probe, NULL, NULL); |
| 7306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7308 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7309 | |
| 7310 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7311 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7313 | err_mdp: |
| 7314 | unregister_blkdev(MD_MAJOR, "md"); |
| 7315 | err_md: |
| 7316 | destroy_workqueue(md_misc_wq); |
| 7317 | err_misc_wq: |
| 7318 | destroy_workqueue(md_wq); |
| 7319 | err_wq: |
| 7320 | return ret; |
| 7321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | |
| 7323 | #ifndef MODULE |
| 7324 | |
| 7325 | /* |
| 7326 | * Searches all registered partitions for autorun RAID arrays |
| 7327 | * at boot time. |
| 7328 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7329 | |
| 7330 | static LIST_HEAD(all_detected_devices); |
| 7331 | struct detected_devices_node { |
| 7332 | struct list_head list; |
| 7333 | dev_t dev; |
| 7334 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | |
| 7336 | void md_autodetect_dev(dev_t dev) |
| 7337 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7338 | struct detected_devices_node *node_detected_dev; |
| 7339 | |
| 7340 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7341 | if (node_detected_dev) { |
| 7342 | node_detected_dev->dev = dev; |
| 7343 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7344 | } else { |
| 7345 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7346 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | |
| 7351 | static void autostart_arrays(int part) |
| 7352 | { |
| 7353 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7354 | struct detected_devices_node *node_detected_dev; |
| 7355 | dev_t dev; |
| 7356 | int i_scanned, i_passed; |
| 7357 | |
| 7358 | i_scanned = 0; |
| 7359 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7360 | |
| 7361 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7362 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7363 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7364 | i_scanned++; |
| 7365 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7366 | struct detected_devices_node, list); |
| 7367 | list_del(&node_detected_dev->list); |
| 7368 | dev = node_detected_dev->dev; |
| 7369 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7370 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7371 | if (IS_ERR(rdev)) |
| 7372 | continue; |
| 7373 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7374 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7375 | MD_BUG(); |
| 7376 | continue; |
| 7377 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7378 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7379 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7380 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7382 | |
| 7383 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7384 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | |
| 7386 | autorun_devices(part); |
| 7387 | } |
| 7388 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7389 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | |
| 7391 | static __exit void md_exit(void) |
| 7392 | { |
| 7393 | mddev_t *mddev; |
| 7394 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7395 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7396 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7397 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7399 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7400 | unregister_blkdev(mdp_major, "mdp"); |
| 7401 | unregister_reboot_notifier(&md_notifier); |
| 7402 | unregister_sysctl_table(raid_table_header); |
| 7403 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7404 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7406 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7408 | destroy_workqueue(md_misc_wq); |
| 7409 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | } |
| 7411 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7412 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | module_exit(md_exit) |
| 7414 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7415 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7416 | { |
| 7417 | return sprintf(buffer, "%d", start_readonly); |
| 7418 | } |
| 7419 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7420 | { |
| 7421 | char *e; |
| 7422 | int num = simple_strtoul(val, &e, 10); |
| 7423 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7424 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7425 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7426 | } |
| 7427 | return -EINVAL; |
| 7428 | } |
| 7429 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7430 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7431 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7432 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7433 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | EXPORT_SYMBOL(register_md_personality); |
| 7436 | EXPORT_SYMBOL(unregister_md_personality); |
| 7437 | EXPORT_SYMBOL(md_error); |
| 7438 | EXPORT_SYMBOL(md_done_sync); |
| 7439 | EXPORT_SYMBOL(md_write_start); |
| 7440 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | EXPORT_SYMBOL(md_register_thread); |
| 7442 | EXPORT_SYMBOL(md_unregister_thread); |
| 7443 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | EXPORT_SYMBOL(md_check_recovery); |
| 7445 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7446 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7447 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7448 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |