Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
| 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define DEBUG 0 |
| 58 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 64 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pers_lock); |
| 66 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 67 | static void md_print_devices(void); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 70 | static struct workqueue_struct *md_wq; |
| 71 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 73 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 76 | * Default number of read corrections we'll attempt on an rdev |
| 77 | * before ejecting it from the array. We divide the read error |
| 78 | * count by 2 for every hour elapsed between read errors. |
| 79 | */ |
| 80 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .procname = "speed_limit_min", |
| 113 | .data = &sysctl_speed_limit_min, |
| 114 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 115 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 116 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "speed_limit_max", |
| 120 | .data = &sysctl_speed_limit_max, |
| 121 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 122 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 123 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 125 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static ctl_table raid_dir_table[] = { |
| 129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .procname = "raid", |
| 131 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 132 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .child = raid_table, |
| 134 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 135 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static ctl_table raid_root_table[] = { |
| 139 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .procname = "dev", |
| 141 | .maxlen = 0, |
| 142 | .mode = 0555, |
| 143 | .child = raid_dir_table, |
| 144 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 145 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 148 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 150 | static int start_readonly; |
| 151 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 152 | /* bio_clone_mddev |
| 153 | * like bio_clone, but with a local bio set |
| 154 | */ |
| 155 | |
| 156 | static void mddev_bio_destructor(struct bio *bio) |
| 157 | { |
| 158 | mddev_t *mddev, **mddevp; |
| 159 | |
| 160 | mddevp = (void*)bio; |
| 161 | mddev = mddevp[-1]; |
| 162 | |
| 163 | bio_free(bio, mddev->bio_set); |
| 164 | } |
| 165 | |
| 166 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
| 167 | mddev_t *mddev) |
| 168 | { |
| 169 | struct bio *b; |
| 170 | mddev_t **mddevp; |
| 171 | |
| 172 | if (!mddev || !mddev->bio_set) |
| 173 | return bio_alloc(gfp_mask, nr_iovecs); |
| 174 | |
| 175 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, |
| 176 | mddev->bio_set); |
| 177 | if (!b) |
| 178 | return NULL; |
| 179 | mddevp = (void*)b; |
| 180 | mddevp[-1] = mddev; |
| 181 | b->bi_destructor = mddev_bio_destructor; |
| 182 | return b; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 185 | |
| 186 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
| 187 | mddev_t *mddev) |
| 188 | { |
| 189 | struct bio *b; |
| 190 | mddev_t **mddevp; |
| 191 | |
| 192 | if (!mddev || !mddev->bio_set) |
| 193 | return bio_clone(bio, gfp_mask); |
| 194 | |
| 195 | b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, |
| 196 | mddev->bio_set); |
| 197 | if (!b) |
| 198 | return NULL; |
| 199 | mddevp = (void*)b; |
| 200 | mddevp[-1] = mddev; |
| 201 | b->bi_destructor = mddev_bio_destructor; |
| 202 | __bio_clone(b, bio); |
| 203 | if (bio_integrity(bio)) { |
| 204 | int ret; |
| 205 | |
| 206 | ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set); |
| 207 | |
| 208 | if (ret < 0) { |
| 209 | bio_put(b); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return b; |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 219 | * We have a system wide 'event count' that is incremented |
| 220 | * on any 'interesting' event, and readers of /proc/mdstat |
| 221 | * can use 'poll' or 'select' to find out when the event |
| 222 | * count increases. |
| 223 | * |
| 224 | * Events are: |
| 225 | * start array, stop array, error, add device, remove device, |
| 226 | * start build, activate spare |
| 227 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 228 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 229 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 230 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 231 | { |
| 232 | atomic_inc(&md_event_count); |
| 233 | wake_up(&md_event_waiters); |
| 234 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 235 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 236 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 237 | /* Alternate version that can be called from interrupts |
| 238 | * when calling sysfs_notify isn't needed. |
| 239 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 240 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 241 | { |
| 242 | atomic_inc(&md_event_count); |
| 243 | wake_up(&md_event_waiters); |
| 244 | } |
| 245 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 246 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * Enables to iterate over all existing md arrays |
| 248 | * all_mddevs_lock protects this list. |
| 249 | */ |
| 250 | static LIST_HEAD(all_mddevs); |
| 251 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 252 | |
| 253 | |
| 254 | /* |
| 255 | * iterates through all used mddevs in the system. |
| 256 | * We take care to grab the all_mddevs_lock whenever navigating |
| 257 | * the list, and to always hold a refcount when unlocked. |
| 258 | * Any code which breaks out of this loop while own |
| 259 | * a reference to the current mddev and must mddev_put it. |
| 260 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 261 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | \ |
| 263 | for (({ spin_lock(&all_mddevs_lock); \ |
| 264 | tmp = all_mddevs.next; \ |
| 265 | mddev = NULL;}); \ |
| 266 | ({ if (tmp != &all_mddevs) \ |
| 267 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 268 | spin_unlock(&all_mddevs_lock); \ |
| 269 | if (mddev) mddev_put(mddev); \ |
| 270 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 271 | tmp != &all_mddevs;}); \ |
| 272 | ({ spin_lock(&all_mddevs_lock); \ |
| 273 | tmp = tmp->next;}) \ |
| 274 | ) |
| 275 | |
| 276 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 277 | /* Rather than calling directly into the personality make_request function, |
| 278 | * IO requests come here first so that we can check if the device is |
| 279 | * being suspended pending a reconfiguration. |
| 280 | * We hold a refcount over the call to ->make_request. By the time that |
| 281 | * call has finished, the bio has been linked into some internal structure |
| 282 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 283 | */ |
| 284 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 286 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 287 | mddev_t *mddev = q->queuedata; |
| 288 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 289 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 290 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 291 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 292 | if (mddev == NULL || mddev->pers == NULL |
| 293 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 294 | bio_io_error(bio); |
| 295 | return 0; |
| 296 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 297 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 298 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 299 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 300 | DEFINE_WAIT(__wait); |
| 301 | for (;;) { |
| 302 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 303 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 304 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 305 | break; |
| 306 | rcu_read_unlock(); |
| 307 | schedule(); |
| 308 | rcu_read_lock(); |
| 309 | } |
| 310 | finish_wait(&mddev->sb_wait, &__wait); |
| 311 | } |
| 312 | atomic_inc(&mddev->active_io); |
| 313 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 314 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 315 | /* |
| 316 | * save the sectors now since our bio can |
| 317 | * go away inside make_request |
| 318 | */ |
| 319 | sectors = bio_sectors(bio); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 320 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 321 | |
| 322 | cpu = part_stat_lock(); |
| 323 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 324 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 325 | part_stat_unlock(); |
| 326 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 327 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 328 | wake_up(&mddev->sb_wait); |
| 329 | |
| 330 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 333 | /* mddev_suspend makes sure no new requests are submitted |
| 334 | * to the device, and that any requests that have been submitted |
| 335 | * are completely handled. |
| 336 | * Once ->stop is called and completes, the module will be completely |
| 337 | * unused. |
| 338 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 339 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 340 | { |
| 341 | BUG_ON(mddev->suspended); |
| 342 | mddev->suspended = 1; |
| 343 | synchronize_rcu(); |
| 344 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 345 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 346 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 348 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 349 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 350 | { |
| 351 | mddev->suspended = 0; |
| 352 | wake_up(&mddev->sb_wait); |
| 353 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 354 | |
| 355 | md_wakeup_thread(mddev->thread); |
| 356 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 357 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 358 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 359 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 360 | int mddev_congested(mddev_t *mddev, int bits) |
| 361 | { |
| 362 | return mddev->suspended; |
| 363 | } |
| 364 | EXPORT_SYMBOL(mddev_congested); |
| 365 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 366 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | */ |
| 369 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 370 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | { |
| 372 | mdk_rdev_t *rdev = bio->bi_private; |
| 373 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | |
| 375 | rdev_dec_pending(rdev, mddev); |
| 376 | |
| 377 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 378 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 379 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | } |
| 381 | bio_put(bio); |
| 382 | } |
| 383 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 384 | static void md_submit_flush_data(struct work_struct *ws); |
| 385 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 386 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | { |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 388 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | mdk_rdev_t *rdev; |
| 390 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 391 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 392 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 393 | rcu_read_lock(); |
| 394 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 395 | if (rdev->raid_disk >= 0 && |
| 396 | !test_bit(Faulty, &rdev->flags)) { |
| 397 | /* Take two references, one is dropped |
| 398 | * when request finishes, one after |
| 399 | * we reclaim rcu_read_lock |
| 400 | */ |
| 401 | struct bio *bi; |
| 402 | atomic_inc(&rdev->nr_pending); |
| 403 | atomic_inc(&rdev->nr_pending); |
| 404 | rcu_read_unlock(); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 405 | bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 406 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | bi->bi_private = rdev; |
| 408 | bi->bi_bdev = rdev->bdev; |
| 409 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | rcu_read_lock(); |
| 412 | rdev_dec_pending(rdev, mddev); |
| 413 | } |
| 414 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 415 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 416 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | } |
| 418 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 419 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 422 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | /* an empty barrier - all done */ |
| 426 | bio_endio(bio, 0); |
| 427 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 429 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 432 | |
| 433 | mddev->flush_bio = NULL; |
| 434 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | } |
| 436 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 437 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | { |
| 439 | spin_lock_irq(&mddev->write_lock); |
| 440 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 441 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 442 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 443 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | spin_unlock_irq(&mddev->write_lock); |
| 445 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 446 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 447 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 448 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 449 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 450 | |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 451 | /* Support for plugging. |
| 452 | * This mirrors the plugging support in request_queue, but does not |
| 453 | * require having a whole queue or request structures. |
| 454 | * We allocate an md_plug_cb for each md device and each thread it gets |
| 455 | * plugged on. This links tot the private plug_handle structure in the |
| 456 | * personality data where we keep a count of the number of outstanding |
| 457 | * plugs so other code can see if a plug is active. |
| 458 | */ |
| 459 | struct md_plug_cb { |
| 460 | struct blk_plug_cb cb; |
| 461 | mddev_t *mddev; |
| 462 | }; |
| 463 | |
| 464 | static void plugger_unplug(struct blk_plug_cb *cb) |
| 465 | { |
| 466 | struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb); |
| 467 | if (atomic_dec_and_test(&mdcb->mddev->plug_cnt)) |
| 468 | md_wakeup_thread(mdcb->mddev->thread); |
| 469 | kfree(mdcb); |
| 470 | } |
| 471 | |
| 472 | /* Check that an unplug wakeup will come shortly. |
| 473 | * If not, wakeup the md thread immediately |
| 474 | */ |
| 475 | int mddev_check_plugged(mddev_t *mddev) |
| 476 | { |
| 477 | struct blk_plug *plug = current->plug; |
| 478 | struct md_plug_cb *mdcb; |
| 479 | |
| 480 | if (!plug) |
| 481 | return 0; |
| 482 | |
| 483 | list_for_each_entry(mdcb, &plug->cb_list, cb.list) { |
| 484 | if (mdcb->cb.callback == plugger_unplug && |
| 485 | mdcb->mddev == mddev) { |
| 486 | /* Already on the list, move to top */ |
| 487 | if (mdcb != list_first_entry(&plug->cb_list, |
| 488 | struct md_plug_cb, |
| 489 | cb.list)) |
| 490 | list_move(&mdcb->cb.list, &plug->cb_list); |
| 491 | return 1; |
| 492 | } |
| 493 | } |
| 494 | /* Not currently on the callback list */ |
| 495 | mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC); |
| 496 | if (!mdcb) |
| 497 | return 0; |
| 498 | |
| 499 | mdcb->mddev = mddev; |
| 500 | mdcb->cb.callback = plugger_unplug; |
| 501 | atomic_inc(&mddev->plug_cnt); |
| 502 | list_add(&mdcb->cb.list, &plug->cb_list); |
| 503 | return 1; |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(mddev_check_plugged); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 508 | { |
| 509 | atomic_inc(&mddev->active); |
| 510 | return mddev; |
| 511 | } |
| 512 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 513 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | static void mddev_put(mddev_t *mddev) |
| 516 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 517 | struct bio_set *bs = NULL; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 520 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 521 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 522 | mddev->ctime == 0 && !mddev->hold_active) { |
| 523 | /* Array is not configured at all, and not held active, |
| 524 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | list_del(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 526 | bs = mddev->bio_set; |
| 527 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 528 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 529 | /* We did a probe so need to clean up. Call |
| 530 | * queue_work inside the spinlock so that |
| 531 | * flush_workqueue() after mddev_find will |
| 532 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 533 | */ |
| 534 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 535 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 536 | } else |
| 537 | kfree(mddev); |
| 538 | } |
| 539 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 540 | if (bs) |
| 541 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 544 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 545 | { |
| 546 | mutex_init(&mddev->open_mutex); |
| 547 | mutex_init(&mddev->reconfig_mutex); |
| 548 | mutex_init(&mddev->bitmap_info.mutex); |
| 549 | INIT_LIST_HEAD(&mddev->disks); |
| 550 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 551 | init_timer(&mddev->safemode_timer); |
| 552 | atomic_set(&mddev->active, 1); |
| 553 | atomic_set(&mddev->openers, 0); |
| 554 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 555 | atomic_set(&mddev->plug_cnt, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 556 | spin_lock_init(&mddev->write_lock); |
| 557 | atomic_set(&mddev->flush_pending, 0); |
| 558 | init_waitqueue_head(&mddev->sb_wait); |
| 559 | init_waitqueue_head(&mddev->recovery_wait); |
| 560 | mddev->reshape_position = MaxSector; |
| 561 | mddev->resync_min = 0; |
| 562 | mddev->resync_max = MaxSector; |
| 563 | mddev->level = LEVEL_NONE; |
| 564 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 565 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | static mddev_t * mddev_find(dev_t unit) |
| 568 | { |
| 569 | mddev_t *mddev, *new = NULL; |
| 570 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 571 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 572 | unit &= ~((1<<MdpMinorShift)-1); |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | retry: |
| 575 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 577 | if (unit) { |
| 578 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 579 | if (mddev->unit == unit) { |
| 580 | mddev_get(mddev); |
| 581 | spin_unlock(&all_mddevs_lock); |
| 582 | kfree(new); |
| 583 | return mddev; |
| 584 | } |
| 585 | |
| 586 | if (new) { |
| 587 | list_add(&new->all_mddevs, &all_mddevs); |
| 588 | spin_unlock(&all_mddevs_lock); |
| 589 | new->hold_active = UNTIL_IOCTL; |
| 590 | return new; |
| 591 | } |
| 592 | } else if (new) { |
| 593 | /* find an unused unit number */ |
| 594 | static int next_minor = 512; |
| 595 | int start = next_minor; |
| 596 | int is_free = 0; |
| 597 | int dev = 0; |
| 598 | while (!is_free) { |
| 599 | dev = MKDEV(MD_MAJOR, next_minor); |
| 600 | next_minor++; |
| 601 | if (next_minor > MINORMASK) |
| 602 | next_minor = 0; |
| 603 | if (next_minor == start) { |
| 604 | /* Oh dear, all in use. */ |
| 605 | spin_unlock(&all_mddevs_lock); |
| 606 | kfree(new); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | is_free = 1; |
| 611 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 612 | if (mddev->unit == dev) { |
| 613 | is_free = 0; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | new->unit = dev; |
| 618 | new->md_minor = MINOR(dev); |
| 619 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | list_add(&new->all_mddevs, &all_mddevs); |
| 621 | spin_unlock(&all_mddevs_lock); |
| 622 | return new; |
| 623 | } |
| 624 | spin_unlock(&all_mddevs_lock); |
| 625 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 626 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (!new) |
| 628 | return NULL; |
| 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | new->unit = unit; |
| 631 | if (MAJOR(unit) == MD_MAJOR) |
| 632 | new->md_minor = MINOR(unit); |
| 633 | else |
| 634 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 635 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 636 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | goto retry; |
| 639 | } |
| 640 | |
| 641 | static inline int mddev_lock(mddev_t * mddev) |
| 642 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 643 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 646 | static inline int mddev_is_locked(mddev_t *mddev) |
| 647 | { |
| 648 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | static inline int mddev_trylock(mddev_t * mddev) |
| 652 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 653 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 656 | static struct attribute_group md_redundancy_group; |
| 657 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 658 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 660 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 661 | /* These cannot be removed under reconfig_mutex as |
| 662 | * an access to the files will try to take reconfig_mutex |
| 663 | * while holding the file unremovable, which leads to |
| 664 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 665 | * So hold set sysfs_active while the remove in happeing, |
| 666 | * and anything else which might set ->to_remove or my |
| 667 | * otherwise change the sysfs namespace will fail with |
| 668 | * -EBUSY if sysfs_active is still set. |
| 669 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 670 | * test it under the same mutex to ensure its correct value |
| 671 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 672 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 673 | struct attribute_group *to_remove = mddev->to_remove; |
| 674 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 675 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 676 | mutex_unlock(&mddev->reconfig_mutex); |
| 677 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 678 | if (mddev->kobj.sd) { |
| 679 | if (to_remove != &md_redundancy_group) |
| 680 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 681 | if (mddev->pers == NULL || |
| 682 | mddev->pers->sync_request == NULL) { |
| 683 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 684 | if (mddev->sysfs_action) |
| 685 | sysfs_put(mddev->sysfs_action); |
| 686 | mddev->sysfs_action = NULL; |
| 687 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 688 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 689 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 690 | } else |
| 691 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 693 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 696 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 698 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 700 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (rdev->desc_nr == nr) |
| 702 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 708 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | mdk_rdev_t *rdev; |
| 710 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 711 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | if (rdev->bdev->bd_dev == dev) |
| 713 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return NULL; |
| 716 | } |
| 717 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 718 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 719 | { |
| 720 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 721 | list_for_each_entry(pers, &pers_list, list) { |
| 722 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 723 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 724 | if (strcmp(pers->name, clevel)==0) |
| 725 | return pers; |
| 726 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 727 | return NULL; |
| 728 | } |
| 729 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 730 | /* return the offset of the super block in 512byte sectors */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 731 | static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 733 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 734 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 738 | { |
| 739 | if (rdev->sb_page) |
| 740 | MD_BUG(); |
| 741 | |
| 742 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 743 | if (!rdev->sb_page) { |
| 744 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 745 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 752 | { |
| 753 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 754 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | rdev->sb_loaded = 0; |
| 756 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 757 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 758 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 763 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 764 | { |
| 765 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 766 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 767 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 768 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 769 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 770 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 771 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 772 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 773 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 774 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 775 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 776 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 777 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 781 | sector_t sector, int size, struct page *page) |
| 782 | { |
| 783 | /* write first size bytes of page to sector of rdev |
| 784 | * Increment mddev->pending_writes before returning |
| 785 | * and decrement it on completion, waking up sb_wait |
| 786 | * if zero is reached. |
| 787 | * If an error occurred, call md_error |
| 788 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 789 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 790 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 791 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 792 | bio->bi_sector = sector; |
| 793 | bio_add_page(bio, page, size, 0); |
| 794 | bio->bi_private = rdev; |
| 795 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 796 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 797 | atomic_inc(&mddev->pending_writes); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 798 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | void md_super_wait(mddev_t *mddev) |
| 802 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 803 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 804 | DEFINE_WAIT(wq); |
| 805 | for(;;) { |
| 806 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 807 | if (atomic_read(&mddev->pending_writes)==0) |
| 808 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 809 | schedule(); |
| 810 | } |
| 811 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 812 | } |
| 813 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 814 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 819 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 820 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 822 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | struct completion event; |
| 824 | int ret; |
| 825 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 826 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 828 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 829 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 830 | if (metadata_op) |
| 831 | bio->bi_sector = sector + rdev->sb_start; |
| 832 | else |
| 833 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | bio_add_page(bio, page, size, 0); |
| 835 | init_completion(&event); |
| 836 | bio->bi_private = &event; |
| 837 | bio->bi_end_io = bi_complete; |
| 838 | submit_bio(rw, bio); |
| 839 | wait_for_completion(&event); |
| 840 | |
| 841 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 842 | bio_put(bio); |
| 843 | return ret; |
| 844 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 845 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 847 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | { |
| 849 | char b[BDEVNAME_SIZE]; |
| 850 | if (!rdev->sb_page) { |
| 851 | MD_BUG(); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | if (rdev->sb_loaded) |
| 855 | return 0; |
| 856 | |
| 857 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 858 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | goto fail; |
| 860 | rdev->sb_loaded = 1; |
| 861 | return 0; |
| 862 | |
| 863 | fail: |
| 864 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 865 | bdevname(rdev->bdev,b)); |
| 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 870 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 871 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 872 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 873 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 874 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 878 | { |
| 879 | int ret; |
| 880 | mdp_super_t *tmp1, *tmp2; |
| 881 | |
| 882 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 883 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 884 | |
| 885 | if (!tmp1 || !tmp2) { |
| 886 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 887 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | goto abort; |
| 889 | } |
| 890 | |
| 891 | *tmp1 = *sb1; |
| 892 | *tmp2 = *sb2; |
| 893 | |
| 894 | /* |
| 895 | * nr_disks is not constant |
| 896 | */ |
| 897 | tmp1->nr_disks = 0; |
| 898 | tmp2->nr_disks = 0; |
| 899 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 900 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 902 | kfree(tmp1); |
| 903 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | return ret; |
| 905 | } |
| 906 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 907 | |
| 908 | static u32 md_csum_fold(u32 csum) |
| 909 | { |
| 910 | csum = (csum & 0xffff) + (csum >> 16); |
| 911 | return (csum & 0xffff) + (csum >> 16); |
| 912 | } |
| 913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 915 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 916 | u64 newcsum = 0; |
| 917 | u32 *sb32 = (u32*)sb; |
| 918 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | unsigned int disk_csum, csum; |
| 920 | |
| 921 | disk_csum = sb->sb_csum; |
| 922 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 923 | |
| 924 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 925 | newcsum += sb32[i]; |
| 926 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 927 | |
| 928 | |
| 929 | #ifdef CONFIG_ALPHA |
| 930 | /* This used to use csum_partial, which was wrong for several |
| 931 | * reasons including that different results are returned on |
| 932 | * different architectures. It isn't critical that we get exactly |
| 933 | * the same return value as before (we always csum_fold before |
| 934 | * testing, and that removes any differences). However as we |
| 935 | * know that csum_partial always returned a 16bit value on |
| 936 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 937 | */ |
| 938 | sb->sb_csum = md_csum_fold(disk_csum); |
| 939 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 941 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return csum; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | /* |
| 947 | * Handle superblock details. |
| 948 | * We want to be able to handle multiple superblock formats |
| 949 | * so we have a common interface to them all, and an array of |
| 950 | * different handlers. |
| 951 | * We rely on user-space to write the initial superblock, and support |
| 952 | * reading and updating of superblocks. |
| 953 | * Interface methods are: |
| 954 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 955 | * loads and validates a superblock on dev. |
| 956 | * if refdev != NULL, compare superblocks on both devices |
| 957 | * Return: |
| 958 | * 0 - dev has a superblock that is compatible with refdev |
| 959 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 960 | * so dev should be used as the refdev in future |
| 961 | * -EINVAL superblock incompatible or invalid |
| 962 | * -othererror e.g. -EIO |
| 963 | * |
| 964 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 965 | * Verify that dev is acceptable into mddev. |
| 966 | * The first time, mddev->raid_disks will be 0, and data from |
| 967 | * dev should be merged in. Subsequent calls check that dev |
| 968 | * is new enough. Return 0 or -EINVAL |
| 969 | * |
| 970 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 971 | * Update the superblock for rdev with data in mddev |
| 972 | * This does not write to disc. |
| 973 | * |
| 974 | */ |
| 975 | |
| 976 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 977 | char *name; |
| 978 | struct module *owner; |
| 979 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 980 | int minor_version); |
| 981 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 982 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 983 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 984 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | }; |
| 986 | |
| 987 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 988 | * Check that the given mddev has no bitmap. |
| 989 | * |
| 990 | * This function is called from the run method of all personalities that do not |
| 991 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 992 | * has a bitmap. Otherwise, it returns 0. |
| 993 | * |
| 994 | */ |
| 995 | int md_check_no_bitmap(mddev_t *mddev) |
| 996 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 997 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 998 | return 0; |
| 999 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 1000 | mdname(mddev), mddev->pers->name); |
| 1001 | return 1; |
| 1002 | } |
| 1003 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1004 | |
| 1005 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | * load_super for 0.90.0 |
| 1007 | */ |
| 1008 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1009 | { |
| 1010 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1011 | mdp_super_t *sb; |
| 1012 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1015 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | * it's at the end of the disk. |
| 1017 | * |
| 1018 | * It also happens to be a multiple of 4Kb. |
| 1019 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1020 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1022 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (ret) return ret; |
| 1024 | |
| 1025 | ret = -EINVAL; |
| 1026 | |
| 1027 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1028 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1031 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1032 | b); |
| 1033 | goto abort; |
| 1034 | } |
| 1035 | |
| 1036 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1037 | sb->minor_version < 90 || |
| 1038 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1040 | sb->major_version, sb->minor_version, |
| 1041 | b); |
| 1042 | goto abort; |
| 1043 | } |
| 1044 | |
| 1045 | if (sb->raid_disks <= 0) |
| 1046 | goto abort; |
| 1047 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1048 | 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] | 1049 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1050 | b); |
| 1051 | goto abort; |
| 1052 | } |
| 1053 | |
| 1054 | rdev->preferred_minor = sb->md_minor; |
| 1055 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1056 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
| 1058 | if (sb->level == LEVEL_MULTIPATH) |
| 1059 | rdev->desc_nr = -1; |
| 1060 | else |
| 1061 | rdev->desc_nr = sb->this_disk.number; |
| 1062 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1063 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1065 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1067 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if (!uuid_equal(refsb, sb)) { |
| 1069 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1070 | b, bdevname(refdev->bdev,b2)); |
| 1071 | goto abort; |
| 1072 | } |
| 1073 | if (!sb_equal(refsb, sb)) { |
| 1074 | printk(KERN_WARNING "md: %s has same UUID" |
| 1075 | " but different superblock to %s\n", |
| 1076 | b, bdevname(refdev->bdev, b2)); |
| 1077 | goto abort; |
| 1078 | } |
| 1079 | ev1 = md_event(sb); |
| 1080 | ev2 = md_event(refsb); |
| 1081 | if (ev1 > ev2) |
| 1082 | ret = 1; |
| 1083 | else |
| 1084 | ret = 0; |
| 1085 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1086 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1088 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1089 | /* "this cannot possibly happen" ... */ |
| 1090 | ret = -EINVAL; |
| 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | abort: |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * validate_super for 0.90.0 |
| 1098 | */ |
| 1099 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1100 | { |
| 1101 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1102 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1103 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1105 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1106 | clear_bit(Faulty, &rdev->flags); |
| 1107 | clear_bit(In_sync, &rdev->flags); |
| 1108 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (mddev->raid_disks == 0) { |
| 1111 | mddev->major_version = 0; |
| 1112 | mddev->minor_version = sb->minor_version; |
| 1113 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1114 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1115 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | mddev->ctime = sb->ctime; |
| 1117 | mddev->utime = sb->utime; |
| 1118 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1119 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | mddev->layout = sb->layout; |
| 1121 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1122 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1123 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1124 | mddev->bitmap_info.offset = 0; |
| 1125 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1127 | if (mddev->minor_version >= 91) { |
| 1128 | mddev->reshape_position = sb->reshape_position; |
| 1129 | mddev->delta_disks = sb->delta_disks; |
| 1130 | mddev->new_level = sb->new_level; |
| 1131 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1132 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1133 | } else { |
| 1134 | mddev->reshape_position = MaxSector; |
| 1135 | mddev->delta_disks = 0; |
| 1136 | mddev->new_level = mddev->level; |
| 1137 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1138 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1142 | mddev->recovery_cp = MaxSector; |
| 1143 | else { |
| 1144 | if (sb->events_hi == sb->cp_events_hi && |
| 1145 | sb->events_lo == sb->cp_events_lo) { |
| 1146 | mddev->recovery_cp = sb->recovery_cp; |
| 1147 | } else |
| 1148 | mddev->recovery_cp = 0; |
| 1149 | } |
| 1150 | |
| 1151 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1152 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1153 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1154 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1155 | |
| 1156 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1157 | |
| 1158 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1159 | mddev->bitmap_info.file == NULL) |
| 1160 | mddev->bitmap_info.offset = |
| 1161 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1162 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1163 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1164 | /* Insist on good event counter while assembling, except |
| 1165 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1167 | if (sb->disks[rdev->desc_nr].state & ( |
| 1168 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1169 | if (ev1 < mddev->events) |
| 1170 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1171 | } else if (mddev->bitmap) { |
| 1172 | /* if adding to array with a bitmap, then we can accept an |
| 1173 | * older device ... but not too old. |
| 1174 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1175 | if (ev1 < mddev->bitmap->events_cleared) |
| 1176 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1177 | } else { |
| 1178 | if (ev1 < mddev->events) |
| 1179 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1180 | return 0; |
| 1181 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | desc = sb->disks + rdev->desc_nr; |
| 1185 | |
| 1186 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1187 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1188 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1189 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1190 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1192 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1193 | /* active but not in sync implies recovery up to |
| 1194 | * reshape position. We don't know exactly where |
| 1195 | * that is, so set to zero for now */ |
| 1196 | if (mddev->minor_version >= 91) { |
| 1197 | rdev->recovery_offset = 0; |
| 1198 | rdev->raid_disk = desc->raid_disk; |
| 1199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1201 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1202 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1203 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1204 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * sync_super for 0.90.0 |
| 1210 | */ |
| 1211 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1212 | { |
| 1213 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | mdk_rdev_t *rdev2; |
| 1215 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | /* make rdev->sb match mddev data.. |
| 1219 | * |
| 1220 | * 1/ zero out disks |
| 1221 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1222 | * 3/ any empty disks < next_spare become removed |
| 1223 | * |
| 1224 | * disks[0] gets initialised to REMOVED because |
| 1225 | * we cannot be sure from other fields if it has |
| 1226 | * been initialised or not. |
| 1227 | */ |
| 1228 | int i; |
| 1229 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1230 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1231 | rdev->sb_size = MD_SB_BYTES; |
| 1232 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1233 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
| 1235 | memset(sb, 0, sizeof(*sb)); |
| 1236 | |
| 1237 | sb->md_magic = MD_SB_MAGIC; |
| 1238 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | sb->patch_version = mddev->patch_version; |
| 1240 | sb->gvalid_words = 0; /* ignored */ |
| 1241 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1242 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1243 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1244 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1245 | |
| 1246 | sb->ctime = mddev->ctime; |
| 1247 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1248 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | sb->raid_disks = mddev->raid_disks; |
| 1250 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1251 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | sb->utime = mddev->utime; |
| 1253 | sb->state = 0; |
| 1254 | sb->events_hi = (mddev->events>>32); |
| 1255 | sb->events_lo = (u32)mddev->events; |
| 1256 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1257 | if (mddev->reshape_position == MaxSector) |
| 1258 | sb->minor_version = 90; |
| 1259 | else { |
| 1260 | sb->minor_version = 91; |
| 1261 | sb->reshape_position = mddev->reshape_position; |
| 1262 | sb->new_level = mddev->new_level; |
| 1263 | sb->delta_disks = mddev->delta_disks; |
| 1264 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1265 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1266 | } |
| 1267 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (mddev->in_sync) |
| 1269 | { |
| 1270 | sb->recovery_cp = mddev->recovery_cp; |
| 1271 | sb->cp_events_hi = (mddev->events>>32); |
| 1272 | sb->cp_events_lo = (u32)mddev->events; |
| 1273 | if (mddev->recovery_cp == MaxSector) |
| 1274 | sb->state = (1<< MD_SB_CLEAN); |
| 1275 | } else |
| 1276 | sb->recovery_cp = 0; |
| 1277 | |
| 1278 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1279 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1281 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1282 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1285 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1287 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1288 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1289 | |
| 1290 | if (rdev2->raid_disk >= 0 && |
| 1291 | sb->minor_version >= 91) |
| 1292 | /* we have nowhere to store the recovery_offset, |
| 1293 | * but if it is not below the reshape_position, |
| 1294 | * we can piggy-back on that. |
| 1295 | */ |
| 1296 | is_active = 1; |
| 1297 | if (rdev2->raid_disk < 0 || |
| 1298 | test_bit(Faulty, &rdev2->flags)) |
| 1299 | is_active = 0; |
| 1300 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1301 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1303 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1304 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | d = &sb->disks[rdev2->desc_nr]; |
| 1306 | nr_disks++; |
| 1307 | d->number = rdev2->desc_nr; |
| 1308 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1309 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1310 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | d->raid_disk = rdev2->raid_disk; |
| 1312 | else |
| 1313 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1314 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1316 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1318 | if (test_bit(In_sync, &rdev2->flags)) |
| 1319 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | active++; |
| 1321 | working++; |
| 1322 | } else { |
| 1323 | d->state = 0; |
| 1324 | spare++; |
| 1325 | working++; |
| 1326 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1327 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1328 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1331 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1332 | mdp_disk_t *d = &sb->disks[i]; |
| 1333 | if (d->state == 0 && d->number == 0) { |
| 1334 | d->number = i; |
| 1335 | d->raid_disk = i; |
| 1336 | d->state = (1<<MD_DISK_REMOVED); |
| 1337 | d->state |= (1<<MD_DISK_FAULTY); |
| 1338 | failed++; |
| 1339 | } |
| 1340 | } |
| 1341 | sb->nr_disks = nr_disks; |
| 1342 | sb->active_disks = active; |
| 1343 | sb->working_disks = working; |
| 1344 | sb->failed_disks = failed; |
| 1345 | sb->spare_disks = spare; |
| 1346 | |
| 1347 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1348 | sb->sb_csum = calc_sb_csum(sb); |
| 1349 | } |
| 1350 | |
| 1351 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1352 | * rdev_size_change for 0.90.0 |
| 1353 | */ |
| 1354 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1355 | 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] | 1356 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1357 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1358 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1359 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1360 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1361 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1362 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1363 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | rdev->sb_page); |
| 1366 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1367 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | * version 1 superblock |
| 1373 | */ |
| 1374 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1375 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1377 | __le32 disk_csum; |
| 1378 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | unsigned long long newcsum; |
| 1380 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1381 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | int i; |
| 1383 | |
| 1384 | disk_csum = sb->sb_csum; |
| 1385 | sb->sb_csum = 0; |
| 1386 | newcsum = 0; |
| 1387 | for (i=0; size>=4; size -= 4 ) |
| 1388 | newcsum += le32_to_cpu(*isuper++); |
| 1389 | |
| 1390 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1391 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
| 1393 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1394 | sb->sb_csum = disk_csum; |
| 1395 | return cpu_to_le32(csum); |
| 1396 | } |
| 1397 | |
| 1398 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1399 | { |
| 1400 | struct mdp_superblock_1 *sb; |
| 1401 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1402 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1404 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | |
| 1406 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1407 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | * It is always aligned to a 4K boundary and |
| 1409 | * depeding on minor_version, it can be: |
| 1410 | * 0: At least 8K, but less than 12K, from end of device |
| 1411 | * 1: At start of device |
| 1412 | * 2: 4K from start of device. |
| 1413 | */ |
| 1414 | switch(minor_version) { |
| 1415 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1416 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1417 | sb_start -= 8*2; |
| 1418 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | break; |
| 1420 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1421 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | break; |
| 1423 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1424 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | break; |
| 1426 | default: |
| 1427 | return -EINVAL; |
| 1428 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1429 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1431 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1432 | * and it is safe to read 4k, so we do that |
| 1433 | */ |
| 1434 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | if (ret) return ret; |
| 1436 | |
| 1437 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1438 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | |
| 1440 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1441 | sb->major_version != cpu_to_le32(1) || |
| 1442 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1443 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1444 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | return -EINVAL; |
| 1446 | |
| 1447 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1448 | printk("md: invalid superblock checksum on %s\n", |
| 1449 | bdevname(rdev->bdev,b)); |
| 1450 | return -EINVAL; |
| 1451 | } |
| 1452 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1453 | printk("md: data_size too small on %s\n", |
| 1454 | bdevname(rdev->bdev,b)); |
| 1455 | return -EINVAL; |
| 1456 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | rdev->preferred_minor = 0xffff; |
| 1459 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1460 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1462 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1463 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1464 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1465 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1466 | |
| 1467 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1468 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1469 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1470 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1471 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1472 | rdev->desc_nr = -1; |
| 1473 | else |
| 1474 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1475 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1476 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1477 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1478 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1480 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | |
| 1482 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1483 | sb->level != refsb->level || |
| 1484 | sb->layout != refsb->layout || |
| 1485 | sb->chunksize != refsb->chunksize) { |
| 1486 | printk(KERN_WARNING "md: %s has strangely different" |
| 1487 | " superblock to %s\n", |
| 1488 | bdevname(rdev->bdev,b), |
| 1489 | bdevname(refdev->bdev,b2)); |
| 1490 | return -EINVAL; |
| 1491 | } |
| 1492 | ev1 = le64_to_cpu(sb->events); |
| 1493 | ev2 = le64_to_cpu(refsb->events); |
| 1494 | |
| 1495 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1496 | ret = 1; |
| 1497 | else |
| 1498 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1500 | if (minor_version) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1501 | rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1502 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1504 | rdev->sectors = rdev->sb_start; |
| 1505 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1507 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1508 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1509 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1510 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1514 | { |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1515 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1516 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1518 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1519 | clear_bit(Faulty, &rdev->flags); |
| 1520 | clear_bit(In_sync, &rdev->flags); |
| 1521 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | if (mddev->raid_disks == 0) { |
| 1524 | mddev->major_version = 1; |
| 1525 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1526 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1527 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1529 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1530 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1531 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | mddev->layout = le32_to_cpu(sb->layout); |
| 1533 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1534 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1535 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1536 | mddev->bitmap_info.offset = 0; |
| 1537 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1540 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1541 | |
| 1542 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1543 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1544 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1545 | mddev->bitmap_info.file == NULL ) |
| 1546 | mddev->bitmap_info.offset = |
| 1547 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1548 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1549 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1550 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1551 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1552 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1553 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1554 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1555 | } else { |
| 1556 | mddev->reshape_position = MaxSector; |
| 1557 | mddev->delta_disks = 0; |
| 1558 | mddev->new_level = mddev->level; |
| 1559 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1560 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1561 | } |
| 1562 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1563 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1564 | /* Insist of good event counter while assembling, except for |
| 1565 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1567 | if (rdev->desc_nr >= 0 && |
| 1568 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1569 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1570 | if (ev1 < mddev->events) |
| 1571 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1572 | } else if (mddev->bitmap) { |
| 1573 | /* If adding to array with a bitmap, then we can accept an |
| 1574 | * older device, but not too old. |
| 1575 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1576 | if (ev1 < mddev->bitmap->events_cleared) |
| 1577 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1578 | } else { |
| 1579 | if (ev1 < mddev->events) |
| 1580 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1581 | return 0; |
| 1582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1584 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1585 | if (rdev->desc_nr < 0 || |
| 1586 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1587 | role = 0xffff; |
| 1588 | rdev->desc_nr = -1; |
| 1589 | } else |
| 1590 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | switch(role) { |
| 1592 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | break; |
| 1594 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1595 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | break; |
| 1597 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1598 | if ((le32_to_cpu(sb->feature_map) & |
| 1599 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1600 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1601 | else |
| 1602 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | rdev->raid_disk = role; |
| 1604 | break; |
| 1605 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1606 | if (sb->devflags & WriteMostly1) |
| 1607 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1608 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1609 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1615 | { |
| 1616 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | mdk_rdev_t *rdev2; |
| 1618 | int max_dev, i; |
| 1619 | /* make rdev->sb match mddev and rdev data. */ |
| 1620 | |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1621 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | sb->feature_map = 0; |
| 1624 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1625 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1627 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1628 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1629 | |
| 1630 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1631 | sb->events = cpu_to_le64(mddev->events); |
| 1632 | if (mddev->in_sync) |
| 1633 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1634 | else |
| 1635 | sb->resync_offset = cpu_to_le64(0); |
| 1636 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1637 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1638 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1639 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1640 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1641 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1642 | sb->level = cpu_to_le32(mddev->level); |
| 1643 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1644 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1645 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1646 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1647 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1648 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1649 | |
| 1650 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1651 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1652 | sb->feature_map |= |
| 1653 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1654 | sb->recovery_offset = |
| 1655 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1658 | if (mddev->reshape_position != MaxSector) { |
| 1659 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1660 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1661 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1662 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1663 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1664 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1665 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1668 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | if (rdev2->desc_nr+1 > max_dev) |
| 1670 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1671 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1672 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1673 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1674 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1675 | rdev->sb_size = max_dev * 2 + 256; |
| 1676 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1677 | if (rdev->sb_size & bmask) |
| 1678 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1679 | } else |
| 1680 | max_dev = le32_to_cpu(sb->max_dev); |
| 1681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | for (i=0; i<max_dev;i++) |
| 1683 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1684 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1685 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1687 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1689 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1691 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1692 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | else |
| 1694 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1695 | } |
| 1696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1698 | } |
| 1699 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1700 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1701 | 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] | 1702 | { |
| 1703 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1704 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1705 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1706 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1707 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1708 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1709 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1710 | max_sectors -= rdev->data_offset; |
| 1711 | if (!num_sectors || num_sectors > max_sectors) |
| 1712 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1713 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1714 | /* minor version 0 with bitmap we can't move */ |
| 1715 | return 0; |
| 1716 | } else { |
| 1717 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1718 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1719 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1720 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1721 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1722 | if (!num_sectors || num_sectors > max_sectors) |
| 1723 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1724 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1725 | } |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 1726 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1727 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1728 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1729 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1730 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1731 | rdev->sb_page); |
| 1732 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1733 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1734 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1736 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | [0] = { |
| 1738 | .name = "0.90.0", |
| 1739 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1740 | .load_super = super_90_load, |
| 1741 | .validate_super = super_90_validate, |
| 1742 | .sync_super = super_90_sync, |
| 1743 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | }, |
| 1745 | [1] = { |
| 1746 | .name = "md-1", |
| 1747 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1748 | .load_super = super_1_load, |
| 1749 | .validate_super = super_1_validate, |
| 1750 | .sync_super = super_1_sync, |
| 1751 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | }, |
| 1753 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1755 | static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1756 | { |
| 1757 | if (mddev->sync_super) { |
| 1758 | mddev->sync_super(mddev, rdev); |
| 1759 | return; |
| 1760 | } |
| 1761 | |
| 1762 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1763 | |
| 1764 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1765 | } |
| 1766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1768 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1769 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1771 | rcu_read_lock(); |
| 1772 | rdev_for_each_rcu(rdev, mddev1) |
| 1773 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1774 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1775 | rdev2->bdev->bd_contains) { |
| 1776 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1777 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1778 | } |
| 1779 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | return 0; |
| 1781 | } |
| 1782 | |
| 1783 | static LIST_HEAD(pending_raid_disks); |
| 1784 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1785 | /* |
| 1786 | * Try to register data integrity profile for an mddev |
| 1787 | * |
| 1788 | * This is called when an array is started and after a disk has been kicked |
| 1789 | * from the array. It only succeeds if all working and active component devices |
| 1790 | * are integrity capable with matching profiles. |
| 1791 | */ |
| 1792 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1793 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1794 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1795 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1796 | if (list_empty(&mddev->disks)) |
| 1797 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1798 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1799 | return 0; /* shouldn't register, or already is */ |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1800 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1801 | /* skip spares and non-functional disks */ |
| 1802 | if (test_bit(Faulty, &rdev->flags)) |
| 1803 | continue; |
| 1804 | if (rdev->raid_disk < 0) |
| 1805 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1806 | if (!reference) { |
| 1807 | /* Use the first rdev as the reference */ |
| 1808 | reference = rdev; |
| 1809 | continue; |
| 1810 | } |
| 1811 | /* does this rdev's profile match the reference profile? */ |
| 1812 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1813 | rdev->bdev->bd_disk) < 0) |
| 1814 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1815 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1816 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1817 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1818 | /* |
| 1819 | * All component devices are integrity capable and have matching |
| 1820 | * profiles, register the common profile for the md device. |
| 1821 | */ |
| 1822 | if (blk_integrity_register(mddev->gendisk, |
| 1823 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1824 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1825 | mdname(mddev)); |
| 1826 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1827 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1828 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1829 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1830 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1831 | mdname(mddev)); |
| 1832 | return -EINVAL; |
| 1833 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1834 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1835 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1836 | EXPORT_SYMBOL(md_integrity_register); |
| 1837 | |
| 1838 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1839 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1840 | { |
| 1841 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1842 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1843 | |
| 1844 | if (!bi_mddev) /* nothing to do */ |
| 1845 | return; |
| 1846 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1847 | return; |
| 1848 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1849 | rdev->bdev->bd_disk) >= 0) |
| 1850 | return; |
| 1851 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1852 | blk_integrity_unregister(mddev->gendisk); |
| 1853 | } |
| 1854 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1857 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1858 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1859 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1860 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1861 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
| 1863 | if (rdev->mddev) { |
| 1864 | MD_BUG(); |
| 1865 | return -EINVAL; |
| 1866 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1867 | |
| 1868 | /* prevent duplicates */ |
| 1869 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1870 | return -EEXIST; |
| 1871 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1872 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1873 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1874 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1875 | if (mddev->pers) { |
| 1876 | /* Cannot change size, so fail |
| 1877 | * If mddev->level <= 0, then we don't care |
| 1878 | * about aligning sizes (e.g. linear) |
| 1879 | */ |
| 1880 | if (mddev->level > 0) |
| 1881 | return -ENOSPC; |
| 1882 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1883 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | |
| 1886 | /* Verify rdev->desc_nr is unique. |
| 1887 | * If it is -1, assign a free number, else |
| 1888 | * check number is not in use |
| 1889 | */ |
| 1890 | if (rdev->desc_nr < 0) { |
| 1891 | int choice = 0; |
| 1892 | if (mddev->pers) choice = mddev->raid_disks; |
| 1893 | while (find_rdev_nr(mddev, choice)) |
| 1894 | choice++; |
| 1895 | rdev->desc_nr = choice; |
| 1896 | } else { |
| 1897 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1898 | return -EBUSY; |
| 1899 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1900 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1901 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1902 | mdname(mddev), mddev->max_disks); |
| 1903 | return -EBUSY; |
| 1904 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1905 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1906 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1907 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1910 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1911 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1912 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1913 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1914 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1915 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1916 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1917 | /* failure here is OK */; |
| 1918 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1919 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1920 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1921 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1922 | |
| 1923 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1924 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1927 | |
| 1928 | fail: |
| 1929 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1930 | b, mdname(mddev)); |
| 1931 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1934 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1935 | { |
| 1936 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1937 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1938 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1939 | } |
| 1940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1942 | { |
| 1943 | char b[BDEVNAME_SIZE]; |
| 1944 | if (!rdev->mddev) { |
| 1945 | MD_BUG(); |
| 1946 | return; |
| 1947 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 1948 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1949 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1951 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1952 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1953 | sysfs_put(rdev->sysfs_state); |
| 1954 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1955 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1956 | * writing to 'remove' to "dev/state". We also need |
| 1957 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1958 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1959 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1960 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1961 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1962 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * prevent the device from being mounted, repartitioned or |
| 1967 | * otherwise reused by a RAID array (or any other kernel |
| 1968 | * subsystem), by bd_claiming the device. |
| 1969 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1970 | 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] | 1971 | { |
| 1972 | int err = 0; |
| 1973 | struct block_device *bdev; |
| 1974 | char b[BDEVNAME_SIZE]; |
| 1975 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1976 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
| 1977 | shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | if (IS_ERR(bdev)) { |
| 1979 | printk(KERN_ERR "md: could not open %s.\n", |
| 1980 | __bdevname(dev, b)); |
| 1981 | return PTR_ERR(bdev); |
| 1982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | rdev->bdev = bdev; |
| 1984 | return err; |
| 1985 | } |
| 1986 | |
| 1987 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1988 | { |
| 1989 | struct block_device *bdev = rdev->bdev; |
| 1990 | rdev->bdev = NULL; |
| 1991 | if (!bdev) |
| 1992 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1993 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | void md_autodetect_dev(dev_t dev); |
| 1997 | |
| 1998 | static void export_rdev(mdk_rdev_t * rdev) |
| 1999 | { |
| 2000 | char b[BDEVNAME_SIZE]; |
| 2001 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2002 | bdevname(rdev->bdev,b)); |
| 2003 | if (rdev->mddev) |
| 2004 | MD_BUG(); |
| 2005 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2007 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2008 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | #endif |
| 2010 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2011 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 2015 | { |
| 2016 | unbind_rdev_from_array(rdev); |
| 2017 | export_rdev(rdev); |
| 2018 | } |
| 2019 | |
| 2020 | static void export_array(mddev_t *mddev) |
| 2021 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2022 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2024 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | if (!rdev->mddev) { |
| 2026 | MD_BUG(); |
| 2027 | continue; |
| 2028 | } |
| 2029 | kick_rdev_from_array(rdev); |
| 2030 | } |
| 2031 | if (!list_empty(&mddev->disks)) |
| 2032 | MD_BUG(); |
| 2033 | mddev->raid_disks = 0; |
| 2034 | mddev->major_version = 0; |
| 2035 | } |
| 2036 | |
| 2037 | static void print_desc(mdp_disk_t *desc) |
| 2038 | { |
| 2039 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2040 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2041 | } |
| 2042 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2043 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | { |
| 2045 | int i; |
| 2046 | |
| 2047 | printk(KERN_INFO |
| 2048 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2049 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2050 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2051 | sb->ctime); |
| 2052 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2053 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2054 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2055 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2056 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2057 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2058 | sb->failed_disks, sb->spare_disks, |
| 2059 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2060 | |
| 2061 | printk(KERN_INFO); |
| 2062 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2063 | mdp_disk_t *desc; |
| 2064 | |
| 2065 | desc = sb->disks + i; |
| 2066 | if (desc->number || desc->major || desc->minor || |
| 2067 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2068 | printk(" D %2d: ", i); |
| 2069 | print_desc(desc); |
| 2070 | } |
| 2071 | } |
| 2072 | printk(KERN_INFO "md: THIS: "); |
| 2073 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2076 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2077 | { |
| 2078 | __u8 *uuid; |
| 2079 | |
| 2080 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2081 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2082 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2083 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2084 | le32_to_cpu(sb->major_version), |
| 2085 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2086 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2087 | sb->set_name, |
| 2088 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2089 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2090 | |
| 2091 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2092 | printk(KERN_INFO |
| 2093 | "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] | 2094 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2095 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2096 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2097 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2098 | le32_to_cpu(sb->level), |
| 2099 | (unsigned long long)le64_to_cpu(sb->size), |
| 2100 | le32_to_cpu(sb->raid_disks), |
| 2101 | le32_to_cpu(sb->layout), |
| 2102 | le32_to_cpu(sb->chunksize), |
| 2103 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2104 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2105 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2106 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2107 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2108 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2109 | sb->devflags, |
| 2110 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2111 | (unsigned long long)le64_to_cpu(sb->events), |
| 2112 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2113 | le32_to_cpu(sb->sb_csum), |
| 2114 | le32_to_cpu(sb->max_dev) |
| 2115 | ); |
| 2116 | } |
| 2117 | |
| 2118 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | { |
| 2120 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2121 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2122 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2123 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2124 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2126 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2127 | switch (major_version) { |
| 2128 | case 0: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 2129 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2130 | break; |
| 2131 | case 1: |
Namhyung Kim | 65a06f0 | 2011-07-27 11:00:36 +1000 | [diff] [blame^] | 2132 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2133 | break; |
| 2134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | } else |
| 2136 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2137 | } |
| 2138 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2139 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2141 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | mdk_rdev_t *rdev; |
| 2143 | mddev_t *mddev; |
| 2144 | char b[BDEVNAME_SIZE]; |
| 2145 | |
| 2146 | printk("\n"); |
| 2147 | printk("md: **********************************\n"); |
| 2148 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2149 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2150 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2152 | if (mddev->bitmap) |
| 2153 | bitmap_print_sb(mddev->bitmap); |
| 2154 | else |
| 2155 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2156 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2158 | printk("\n"); |
| 2159 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2160 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2161 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | } |
| 2163 | printk("md: **********************************\n"); |
| 2164 | printk("\n"); |
| 2165 | } |
| 2166 | |
| 2167 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2168 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2170 | /* Update each superblock (in-memory image), but |
| 2171 | * if we are allowed to, skip spares which already |
| 2172 | * have the right event counter, or have one earlier |
| 2173 | * (which would mean they aren't being marked as dirty |
| 2174 | * with the rest of the array) |
| 2175 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2177 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2178 | if (rdev->sb_events == mddev->events || |
| 2179 | (nospares && |
| 2180 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2181 | rdev->sb_events+1 == mddev->events)) { |
| 2182 | /* Don't update this superblock */ |
| 2183 | rdev->sb_loaded = 2; |
| 2184 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2185 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2186 | rdev->sb_loaded = 1; |
| 2187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2191 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2194 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2195 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2198 | /* First make sure individual recovery_offsets are correct */ |
| 2199 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2200 | if (rdev->raid_disk >= 0 && |
| 2201 | mddev->delta_disks >= 0 && |
| 2202 | !test_bit(In_sync, &rdev->flags) && |
| 2203 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2204 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2205 | |
| 2206 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2207 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2208 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2209 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2210 | if (!mddev->external) |
| 2211 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2212 | wake_up(&mddev->sb_wait); |
| 2213 | return; |
| 2214 | } |
| 2215 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2216 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2217 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2218 | mddev->utime = get_seconds(); |
| 2219 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2220 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2221 | force_change = 1; |
| 2222 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2223 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2224 | * though if events isn't the right even/odd, we will have to do |
| 2225 | * spares after all |
| 2226 | */ |
| 2227 | nospares = 1; |
| 2228 | if (force_change) |
| 2229 | nospares = 0; |
| 2230 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2231 | /* If the array is degraded, then skipping spares is both |
| 2232 | * dangerous and fairly pointless. |
| 2233 | * Dangerous because a device that was removed from the array |
| 2234 | * might have a event_count that still looks up-to-date, |
| 2235 | * so it can be re-added without a resync. |
| 2236 | * Pointless because if there are any spares to skip, |
| 2237 | * then a recovery will happen and soon that array won't |
| 2238 | * be degraded any more and the spare can go back to sleep then. |
| 2239 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2240 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2241 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2242 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2243 | |
| 2244 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2245 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2246 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2247 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2248 | && mddev->can_decrease_events |
| 2249 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2250 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2251 | mddev->can_decrease_events = 0; |
| 2252 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2253 | /* otherwise we have to go forward and ... */ |
| 2254 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2255 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | |
| 2258 | if (!mddev->events) { |
| 2259 | /* |
| 2260 | * oops, this 64-bit counter should never wrap. |
| 2261 | * Either we are in around ~1 trillion A.C., assuming |
| 2262 | * 1 reboot per second, or we have a bug: |
| 2263 | */ |
| 2264 | MD_BUG(); |
| 2265 | mddev->events --; |
| 2266 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2267 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2268 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | |
| 2270 | dprintk(KERN_INFO |
| 2271 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2272 | mdname(mddev),mddev->in_sync); |
| 2273 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2274 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2275 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | char b[BDEVNAME_SIZE]; |
| 2277 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2278 | if (rdev->sb_loaded != 1) |
| 2279 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2280 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | dprintk("(skipping faulty "); |
| 2282 | |
| 2283 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2284 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2285 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2286 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2287 | rdev->sb_page); |
| 2288 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2289 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2290 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2291 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | } else |
| 2294 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2295 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | /* only need to write one superblock... */ |
| 2297 | break; |
| 2298 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2299 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2300 | /* 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] | 2301 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2302 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2303 | if (mddev->in_sync != sync_req || |
| 2304 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2305 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2306 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2307 | goto repeat; |
| 2308 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2309 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2310 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2311 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2312 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2313 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2317 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2318 | * We want to accept with case. For this we use cmd_match. |
| 2319 | */ |
| 2320 | static int cmd_match(const char *cmd, const char *str) |
| 2321 | { |
| 2322 | /* See if cmd, written into a sysfs file, matches |
| 2323 | * str. They must either be the same, or cmd can |
| 2324 | * have a trailing newline |
| 2325 | */ |
| 2326 | while (*cmd && *str && *cmd == *str) { |
| 2327 | cmd++; |
| 2328 | str++; |
| 2329 | } |
| 2330 | if (*cmd == '\n') |
| 2331 | cmd++; |
| 2332 | if (*str || *cmd) |
| 2333 | return 0; |
| 2334 | return 1; |
| 2335 | } |
| 2336 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2337 | struct rdev_sysfs_entry { |
| 2338 | struct attribute attr; |
| 2339 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2340 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2341 | }; |
| 2342 | |
| 2343 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2344 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2345 | { |
| 2346 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2347 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2348 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2349 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2350 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2351 | sep = ","; |
| 2352 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2353 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2354 | len += sprintf(page+len, "%sin_sync",sep); |
| 2355 | sep = ","; |
| 2356 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2357 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2358 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2359 | sep = ","; |
| 2360 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2361 | if (test_bit(Blocked, &rdev->flags)) { |
| 2362 | len += sprintf(page+len, "%sblocked", sep); |
| 2363 | sep = ","; |
| 2364 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2365 | if (!test_bit(Faulty, &rdev->flags) && |
| 2366 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2367 | len += sprintf(page+len, "%sspare", sep); |
| 2368 | sep = ","; |
| 2369 | } |
| 2370 | return len+sprintf(page+len, "\n"); |
| 2371 | } |
| 2372 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2373 | static ssize_t |
| 2374 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2375 | { |
| 2376 | /* can write |
| 2377 | * faulty - simulates and error |
| 2378 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2379 | * writemostly - sets write_mostly |
| 2380 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2381 | * blocked - sets the Blocked flag |
| 2382 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2383 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2384 | */ |
| 2385 | int err = -EINVAL; |
| 2386 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2387 | md_error(rdev->mddev, rdev); |
| 2388 | err = 0; |
| 2389 | } else if (cmd_match(buf, "remove")) { |
| 2390 | if (rdev->raid_disk >= 0) |
| 2391 | err = -EBUSY; |
| 2392 | else { |
| 2393 | mddev_t *mddev = rdev->mddev; |
| 2394 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2395 | if (mddev->pers) |
| 2396 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2397 | md_new_event(mddev); |
| 2398 | err = 0; |
| 2399 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2400 | } else if (cmd_match(buf, "writemostly")) { |
| 2401 | set_bit(WriteMostly, &rdev->flags); |
| 2402 | err = 0; |
| 2403 | } else if (cmd_match(buf, "-writemostly")) { |
| 2404 | clear_bit(WriteMostly, &rdev->flags); |
| 2405 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2406 | } else if (cmd_match(buf, "blocked")) { |
| 2407 | set_bit(Blocked, &rdev->flags); |
| 2408 | err = 0; |
| 2409 | } else if (cmd_match(buf, "-blocked")) { |
| 2410 | clear_bit(Blocked, &rdev->flags); |
| 2411 | wake_up(&rdev->blocked_wait); |
| 2412 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2413 | md_wakeup_thread(rdev->mddev->thread); |
| 2414 | |
| 2415 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2416 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2417 | set_bit(In_sync, &rdev->flags); |
| 2418 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2419 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2420 | if (!err) |
| 2421 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2422 | return err ? err : len; |
| 2423 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2424 | static struct rdev_sysfs_entry rdev_state = |
| 2425 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2426 | |
| 2427 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2428 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2429 | { |
| 2430 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2431 | } |
| 2432 | |
| 2433 | static ssize_t |
| 2434 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2435 | { |
| 2436 | char *e; |
| 2437 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2438 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2439 | atomic_set(&rdev->corrected_errors, n); |
| 2440 | return len; |
| 2441 | } |
| 2442 | return -EINVAL; |
| 2443 | } |
| 2444 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2445 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2446 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2447 | static ssize_t |
| 2448 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2449 | { |
| 2450 | if (rdev->raid_disk < 0) |
| 2451 | return sprintf(page, "none\n"); |
| 2452 | else |
| 2453 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2454 | } |
| 2455 | |
| 2456 | static ssize_t |
| 2457 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2458 | { |
| 2459 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2460 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2461 | int slot = simple_strtoul(buf, &e, 10); |
| 2462 | if (strncmp(buf, "none", 4)==0) |
| 2463 | slot = -1; |
| 2464 | else if (e==buf || (*e && *e!= '\n')) |
| 2465 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2466 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2467 | /* Setting 'slot' on an active array requires also |
| 2468 | * updating the 'rd%d' link, and communicating |
| 2469 | * with the personality with ->hot_*_disk. |
| 2470 | * For now we only support removing |
| 2471 | * failed/spare devices. This normally happens automatically, |
| 2472 | * but not when the metadata is externally managed. |
| 2473 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2474 | if (rdev->raid_disk == -1) |
| 2475 | return -EEXIST; |
| 2476 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2477 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2478 | return -EINVAL; |
| 2479 | err = rdev->mddev->pers-> |
| 2480 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2481 | if (err) |
| 2482 | return err; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2483 | sysfs_unlink_rdev(rdev->mddev, rdev); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2484 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2485 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2486 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2487 | } else if (rdev->mddev->pers) { |
| 2488 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2489 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2490 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2491 | */ |
| 2492 | |
| 2493 | if (rdev->raid_disk != -1) |
| 2494 | return -EBUSY; |
| 2495 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2496 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2497 | return -EBUSY; |
| 2498 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2499 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2500 | return -EINVAL; |
| 2501 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2502 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2503 | if (rdev2->raid_disk == slot) |
| 2504 | return -EEXIST; |
| 2505 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2506 | if (slot >= rdev->mddev->raid_disks && |
| 2507 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2508 | return -ENOSPC; |
| 2509 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2510 | rdev->raid_disk = slot; |
| 2511 | if (test_bit(In_sync, &rdev->flags)) |
| 2512 | rdev->saved_raid_disk = slot; |
| 2513 | else |
| 2514 | rdev->saved_raid_disk = -1; |
| 2515 | err = rdev->mddev->pers-> |
| 2516 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2517 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2518 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2519 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2520 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2521 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2522 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2523 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2524 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2525 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2526 | if (slot >= rdev->mddev->raid_disks && |
| 2527 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2528 | return -ENOSPC; |
| 2529 | rdev->raid_disk = slot; |
| 2530 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2531 | clear_bit(Faulty, &rdev->flags); |
| 2532 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2533 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2534 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2535 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2536 | return len; |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2541 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2542 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2543 | static ssize_t |
| 2544 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2545 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2546 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | static ssize_t |
| 2550 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2551 | { |
| 2552 | char *e; |
| 2553 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2554 | if (e==buf || (*e && *e != '\n')) |
| 2555 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2556 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2557 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2558 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2559 | /* Must set offset before size, so overlap checks |
| 2560 | * can be sane */ |
| 2561 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2562 | rdev->data_offset = offset; |
| 2563 | return len; |
| 2564 | } |
| 2565 | |
| 2566 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2567 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2568 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2569 | static ssize_t |
| 2570 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2571 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2572 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2573 | } |
| 2574 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2575 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2576 | { |
| 2577 | /* check if two start/length pairs overlap */ |
| 2578 | if (s1+l1 <= s2) |
| 2579 | return 0; |
| 2580 | if (s2+l2 <= s1) |
| 2581 | return 0; |
| 2582 | return 1; |
| 2583 | } |
| 2584 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2585 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2586 | { |
| 2587 | unsigned long long blocks; |
| 2588 | sector_t new; |
| 2589 | |
| 2590 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2591 | return -EINVAL; |
| 2592 | |
| 2593 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2594 | return -EINVAL; /* sector conversion overflow */ |
| 2595 | |
| 2596 | new = blocks * 2; |
| 2597 | if (new != blocks * 2) |
| 2598 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2599 | |
| 2600 | *sectors = new; |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2604 | static ssize_t |
| 2605 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2606 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2607 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2608 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2609 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2610 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2611 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2612 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2613 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2614 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2615 | sectors = super_types[my_mddev->major_version]. |
| 2616 | rdev_size_change(rdev, sectors); |
| 2617 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2618 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2619 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2620 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2621 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2622 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2623 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2624 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2625 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2626 | rdev->sectors = sectors; |
| 2627 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2628 | /* need to check that all other rdevs with the same ->bdev |
| 2629 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2630 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2631 | * we have to change it back, we will have the lock again. |
| 2632 | */ |
| 2633 | mddev_t *mddev; |
| 2634 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2635 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2636 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2637 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2638 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2639 | mdk_rdev_t *rdev2; |
| 2640 | |
| 2641 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2642 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2643 | if (rdev->bdev == rdev2->bdev && |
| 2644 | rdev != rdev2 && |
| 2645 | overlaps(rdev->data_offset, rdev->sectors, |
| 2646 | rdev2->data_offset, |
| 2647 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2648 | overlap = 1; |
| 2649 | break; |
| 2650 | } |
| 2651 | mddev_unlock(mddev); |
| 2652 | if (overlap) { |
| 2653 | mddev_put(mddev); |
| 2654 | break; |
| 2655 | } |
| 2656 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2657 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2658 | if (overlap) { |
| 2659 | /* Someone else could have slipped in a size |
| 2660 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2661 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2662 | * safe, and trust userspace not to race with |
| 2663 | * itself |
| 2664 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2665 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2666 | return -EBUSY; |
| 2667 | } |
| 2668 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2669 | return len; |
| 2670 | } |
| 2671 | |
| 2672 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2673 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2674 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2675 | |
| 2676 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2677 | { |
| 2678 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2679 | |
| 2680 | if (test_bit(In_sync, &rdev->flags) || |
| 2681 | recovery_start == MaxSector) |
| 2682 | return sprintf(page, "none\n"); |
| 2683 | |
| 2684 | return sprintf(page, "%llu\n", recovery_start); |
| 2685 | } |
| 2686 | |
| 2687 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2688 | { |
| 2689 | unsigned long long recovery_start; |
| 2690 | |
| 2691 | if (cmd_match(buf, "none")) |
| 2692 | recovery_start = MaxSector; |
| 2693 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2694 | return -EINVAL; |
| 2695 | |
| 2696 | if (rdev->mddev->pers && |
| 2697 | rdev->raid_disk >= 0) |
| 2698 | return -EBUSY; |
| 2699 | |
| 2700 | rdev->recovery_offset = recovery_start; |
| 2701 | if (recovery_start == MaxSector) |
| 2702 | set_bit(In_sync, &rdev->flags); |
| 2703 | else |
| 2704 | clear_bit(In_sync, &rdev->flags); |
| 2705 | return len; |
| 2706 | } |
| 2707 | |
| 2708 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2709 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2710 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2711 | static struct attribute *rdev_default_attrs[] = { |
| 2712 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2713 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2714 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2715 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2716 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2717 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2718 | NULL, |
| 2719 | }; |
| 2720 | static ssize_t |
| 2721 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2722 | { |
| 2723 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2724 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2725 | mddev_t *mddev = rdev->mddev; |
| 2726 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2727 | |
| 2728 | if (!entry->show) |
| 2729 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2730 | |
| 2731 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2732 | if (!rv) { |
| 2733 | if (rdev->mddev == NULL) |
| 2734 | rv = -EBUSY; |
| 2735 | else |
| 2736 | rv = entry->show(rdev, page); |
| 2737 | mddev_unlock(mddev); |
| 2738 | } |
| 2739 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static ssize_t |
| 2743 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2744 | const char *page, size_t length) |
| 2745 | { |
| 2746 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2747 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2748 | ssize_t rv; |
| 2749 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2750 | |
| 2751 | if (!entry->store) |
| 2752 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2753 | if (!capable(CAP_SYS_ADMIN)) |
| 2754 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2755 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2756 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2757 | if (rdev->mddev == NULL) |
| 2758 | rv = -EBUSY; |
| 2759 | else |
| 2760 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2761 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2762 | } |
| 2763 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | static void rdev_free(struct kobject *ko) |
| 2767 | { |
| 2768 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2769 | kfree(rdev); |
| 2770 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2771 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2772 | .show = rdev_attr_show, |
| 2773 | .store = rdev_attr_store, |
| 2774 | }; |
| 2775 | static struct kobj_type rdev_ktype = { |
| 2776 | .release = rdev_free, |
| 2777 | .sysfs_ops = &rdev_sysfs_ops, |
| 2778 | .default_attrs = rdev_default_attrs, |
| 2779 | }; |
| 2780 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2781 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2782 | { |
| 2783 | rdev->desc_nr = -1; |
| 2784 | rdev->saved_raid_disk = -1; |
| 2785 | rdev->raid_disk = -1; |
| 2786 | rdev->flags = 0; |
| 2787 | rdev->data_offset = 0; |
| 2788 | rdev->sb_events = 0; |
| 2789 | rdev->last_read_error.tv_sec = 0; |
| 2790 | rdev->last_read_error.tv_nsec = 0; |
| 2791 | atomic_set(&rdev->nr_pending, 0); |
| 2792 | atomic_set(&rdev->read_errors, 0); |
| 2793 | atomic_set(&rdev->corrected_errors, 0); |
| 2794 | |
| 2795 | INIT_LIST_HEAD(&rdev->same_set); |
| 2796 | init_waitqueue_head(&rdev->blocked_wait); |
| 2797 | } |
| 2798 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2801 | * |
| 2802 | * mark the device faulty if: |
| 2803 | * |
| 2804 | * - the device is nonexistent (zero size) |
| 2805 | * - the device has no valid superblock |
| 2806 | * |
| 2807 | * a faulty rdev _never_ has rdev->sb set. |
| 2808 | */ |
| 2809 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2810 | { |
| 2811 | char b[BDEVNAME_SIZE]; |
| 2812 | int err; |
| 2813 | mdk_rdev_t *rdev; |
| 2814 | sector_t size; |
| 2815 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2816 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | if (!rdev) { |
| 2818 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2819 | return ERR_PTR(-ENOMEM); |
| 2820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2822 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | if ((err = alloc_disk_sb(rdev))) |
| 2824 | goto abort_free; |
| 2825 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2826 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | if (err) |
| 2828 | goto abort_free; |
| 2829 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2830 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2831 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2832 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | if (!size) { |
| 2834 | printk(KERN_WARNING |
| 2835 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2836 | bdevname(rdev->bdev,b)); |
| 2837 | err = -EINVAL; |
| 2838 | goto abort_free; |
| 2839 | } |
| 2840 | |
| 2841 | if (super_format >= 0) { |
| 2842 | err = super_types[super_format]. |
| 2843 | load_super(rdev, NULL, super_minor); |
| 2844 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2845 | printk(KERN_WARNING |
| 2846 | "md: %s does not have a valid v%d.%d " |
| 2847 | "superblock, not importing!\n", |
| 2848 | bdevname(rdev->bdev,b), |
| 2849 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | goto abort_free; |
| 2851 | } |
| 2852 | if (err < 0) { |
| 2853 | printk(KERN_WARNING |
| 2854 | "md: could not read %s's sb, not importing!\n", |
| 2855 | bdevname(rdev->bdev,b)); |
| 2856 | goto abort_free; |
| 2857 | } |
| 2858 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | return rdev; |
| 2861 | |
| 2862 | abort_free: |
| 2863 | if (rdev->sb_page) { |
| 2864 | if (rdev->bdev) |
| 2865 | unlock_rdev(rdev); |
| 2866 | free_disk_sb(rdev); |
| 2867 | } |
| 2868 | kfree(rdev); |
| 2869 | return ERR_PTR(err); |
| 2870 | } |
| 2871 | |
| 2872 | /* |
| 2873 | * Check a full RAID array for plausibility |
| 2874 | */ |
| 2875 | |
| 2876 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2877 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | { |
| 2879 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2880 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | char b[BDEVNAME_SIZE]; |
| 2882 | |
| 2883 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2884 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | switch (super_types[mddev->major_version]. |
| 2886 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2887 | case 1: |
| 2888 | freshest = rdev; |
| 2889 | break; |
| 2890 | case 0: |
| 2891 | break; |
| 2892 | default: |
| 2893 | printk( KERN_ERR \ |
| 2894 | "md: fatal superblock inconsistency in %s" |
| 2895 | " -- removing from array\n", |
| 2896 | bdevname(rdev->bdev,b)); |
| 2897 | kick_rdev_from_array(rdev); |
| 2898 | } |
| 2899 | |
| 2900 | |
| 2901 | super_types[mddev->major_version]. |
| 2902 | validate_super(mddev, freshest); |
| 2903 | |
| 2904 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2905 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2906 | if (mddev->max_disks && |
| 2907 | (rdev->desc_nr >= mddev->max_disks || |
| 2908 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2909 | printk(KERN_WARNING |
| 2910 | "md: %s: %s: only %d devices permitted\n", |
| 2911 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2912 | mddev->max_disks); |
| 2913 | kick_rdev_from_array(rdev); |
| 2914 | continue; |
| 2915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | if (rdev != freshest) |
| 2917 | if (super_types[mddev->major_version]. |
| 2918 | validate_super(mddev, rdev)) { |
| 2919 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2920 | " from array!\n", |
| 2921 | bdevname(rdev->bdev,b)); |
| 2922 | kick_rdev_from_array(rdev); |
| 2923 | continue; |
| 2924 | } |
| 2925 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2926 | rdev->desc_nr = i++; |
| 2927 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2928 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2929 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2930 | rdev->raid_disk = -1; |
| 2931 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | } |
| 2933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2936 | /* Read a fixed-point number. |
| 2937 | * Numbers in sysfs attributes should be in "standard" units where |
| 2938 | * possible, so time should be in seconds. |
| 2939 | * However we internally use a a much smaller unit such as |
| 2940 | * milliseconds or jiffies. |
| 2941 | * This function takes a decimal number with a possible fractional |
| 2942 | * component, and produces an integer which is the result of |
| 2943 | * multiplying that number by 10^'scale'. |
| 2944 | * all without any floating-point arithmetic. |
| 2945 | */ |
| 2946 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2947 | { |
| 2948 | unsigned long result = 0; |
| 2949 | long decimals = -1; |
| 2950 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2951 | if (*cp == '.') |
| 2952 | decimals = 0; |
| 2953 | else if (decimals < scale) { |
| 2954 | unsigned int value; |
| 2955 | value = *cp - '0'; |
| 2956 | result = result * 10 + value; |
| 2957 | if (decimals >= 0) |
| 2958 | decimals++; |
| 2959 | } |
| 2960 | cp++; |
| 2961 | } |
| 2962 | if (*cp == '\n') |
| 2963 | cp++; |
| 2964 | if (*cp) |
| 2965 | return -EINVAL; |
| 2966 | if (decimals < 0) |
| 2967 | decimals = 0; |
| 2968 | while (decimals < scale) { |
| 2969 | result *= 10; |
| 2970 | decimals ++; |
| 2971 | } |
| 2972 | *res = result; |
| 2973 | return 0; |
| 2974 | } |
| 2975 | |
| 2976 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2977 | static void md_safemode_timeout(unsigned long data); |
| 2978 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2979 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2980 | safe_delay_show(mddev_t *mddev, char *page) |
| 2981 | { |
| 2982 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2983 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2984 | } |
| 2985 | static ssize_t |
| 2986 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2987 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2988 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2989 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2990 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2991 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2992 | if (msec == 0) |
| 2993 | mddev->safemode_delay = 0; |
| 2994 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2995 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2996 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2997 | if (mddev->safemode_delay == 0) |
| 2998 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2999 | if (mddev->safemode_delay < old_delay) |
| 3000 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3001 | } |
| 3002 | return len; |
| 3003 | } |
| 3004 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3005 | __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] | 3006 | |
| 3007 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3008 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3009 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3010 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3011 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3012 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3013 | else if (mddev->clevel[0]) |
| 3014 | return sprintf(page, "%s\n", mddev->clevel); |
| 3015 | else if (mddev->level != LEVEL_NONE) |
| 3016 | return sprintf(page, "%d\n", mddev->level); |
| 3017 | else |
| 3018 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3019 | } |
| 3020 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3021 | static ssize_t |
| 3022 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 3023 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3024 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3025 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3026 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3027 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3028 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3029 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3030 | |
| 3031 | if (mddev->pers == NULL) { |
| 3032 | if (len == 0) |
| 3033 | return 0; |
| 3034 | if (len >= sizeof(mddev->clevel)) |
| 3035 | return -ENOSPC; |
| 3036 | strncpy(mddev->clevel, buf, len); |
| 3037 | if (mddev->clevel[len-1] == '\n') |
| 3038 | len--; |
| 3039 | mddev->clevel[len] = 0; |
| 3040 | mddev->level = LEVEL_NONE; |
| 3041 | return rv; |
| 3042 | } |
| 3043 | |
| 3044 | /* request to change the personality. Need to ensure: |
| 3045 | * - array is not engaged in resync/recovery/reshape |
| 3046 | * - old personality can be suspended |
| 3047 | * - new personality will access other array. |
| 3048 | */ |
| 3049 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3050 | if (mddev->sync_thread || |
| 3051 | mddev->reshape_position != MaxSector || |
| 3052 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3053 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3054 | |
| 3055 | if (!mddev->pers->quiesce) { |
| 3056 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3057 | mdname(mddev), mddev->pers->name); |
| 3058 | return -EINVAL; |
| 3059 | } |
| 3060 | |
| 3061 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3062 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3063 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3064 | strncpy(clevel, buf, len); |
| 3065 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3066 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3067 | clevel[len] = 0; |
| 3068 | if (strict_strtol(clevel, 10, &level)) |
| 3069 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3070 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3071 | if (request_module("md-%s", clevel) != 0) |
| 3072 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3073 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3074 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3075 | if (!pers || !try_module_get(pers->owner)) { |
| 3076 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3077 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3078 | return -EINVAL; |
| 3079 | } |
| 3080 | spin_unlock(&pers_lock); |
| 3081 | |
| 3082 | if (pers == mddev->pers) { |
| 3083 | /* Nothing to do! */ |
| 3084 | module_put(pers->owner); |
| 3085 | return rv; |
| 3086 | } |
| 3087 | if (!pers->takeover) { |
| 3088 | module_put(pers->owner); |
| 3089 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3090 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3091 | return -EINVAL; |
| 3092 | } |
| 3093 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3094 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3095 | rdev->new_raid_disk = rdev->raid_disk; |
| 3096 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3097 | /* ->takeover must set new_* and/or delta_disks |
| 3098 | * if it succeeds, and may set them when it fails. |
| 3099 | */ |
| 3100 | priv = pers->takeover(mddev); |
| 3101 | if (IS_ERR(priv)) { |
| 3102 | mddev->new_level = mddev->level; |
| 3103 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3104 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3105 | mddev->raid_disks -= mddev->delta_disks; |
| 3106 | mddev->delta_disks = 0; |
| 3107 | module_put(pers->owner); |
| 3108 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3109 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3110 | return PTR_ERR(priv); |
| 3111 | } |
| 3112 | |
| 3113 | /* Looks like we have a winner */ |
| 3114 | mddev_suspend(mddev); |
| 3115 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3116 | |
| 3117 | if (mddev->pers->sync_request == NULL && |
| 3118 | pers->sync_request != NULL) { |
| 3119 | /* need to add the md_redundancy_group */ |
| 3120 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3121 | printk(KERN_WARNING |
| 3122 | "md: cannot register extra attributes for %s\n", |
| 3123 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3124 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3125 | } |
| 3126 | if (mddev->pers->sync_request != NULL && |
| 3127 | pers->sync_request == NULL) { |
| 3128 | /* need to remove the md_redundancy_group */ |
| 3129 | if (mddev->to_remove == NULL) |
| 3130 | mddev->to_remove = &md_redundancy_group; |
| 3131 | } |
| 3132 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3133 | if (mddev->pers->sync_request == NULL && |
| 3134 | mddev->external) { |
| 3135 | /* We are converting from a no-redundancy array |
| 3136 | * to a redundancy array and metadata is managed |
| 3137 | * externally so we need to be sure that writes |
| 3138 | * won't block due to a need to transition |
| 3139 | * clean->dirty |
| 3140 | * until external management is started. |
| 3141 | */ |
| 3142 | mddev->in_sync = 0; |
| 3143 | mddev->safemode_delay = 0; |
| 3144 | mddev->safemode = 0; |
| 3145 | } |
| 3146 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3147 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3148 | if (rdev->raid_disk < 0) |
| 3149 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3150 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3151 | rdev->new_raid_disk = -1; |
| 3152 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3153 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3154 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3155 | } |
| 3156 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3157 | if (rdev->raid_disk < 0) |
| 3158 | continue; |
| 3159 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3160 | continue; |
| 3161 | rdev->raid_disk = rdev->new_raid_disk; |
| 3162 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3163 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3164 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3165 | if (sysfs_link_rdev(mddev, rdev)) |
| 3166 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3167 | " for %s after level change\n", |
| 3168 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3169 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3173 | mddev->pers = pers; |
| 3174 | mddev->private = priv; |
| 3175 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3176 | mddev->level = mddev->new_level; |
| 3177 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3178 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3179 | mddev->delta_disks = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3180 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3181 | if (mddev->pers->sync_request == NULL) { |
| 3182 | /* this is now an array without redundancy, so |
| 3183 | * it must always be in_sync |
| 3184 | */ |
| 3185 | mddev->in_sync = 1; |
| 3186 | del_timer_sync(&mddev->safemode_timer); |
| 3187 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3188 | pers->run(mddev); |
| 3189 | mddev_resume(mddev); |
| 3190 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3191 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3192 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3193 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3194 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3195 | return rv; |
| 3196 | } |
| 3197 | |
| 3198 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3199 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3200 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3201 | |
| 3202 | static ssize_t |
| 3203 | layout_show(mddev_t *mddev, char *page) |
| 3204 | { |
| 3205 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3206 | if (mddev->reshape_position != MaxSector && |
| 3207 | mddev->layout != mddev->new_layout) |
| 3208 | return sprintf(page, "%d (%d)\n", |
| 3209 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3210 | return sprintf(page, "%d\n", mddev->layout); |
| 3211 | } |
| 3212 | |
| 3213 | static ssize_t |
| 3214 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3215 | { |
| 3216 | char *e; |
| 3217 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3218 | |
| 3219 | if (!*buf || (*e && *e != '\n')) |
| 3220 | return -EINVAL; |
| 3221 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3222 | if (mddev->pers) { |
| 3223 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3224 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3225 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3226 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3227 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3228 | if (err) { |
| 3229 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3230 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3231 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3232 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3233 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3234 | if (mddev->reshape_position == MaxSector) |
| 3235 | mddev->layout = n; |
| 3236 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3237 | return len; |
| 3238 | } |
| 3239 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3240 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3241 | |
| 3242 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3243 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3244 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3245 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3246 | if (mddev->raid_disks == 0) |
| 3247 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3248 | if (mddev->reshape_position != MaxSector && |
| 3249 | mddev->delta_disks != 0) |
| 3250 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3251 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3252 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3253 | } |
| 3254 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3255 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3256 | |
| 3257 | static ssize_t |
| 3258 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3259 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3260 | char *e; |
| 3261 | int rv = 0; |
| 3262 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3263 | |
| 3264 | if (!*buf || (*e && *e != '\n')) |
| 3265 | return -EINVAL; |
| 3266 | |
| 3267 | if (mddev->pers) |
| 3268 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3269 | else if (mddev->reshape_position != MaxSector) { |
| 3270 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3271 | mddev->delta_disks = n - olddisks; |
| 3272 | mddev->raid_disks = n; |
| 3273 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3274 | mddev->raid_disks = n; |
| 3275 | return rv ? rv : len; |
| 3276 | } |
| 3277 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3278 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3279 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3280 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3281 | chunk_size_show(mddev_t *mddev, char *page) |
| 3282 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3283 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3284 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3285 | return sprintf(page, "%d (%d)\n", |
| 3286 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3287 | mddev->chunk_sectors << 9); |
| 3288 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | static ssize_t |
| 3292 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3293 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3294 | char *e; |
| 3295 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3296 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3297 | if (!*buf || (*e && *e != '\n')) |
| 3298 | return -EINVAL; |
| 3299 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3300 | if (mddev->pers) { |
| 3301 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3302 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3303 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3304 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3305 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3306 | if (err) { |
| 3307 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3308 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3309 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3310 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3311 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3312 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3313 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3314 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3315 | return len; |
| 3316 | } |
| 3317 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3318 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3319 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3320 | static ssize_t |
| 3321 | resync_start_show(mddev_t *mddev, char *page) |
| 3322 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3323 | if (mddev->recovery_cp == MaxSector) |
| 3324 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3325 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3326 | } |
| 3327 | |
| 3328 | static ssize_t |
| 3329 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3330 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3331 | char *e; |
| 3332 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3333 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3334 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3335 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3336 | if (cmd_match(buf, "none")) |
| 3337 | n = MaxSector; |
| 3338 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3339 | return -EINVAL; |
| 3340 | |
| 3341 | mddev->recovery_cp = n; |
| 3342 | return len; |
| 3343 | } |
| 3344 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3345 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3346 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3347 | /* |
| 3348 | * The array state can be: |
| 3349 | * |
| 3350 | * clear |
| 3351 | * No devices, no size, no level |
| 3352 | * Equivalent to STOP_ARRAY ioctl |
| 3353 | * inactive |
| 3354 | * May have some settings, but array is not active |
| 3355 | * all IO results in error |
| 3356 | * When written, doesn't tear down array, but just stops it |
| 3357 | * suspended (not supported yet) |
| 3358 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3359 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3360 | * readonly |
| 3361 | * no resync can happen. no superblocks get written. |
| 3362 | * write requests fail |
| 3363 | * read-auto |
| 3364 | * like readonly, but behaves like 'clean' on a write request. |
| 3365 | * |
| 3366 | * clean - no pending writes, but otherwise active. |
| 3367 | * When written to inactive array, starts without resync |
| 3368 | * If a write request arrives then |
| 3369 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3370 | * if not known, block and switch to write-pending |
| 3371 | * If written to an active array that has pending writes, then fails. |
| 3372 | * active |
| 3373 | * fully active: IO and resync can be happening. |
| 3374 | * When written to inactive array, starts with resync |
| 3375 | * |
| 3376 | * write-pending |
| 3377 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3378 | * |
| 3379 | * active-idle |
| 3380 | * like active, but no writes have been seen for a while (100msec). |
| 3381 | * |
| 3382 | */ |
| 3383 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3384 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3385 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3386 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3387 | "write-pending", "active-idle", NULL }; |
| 3388 | |
| 3389 | static int match_word(const char *word, char **list) |
| 3390 | { |
| 3391 | int n; |
| 3392 | for (n=0; list[n]; n++) |
| 3393 | if (cmd_match(word, list[n])) |
| 3394 | break; |
| 3395 | return n; |
| 3396 | } |
| 3397 | |
| 3398 | static ssize_t |
| 3399 | array_state_show(mddev_t *mddev, char *page) |
| 3400 | { |
| 3401 | enum array_state st = inactive; |
| 3402 | |
| 3403 | if (mddev->pers) |
| 3404 | switch(mddev->ro) { |
| 3405 | case 1: |
| 3406 | st = readonly; |
| 3407 | break; |
| 3408 | case 2: |
| 3409 | st = read_auto; |
| 3410 | break; |
| 3411 | case 0: |
| 3412 | if (mddev->in_sync) |
| 3413 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3414 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3415 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3416 | else if (mddev->safemode) |
| 3417 | st = active_idle; |
| 3418 | else |
| 3419 | st = active; |
| 3420 | } |
| 3421 | else { |
| 3422 | if (list_empty(&mddev->disks) && |
| 3423 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3424 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3425 | st = clear; |
| 3426 | else |
| 3427 | st = inactive; |
| 3428 | } |
| 3429 | return sprintf(page, "%s\n", array_states[st]); |
| 3430 | } |
| 3431 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3432 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3433 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3434 | static int do_md_run(mddev_t * mddev); |
| 3435 | static int restart_array(mddev_t *mddev); |
| 3436 | |
| 3437 | static ssize_t |
| 3438 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3439 | { |
| 3440 | int err = -EINVAL; |
| 3441 | enum array_state st = match_word(buf, array_states); |
| 3442 | switch(st) { |
| 3443 | case bad_word: |
| 3444 | break; |
| 3445 | case clear: |
| 3446 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3447 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3448 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3449 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3450 | break; |
| 3451 | case inactive: |
| 3452 | /* stopping an active array */ |
| 3453 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3454 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3455 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3456 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3457 | } else |
| 3458 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3459 | break; |
| 3460 | case suspended: |
| 3461 | break; /* not supported yet */ |
| 3462 | case readonly: |
| 3463 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3464 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3465 | else { |
| 3466 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3467 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3468 | err = do_md_run(mddev); |
| 3469 | } |
| 3470 | break; |
| 3471 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3472 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3473 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3474 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3475 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3476 | err = restart_array(mddev); |
| 3477 | if (err == 0) { |
| 3478 | mddev->ro = 2; |
| 3479 | set_disk_ro(mddev->gendisk, 0); |
| 3480 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3481 | } else { |
| 3482 | mddev->ro = 2; |
| 3483 | err = do_md_run(mddev); |
| 3484 | } |
| 3485 | break; |
| 3486 | case clean: |
| 3487 | if (mddev->pers) { |
| 3488 | restart_array(mddev); |
| 3489 | spin_lock_irq(&mddev->write_lock); |
| 3490 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3491 | if (mddev->in_sync == 0) { |
| 3492 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3493 | if (mddev->safemode == 1) |
| 3494 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3495 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3496 | } |
| 3497 | err = 0; |
| 3498 | } else |
| 3499 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3500 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3501 | } else |
| 3502 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3503 | break; |
| 3504 | case active: |
| 3505 | if (mddev->pers) { |
| 3506 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3507 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3508 | wake_up(&mddev->sb_wait); |
| 3509 | err = 0; |
| 3510 | } else { |
| 3511 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3512 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3513 | err = do_md_run(mddev); |
| 3514 | } |
| 3515 | break; |
| 3516 | case write_pending: |
| 3517 | case active_idle: |
| 3518 | /* these cannot be set */ |
| 3519 | break; |
| 3520 | } |
| 3521 | if (err) |
| 3522 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3523 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3524 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3525 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3526 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3527 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3528 | static struct md_sysfs_entry md_array_state = |
| 3529 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3530 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3531 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3532 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3533 | return sprintf(page, "%d\n", |
| 3534 | atomic_read(&mddev->max_corr_read_errors)); |
| 3535 | } |
| 3536 | |
| 3537 | static ssize_t |
| 3538 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3539 | { |
| 3540 | char *e; |
| 3541 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3542 | |
| 3543 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3544 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3545 | return len; |
| 3546 | } |
| 3547 | return -EINVAL; |
| 3548 | } |
| 3549 | |
| 3550 | static struct md_sysfs_entry max_corr_read_errors = |
| 3551 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3552 | max_corrected_read_errors_store); |
| 3553 | |
| 3554 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3555 | null_show(mddev_t *mddev, char *page) |
| 3556 | { |
| 3557 | return -EINVAL; |
| 3558 | } |
| 3559 | |
| 3560 | static ssize_t |
| 3561 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3562 | { |
| 3563 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3564 | /* The new device is added to the array. |
| 3565 | * If the array has a persistent superblock, we read the |
| 3566 | * superblock to initialise info and check validity. |
| 3567 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3568 | * which mainly checks size. |
| 3569 | */ |
| 3570 | char *e; |
| 3571 | int major = simple_strtoul(buf, &e, 10); |
| 3572 | int minor; |
| 3573 | dev_t dev; |
| 3574 | mdk_rdev_t *rdev; |
| 3575 | int err; |
| 3576 | |
| 3577 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3578 | return -EINVAL; |
| 3579 | minor = simple_strtoul(e+1, &e, 10); |
| 3580 | if (*e && *e != '\n') |
| 3581 | return -EINVAL; |
| 3582 | dev = MKDEV(major, minor); |
| 3583 | if (major != MAJOR(dev) || |
| 3584 | minor != MINOR(dev)) |
| 3585 | return -EOVERFLOW; |
| 3586 | |
| 3587 | |
| 3588 | if (mddev->persistent) { |
| 3589 | rdev = md_import_device(dev, mddev->major_version, |
| 3590 | mddev->minor_version); |
| 3591 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3592 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3593 | mdk_rdev_t, same_set); |
| 3594 | err = super_types[mddev->major_version] |
| 3595 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3596 | if (err < 0) |
| 3597 | goto out; |
| 3598 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3599 | } else if (mddev->external) |
| 3600 | rdev = md_import_device(dev, -2, -1); |
| 3601 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3602 | rdev = md_import_device(dev, -1, -1); |
| 3603 | |
| 3604 | if (IS_ERR(rdev)) |
| 3605 | return PTR_ERR(rdev); |
| 3606 | err = bind_rdev_to_array(rdev, mddev); |
| 3607 | out: |
| 3608 | if (err) |
| 3609 | export_rdev(rdev); |
| 3610 | return err ? err : len; |
| 3611 | } |
| 3612 | |
| 3613 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3614 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3615 | |
| 3616 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3617 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3618 | { |
| 3619 | char *end; |
| 3620 | unsigned long chunk, end_chunk; |
| 3621 | |
| 3622 | if (!mddev->bitmap) |
| 3623 | goto out; |
| 3624 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3625 | while (*buf) { |
| 3626 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3627 | if (buf == end) break; |
| 3628 | if (*end == '-') { /* range */ |
| 3629 | buf = end + 1; |
| 3630 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3631 | if (buf == end) break; |
| 3632 | } |
| 3633 | if (*end && !isspace(*end)) break; |
| 3634 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3635 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3636 | } |
| 3637 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3638 | out: |
| 3639 | return len; |
| 3640 | } |
| 3641 | |
| 3642 | static struct md_sysfs_entry md_bitmap = |
| 3643 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3644 | |
| 3645 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3646 | size_show(mddev_t *mddev, char *page) |
| 3647 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3648 | return sprintf(page, "%llu\n", |
| 3649 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3650 | } |
| 3651 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3652 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3653 | |
| 3654 | static ssize_t |
| 3655 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3656 | { |
| 3657 | /* If array is inactive, we can reduce the component size, but |
| 3658 | * not increase it (except from 0). |
| 3659 | * If array is active, we can try an on-line resize |
| 3660 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3661 | sector_t sectors; |
| 3662 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3663 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3664 | if (err < 0) |
| 3665 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3666 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3667 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3668 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3669 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3670 | if (mddev->dev_sectors == 0 || |
| 3671 | mddev->dev_sectors > sectors) |
| 3672 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3673 | else |
| 3674 | err = -ENOSPC; |
| 3675 | } |
| 3676 | return err ? err : len; |
| 3677 | } |
| 3678 | |
| 3679 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3680 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3681 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3682 | |
| 3683 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3684 | * This is one of |
| 3685 | * 'none' for arrays with no metadata (good luck...) |
| 3686 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3687 | * or N.M for internally known formats |
| 3688 | */ |
| 3689 | static ssize_t |
| 3690 | metadata_show(mddev_t *mddev, char *page) |
| 3691 | { |
| 3692 | if (mddev->persistent) |
| 3693 | return sprintf(page, "%d.%d\n", |
| 3694 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3695 | else if (mddev->external) |
| 3696 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3697 | else |
| 3698 | return sprintf(page, "none\n"); |
| 3699 | } |
| 3700 | |
| 3701 | static ssize_t |
| 3702 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3703 | { |
| 3704 | int major, minor; |
| 3705 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3706 | /* Changing the details of 'external' metadata is |
| 3707 | * always permitted. Otherwise there must be |
| 3708 | * no devices attached to the array. |
| 3709 | */ |
| 3710 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3711 | ; |
| 3712 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3713 | return -EBUSY; |
| 3714 | |
| 3715 | if (cmd_match(buf, "none")) { |
| 3716 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3717 | mddev->external = 0; |
| 3718 | mddev->major_version = 0; |
| 3719 | mddev->minor_version = 90; |
| 3720 | return len; |
| 3721 | } |
| 3722 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3723 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3724 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3725 | namelen = sizeof(mddev->metadata_type)-1; |
| 3726 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3727 | mddev->metadata_type[namelen] = 0; |
| 3728 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3729 | mddev->metadata_type[--namelen] = 0; |
| 3730 | mddev->persistent = 0; |
| 3731 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3732 | mddev->major_version = 0; |
| 3733 | mddev->minor_version = 90; |
| 3734 | return len; |
| 3735 | } |
| 3736 | major = simple_strtoul(buf, &e, 10); |
| 3737 | if (e==buf || *e != '.') |
| 3738 | return -EINVAL; |
| 3739 | buf = e+1; |
| 3740 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3741 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3742 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3743 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3744 | return -ENOENT; |
| 3745 | mddev->major_version = major; |
| 3746 | mddev->minor_version = minor; |
| 3747 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3748 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3749 | return len; |
| 3750 | } |
| 3751 | |
| 3752 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3753 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3754 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3755 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3756 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3757 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3758 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3759 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3760 | type = "frozen"; |
| 3761 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3762 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3763 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3764 | type = "reshape"; |
| 3765 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3766 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3767 | type = "resync"; |
| 3768 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3769 | type = "check"; |
| 3770 | else |
| 3771 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3772 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3773 | type = "recover"; |
| 3774 | } |
| 3775 | return sprintf(page, "%s\n", type); |
| 3776 | } |
| 3777 | |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3778 | static void reap_sync_thread(mddev_t *mddev); |
| 3779 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3780 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3781 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3782 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3783 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3784 | return -EINVAL; |
| 3785 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3786 | if (cmd_match(page, "frozen")) |
| 3787 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3788 | else |
| 3789 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3790 | |
| 3791 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3792 | if (mddev->sync_thread) { |
| 3793 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 3794 | reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3795 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3796 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3797 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3798 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3799 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3800 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3801 | else if (cmd_match(page, "recover")) { |
| 3802 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3803 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3804 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3805 | int err; |
| 3806 | if (mddev->pers->start_reshape == NULL) |
| 3807 | return -EINVAL; |
| 3808 | err = mddev->pers->start_reshape(mddev); |
| 3809 | if (err) |
| 3810 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3811 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3812 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3813 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3814 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3815 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3816 | return -EINVAL; |
| 3817 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3818 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3819 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3820 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3821 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3822 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3823 | return len; |
| 3824 | } |
| 3825 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3826 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3827 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3828 | { |
| 3829 | return sprintf(page, "%llu\n", |
| 3830 | (unsigned long long) mddev->resync_mismatches); |
| 3831 | } |
| 3832 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3833 | static struct md_sysfs_entry md_scan_mode = |
| 3834 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3835 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3836 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3837 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3838 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3839 | static ssize_t |
| 3840 | sync_min_show(mddev_t *mddev, char *page) |
| 3841 | { |
| 3842 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3843 | mddev->sync_speed_min ? "local": "system"); |
| 3844 | } |
| 3845 | |
| 3846 | static ssize_t |
| 3847 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3848 | { |
| 3849 | int min; |
| 3850 | char *e; |
| 3851 | if (strncmp(buf, "system", 6)==0) { |
| 3852 | mddev->sync_speed_min = 0; |
| 3853 | return len; |
| 3854 | } |
| 3855 | min = simple_strtoul(buf, &e, 10); |
| 3856 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3857 | return -EINVAL; |
| 3858 | mddev->sync_speed_min = min; |
| 3859 | return len; |
| 3860 | } |
| 3861 | |
| 3862 | static struct md_sysfs_entry md_sync_min = |
| 3863 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3864 | |
| 3865 | static ssize_t |
| 3866 | sync_max_show(mddev_t *mddev, char *page) |
| 3867 | { |
| 3868 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3869 | mddev->sync_speed_max ? "local": "system"); |
| 3870 | } |
| 3871 | |
| 3872 | static ssize_t |
| 3873 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3874 | { |
| 3875 | int max; |
| 3876 | char *e; |
| 3877 | if (strncmp(buf, "system", 6)==0) { |
| 3878 | mddev->sync_speed_max = 0; |
| 3879 | return len; |
| 3880 | } |
| 3881 | max = simple_strtoul(buf, &e, 10); |
| 3882 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3883 | return -EINVAL; |
| 3884 | mddev->sync_speed_max = max; |
| 3885 | return len; |
| 3886 | } |
| 3887 | |
| 3888 | static struct md_sysfs_entry md_sync_max = |
| 3889 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3890 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3891 | static ssize_t |
| 3892 | degraded_show(mddev_t *mddev, char *page) |
| 3893 | { |
| 3894 | return sprintf(page, "%d\n", mddev->degraded); |
| 3895 | } |
| 3896 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3897 | |
| 3898 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3899 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3900 | { |
| 3901 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3902 | } |
| 3903 | |
| 3904 | static ssize_t |
| 3905 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3906 | { |
| 3907 | long n; |
| 3908 | |
| 3909 | if (strict_strtol(buf, 10, &n)) |
| 3910 | return -EINVAL; |
| 3911 | |
| 3912 | if (n != 0 && n != 1) |
| 3913 | return -EINVAL; |
| 3914 | |
| 3915 | mddev->parallel_resync = n; |
| 3916 | |
| 3917 | if (mddev->sync_thread) |
| 3918 | wake_up(&resync_wait); |
| 3919 | |
| 3920 | return len; |
| 3921 | } |
| 3922 | |
| 3923 | /* force parallel resync, even with shared block devices */ |
| 3924 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3925 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3926 | sync_force_parallel_show, sync_force_parallel_store); |
| 3927 | |
| 3928 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3929 | sync_speed_show(mddev_t *mddev, char *page) |
| 3930 | { |
| 3931 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3932 | if (mddev->curr_resync == 0) |
| 3933 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3934 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3935 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3936 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3937 | db = resync - mddev->resync_mark_cnt; |
| 3938 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3939 | } |
| 3940 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3941 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3942 | |
| 3943 | static ssize_t |
| 3944 | sync_completed_show(mddev_t *mddev, char *page) |
| 3945 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3946 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3947 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3948 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3949 | return sprintf(page, "none\n"); |
| 3950 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3951 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3952 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3953 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3954 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3955 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3956 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3957 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3960 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3961 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3962 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3963 | min_sync_show(mddev_t *mddev, char *page) |
| 3964 | { |
| 3965 | return sprintf(page, "%llu\n", |
| 3966 | (unsigned long long)mddev->resync_min); |
| 3967 | } |
| 3968 | static ssize_t |
| 3969 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3970 | { |
| 3971 | unsigned long long min; |
| 3972 | if (strict_strtoull(buf, 10, &min)) |
| 3973 | return -EINVAL; |
| 3974 | if (min > mddev->resync_max) |
| 3975 | return -EINVAL; |
| 3976 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3977 | return -EBUSY; |
| 3978 | |
| 3979 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3980 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3981 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3982 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3983 | return -EINVAL; |
| 3984 | } |
| 3985 | mddev->resync_min = min; |
| 3986 | |
| 3987 | return len; |
| 3988 | } |
| 3989 | |
| 3990 | static struct md_sysfs_entry md_min_sync = |
| 3991 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3992 | |
| 3993 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3994 | max_sync_show(mddev_t *mddev, char *page) |
| 3995 | { |
| 3996 | if (mddev->resync_max == MaxSector) |
| 3997 | return sprintf(page, "max\n"); |
| 3998 | else |
| 3999 | return sprintf(page, "%llu\n", |
| 4000 | (unsigned long long)mddev->resync_max); |
| 4001 | } |
| 4002 | static ssize_t |
| 4003 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 4004 | { |
| 4005 | if (strncmp(buf, "max", 3) == 0) |
| 4006 | mddev->resync_max = MaxSector; |
| 4007 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4008 | unsigned long long max; |
| 4009 | if (strict_strtoull(buf, 10, &max)) |
| 4010 | return -EINVAL; |
| 4011 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4012 | return -EINVAL; |
| 4013 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4014 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4015 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4016 | return -EBUSY; |
| 4017 | |
| 4018 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4019 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4020 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4021 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4022 | return -EINVAL; |
| 4023 | } |
| 4024 | mddev->resync_max = max; |
| 4025 | } |
| 4026 | wake_up(&mddev->recovery_wait); |
| 4027 | return len; |
| 4028 | } |
| 4029 | |
| 4030 | static struct md_sysfs_entry md_max_sync = |
| 4031 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4032 | |
| 4033 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4034 | suspend_lo_show(mddev_t *mddev, char *page) |
| 4035 | { |
| 4036 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4037 | } |
| 4038 | |
| 4039 | static ssize_t |
| 4040 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4041 | { |
| 4042 | char *e; |
| 4043 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4044 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4045 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4046 | if (mddev->pers == NULL || |
| 4047 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4048 | return -EINVAL; |
| 4049 | if (buf == e || (*e && *e != '\n')) |
| 4050 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4051 | |
| 4052 | mddev->suspend_lo = new; |
| 4053 | if (new >= old) |
| 4054 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4055 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4056 | else { |
| 4057 | /* Expanding suspended region - need to wait */ |
| 4058 | mddev->pers->quiesce(mddev, 1); |
| 4059 | mddev->pers->quiesce(mddev, 0); |
| 4060 | } |
| 4061 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4062 | } |
| 4063 | static struct md_sysfs_entry md_suspend_lo = |
| 4064 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4065 | |
| 4066 | |
| 4067 | static ssize_t |
| 4068 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4069 | { |
| 4070 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4071 | } |
| 4072 | |
| 4073 | static ssize_t |
| 4074 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4075 | { |
| 4076 | char *e; |
| 4077 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4078 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4079 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4080 | if (mddev->pers == NULL || |
| 4081 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4082 | return -EINVAL; |
| 4083 | if (buf == e || (*e && *e != '\n')) |
| 4084 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4085 | |
| 4086 | mddev->suspend_hi = new; |
| 4087 | if (new <= old) |
| 4088 | /* Shrinking suspended region */ |
| 4089 | mddev->pers->quiesce(mddev, 2); |
| 4090 | else { |
| 4091 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4092 | mddev->pers->quiesce(mddev, 1); |
| 4093 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4094 | } |
| 4095 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4096 | } |
| 4097 | static struct md_sysfs_entry md_suspend_hi = |
| 4098 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4099 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4100 | static ssize_t |
| 4101 | reshape_position_show(mddev_t *mddev, char *page) |
| 4102 | { |
| 4103 | if (mddev->reshape_position != MaxSector) |
| 4104 | return sprintf(page, "%llu\n", |
| 4105 | (unsigned long long)mddev->reshape_position); |
| 4106 | strcpy(page, "none\n"); |
| 4107 | return 5; |
| 4108 | } |
| 4109 | |
| 4110 | static ssize_t |
| 4111 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4112 | { |
| 4113 | char *e; |
| 4114 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4115 | if (mddev->pers) |
| 4116 | return -EBUSY; |
| 4117 | if (buf == e || (*e && *e != '\n')) |
| 4118 | return -EINVAL; |
| 4119 | mddev->reshape_position = new; |
| 4120 | mddev->delta_disks = 0; |
| 4121 | mddev->new_level = mddev->level; |
| 4122 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4123 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4124 | return len; |
| 4125 | } |
| 4126 | |
| 4127 | static struct md_sysfs_entry md_reshape_position = |
| 4128 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4129 | reshape_position_store); |
| 4130 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4131 | static ssize_t |
| 4132 | array_size_show(mddev_t *mddev, char *page) |
| 4133 | { |
| 4134 | if (mddev->external_size) |
| 4135 | return sprintf(page, "%llu\n", |
| 4136 | (unsigned long long)mddev->array_sectors/2); |
| 4137 | else |
| 4138 | return sprintf(page, "default\n"); |
| 4139 | } |
| 4140 | |
| 4141 | static ssize_t |
| 4142 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4143 | { |
| 4144 | sector_t sectors; |
| 4145 | |
| 4146 | if (strncmp(buf, "default", 7) == 0) { |
| 4147 | if (mddev->pers) |
| 4148 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4149 | else |
| 4150 | sectors = mddev->array_sectors; |
| 4151 | |
| 4152 | mddev->external_size = 0; |
| 4153 | } else { |
| 4154 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4155 | return -EINVAL; |
| 4156 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4157 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4158 | |
| 4159 | mddev->external_size = 1; |
| 4160 | } |
| 4161 | |
| 4162 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4163 | if (mddev->pers) { |
| 4164 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4165 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4166 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4167 | return len; |
| 4168 | } |
| 4169 | |
| 4170 | static struct md_sysfs_entry md_array_size = |
| 4171 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4172 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4173 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4174 | static struct attribute *md_default_attrs[] = { |
| 4175 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4176 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4177 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4178 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4179 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4180 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4181 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4182 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4183 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4184 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4185 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4186 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4187 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4188 | NULL, |
| 4189 | }; |
| 4190 | |
| 4191 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4192 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4193 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4194 | &md_sync_min.attr, |
| 4195 | &md_sync_max.attr, |
| 4196 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4197 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4198 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4199 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4200 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4201 | &md_suspend_lo.attr, |
| 4202 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4203 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4204 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4205 | NULL, |
| 4206 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4207 | static struct attribute_group md_redundancy_group = { |
| 4208 | .name = NULL, |
| 4209 | .attrs = md_redundancy_attrs, |
| 4210 | }; |
| 4211 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4212 | |
| 4213 | static ssize_t |
| 4214 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4215 | { |
| 4216 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4217 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4218 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4219 | |
| 4220 | if (!entry->show) |
| 4221 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4222 | rv = mddev_lock(mddev); |
| 4223 | if (!rv) { |
| 4224 | rv = entry->show(mddev, page); |
| 4225 | mddev_unlock(mddev); |
| 4226 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4227 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | static ssize_t |
| 4231 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4232 | const char *page, size_t length) |
| 4233 | { |
| 4234 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4235 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4236 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4237 | |
| 4238 | if (!entry->store) |
| 4239 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4240 | if (!capable(CAP_SYS_ADMIN)) |
| 4241 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4242 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4243 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4244 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4245 | if (!rv) { |
| 4246 | rv = entry->store(mddev, page, length); |
| 4247 | mddev_unlock(mddev); |
| 4248 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4249 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | static void md_free(struct kobject *ko) |
| 4253 | { |
| 4254 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4255 | |
| 4256 | if (mddev->sysfs_state) |
| 4257 | sysfs_put(mddev->sysfs_state); |
| 4258 | |
| 4259 | if (mddev->gendisk) { |
| 4260 | del_gendisk(mddev->gendisk); |
| 4261 | put_disk(mddev->gendisk); |
| 4262 | } |
| 4263 | if (mddev->queue) |
| 4264 | blk_cleanup_queue(mddev->queue); |
| 4265 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4266 | kfree(mddev); |
| 4267 | } |
| 4268 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4269 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4270 | .show = md_attr_show, |
| 4271 | .store = md_attr_store, |
| 4272 | }; |
| 4273 | static struct kobj_type md_ktype = { |
| 4274 | .release = md_free, |
| 4275 | .sysfs_ops = &md_sysfs_ops, |
| 4276 | .default_attrs = md_default_attrs, |
| 4277 | }; |
| 4278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | int mdp_major = 0; |
| 4280 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4281 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4282 | { |
| 4283 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4284 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4285 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4286 | kobject_del(&mddev->kobj); |
| 4287 | kobject_put(&mddev->kobj); |
| 4288 | } |
| 4289 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4290 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4292 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | mddev_t *mddev = mddev_find(dev); |
| 4294 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4295 | int partitioned; |
| 4296 | int shift; |
| 4297 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4298 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | |
| 4300 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4301 | return -ENODEV; |
| 4302 | |
| 4303 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4304 | shift = partitioned ? MdpMinorShift : 0; |
| 4305 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4307 | /* wait for any previous instance of this device to be |
| 4308 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4309 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4310 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4311 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4312 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4313 | error = -EEXIST; |
| 4314 | if (mddev->gendisk) |
| 4315 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4316 | |
| 4317 | if (name) { |
| 4318 | /* Need to ensure that 'name' is not a duplicate. |
| 4319 | */ |
| 4320 | mddev_t *mddev2; |
| 4321 | spin_lock(&all_mddevs_lock); |
| 4322 | |
| 4323 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4324 | if (mddev2->gendisk && |
| 4325 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4326 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4327 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4328 | } |
| 4329 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4331 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4332 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4333 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4334 | if (!mddev->queue) |
| 4335 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4336 | mddev->queue->queuedata = mddev; |
| 4337 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4338 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | disk = alloc_disk(1 << shift); |
| 4341 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4342 | blk_cleanup_queue(mddev->queue); |
| 4343 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4344 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4346 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4348 | if (name) |
| 4349 | strcpy(disk->disk_name, name); |
| 4350 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4352 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | disk->fops = &md_fops; |
| 4355 | disk->private_data = mddev; |
| 4356 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4357 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4358 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4359 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4360 | * remove it now. |
| 4361 | */ |
| 4362 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4364 | /* As soon as we call add_disk(), another thread could get |
| 4365 | * through to md_open, so make sure it doesn't get too far |
| 4366 | */ |
| 4367 | mutex_lock(&mddev->open_mutex); |
| 4368 | add_disk(disk); |
| 4369 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4370 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4371 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4372 | if (error) { |
| 4373 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4374 | * __must_check, we must do something with the result |
| 4375 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4376 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4377 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4378 | error = 0; |
| 4379 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4380 | if (mddev->kobj.sd && |
| 4381 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4382 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4383 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4384 | abort: |
| 4385 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4386 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4387 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4388 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4389 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4390 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4391 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4395 | { |
| 4396 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | return NULL; |
| 4398 | } |
| 4399 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4400 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4401 | { |
| 4402 | /* val must be "md_*" where * is not all digits. |
| 4403 | * We allocate an array with a large free minor number, and |
| 4404 | * set the name to val. val must not already be an active name. |
| 4405 | */ |
| 4406 | int len = strlen(val); |
| 4407 | char buf[DISK_NAME_LEN]; |
| 4408 | |
| 4409 | while (len && val[len-1] == '\n') |
| 4410 | len--; |
| 4411 | if (len >= DISK_NAME_LEN) |
| 4412 | return -E2BIG; |
| 4413 | strlcpy(buf, val, len+1); |
| 4414 | if (strncmp(buf, "md_", 3) != 0) |
| 4415 | return -EINVAL; |
| 4416 | return md_alloc(0, buf); |
| 4417 | } |
| 4418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | static void md_safemode_timeout(unsigned long data) |
| 4420 | { |
| 4421 | mddev_t *mddev = (mddev_t *) data; |
| 4422 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4423 | if (!atomic_read(&mddev->writes_pending)) { |
| 4424 | mddev->safemode = 1; |
| 4425 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4426 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | md_wakeup_thread(mddev->thread); |
| 4429 | } |
| 4430 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4431 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4432 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4433 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4435 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4437 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4439 | if (list_empty(&mddev->disks)) |
| 4440 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | |
| 4443 | if (mddev->pers) |
| 4444 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4445 | /* Cannot run until previous stop completes properly */ |
| 4446 | if (mddev->sysfs_active) |
| 4447 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | /* |
| 4450 | * Analyze all RAID superblock(s) |
| 4451 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4452 | if (!mddev->raid_disks) { |
| 4453 | if (!mddev->persistent) |
| 4454 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4455 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4458 | if (mddev->level != LEVEL_NONE) |
| 4459 | request_module("md-level-%d", mddev->level); |
| 4460 | else if (mddev->clevel[0]) |
| 4461 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
| 4463 | /* |
| 4464 | * Drop all container device buffers, from now on |
| 4465 | * the only valid external interface is through the md |
| 4466 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4468 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4469 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | continue; |
| 4471 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4472 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4473 | |
| 4474 | /* perform some consistency tests on the device. |
| 4475 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4476 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4477 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4478 | if (rdev->meta_bdev) { |
| 4479 | /* Nothing to check */; |
| 4480 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4481 | if (mddev->dev_sectors && |
| 4482 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4483 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4484 | printk("md: %s: data overlaps metadata\n", |
| 4485 | mdname(mddev)); |
| 4486 | return -EINVAL; |
| 4487 | } |
| 4488 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4489 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4490 | > rdev->data_offset) { |
| 4491 | printk("md: %s: metadata overlaps data\n", |
| 4492 | mdname(mddev)); |
| 4493 | return -EINVAL; |
| 4494 | } |
| 4495 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4496 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4499 | if (mddev->bio_set == NULL) |
| 4500 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev)); |
| 4501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4503 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4504 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4506 | if (mddev->level != LEVEL_NONE) |
| 4507 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4508 | mddev->level); |
| 4509 | else |
| 4510 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4511 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | return -EINVAL; |
| 4513 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4514 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4516 | if (mddev->level != pers->level) { |
| 4517 | mddev->level = pers->level; |
| 4518 | mddev->new_level = pers->level; |
| 4519 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4520 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4522 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4523 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4524 | /* This personality cannot handle reshaping... */ |
| 4525 | mddev->pers = NULL; |
| 4526 | module_put(pers->owner); |
| 4527 | return -EINVAL; |
| 4528 | } |
| 4529 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4530 | if (pers->sync_request) { |
| 4531 | /* Warn if this is a potentially silly |
| 4532 | * configuration. |
| 4533 | */ |
| 4534 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4535 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4536 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4537 | |
| 4538 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4539 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4540 | if (rdev < rdev2 && |
| 4541 | rdev->bdev->bd_contains == |
| 4542 | rdev2->bdev->bd_contains) { |
| 4543 | printk(KERN_WARNING |
| 4544 | "%s: WARNING: %s appears to be" |
| 4545 | " on the same physical disk as" |
| 4546 | " %s.\n", |
| 4547 | mdname(mddev), |
| 4548 | bdevname(rdev->bdev,b), |
| 4549 | bdevname(rdev2->bdev,b2)); |
| 4550 | warned = 1; |
| 4551 | } |
| 4552 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4553 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4554 | if (warned) |
| 4555 | printk(KERN_WARNING |
| 4556 | "True protection against single-disk" |
| 4557 | " failure might be compromised.\n"); |
| 4558 | } |
| 4559 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4560 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4561 | /* may be over-ridden by personality */ |
| 4562 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4563 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4564 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4566 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4567 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4568 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4569 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4570 | if (err) |
| 4571 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4572 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4573 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4574 | " but 'external_size' not in effect?\n", __func__); |
| 4575 | printk(KERN_ERR |
| 4576 | "md: invalid array_size %llu > default size %llu\n", |
| 4577 | (unsigned long long)mddev->array_sectors / 2, |
| 4578 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4579 | err = -EINVAL; |
| 4580 | mddev->pers->stop(mddev); |
| 4581 | } |
| 4582 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4583 | err = bitmap_create(mddev); |
| 4584 | if (err) { |
| 4585 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4586 | mdname(mddev), err); |
| 4587 | mddev->pers->stop(mddev); |
| 4588 | } |
| 4589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | module_put(mddev->pers->owner); |
| 4592 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4593 | bitmap_destroy(mddev); |
| 4594 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4596 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4597 | if (mddev->kobj.sd && |
| 4598 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4599 | printk(KERN_WARNING |
| 4600 | "md: cannot register extra attributes for %s\n", |
| 4601 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4602 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4603 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4604 | mddev->ro = 0; |
| 4605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4607 | atomic_set(&mddev->max_corr_read_errors, |
| 4608 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | mddev->safemode = 0; |
| 4610 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4611 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4612 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4613 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4614 | smp_wmb(); |
| 4615 | mddev->ready = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4616 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4617 | if (rdev->raid_disk >= 0) |
| 4618 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4619 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | |
| 4621 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4622 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4623 | if (mddev->flags) |
| 4624 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4626 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4627 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4628 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4629 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | return 0; |
| 4631 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4632 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4634 | static int do_md_run(mddev_t *mddev) |
| 4635 | { |
| 4636 | int err; |
| 4637 | |
| 4638 | err = md_run(mddev); |
| 4639 | if (err) |
| 4640 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4641 | err = bitmap_load(mddev); |
| 4642 | if (err) { |
| 4643 | bitmap_destroy(mddev); |
| 4644 | goto out; |
| 4645 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4646 | |
| 4647 | md_wakeup_thread(mddev->thread); |
| 4648 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4649 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4650 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4651 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4652 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4653 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4654 | out: |
| 4655 | return err; |
| 4656 | } |
| 4657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | static int restart_array(mddev_t *mddev) |
| 4659 | { |
| 4660 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4662 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4664 | return -ENXIO; |
| 4665 | if (!mddev->pers) |
| 4666 | return -EINVAL; |
| 4667 | if (!mddev->ro) |
| 4668 | return -EBUSY; |
| 4669 | mddev->safemode = 0; |
| 4670 | mddev->ro = 0; |
| 4671 | set_disk_ro(disk, 0); |
| 4672 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4673 | mdname(mddev)); |
| 4674 | /* Kick recovery or resync if necessary */ |
| 4675 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4676 | md_wakeup_thread(mddev->thread); |
| 4677 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4678 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4679 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4682 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4683 | * to the file ourselves */ |
| 4684 | static int deny_bitmap_write_access(struct file * file) |
| 4685 | { |
| 4686 | struct inode *inode = file->f_mapping->host; |
| 4687 | |
| 4688 | spin_lock(&inode->i_lock); |
| 4689 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4690 | spin_unlock(&inode->i_lock); |
| 4691 | return -ETXTBSY; |
| 4692 | } |
| 4693 | atomic_set(&inode->i_writecount, -1); |
| 4694 | spin_unlock(&inode->i_lock); |
| 4695 | |
| 4696 | return 0; |
| 4697 | } |
| 4698 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4699 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4700 | { |
| 4701 | struct inode *inode = file->f_mapping->host; |
| 4702 | |
| 4703 | spin_lock(&inode->i_lock); |
| 4704 | atomic_set(&inode->i_writecount, 1); |
| 4705 | spin_unlock(&inode->i_lock); |
| 4706 | } |
| 4707 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4708 | static void md_clean(mddev_t *mddev) |
| 4709 | { |
| 4710 | mddev->array_sectors = 0; |
| 4711 | mddev->external_size = 0; |
| 4712 | mddev->dev_sectors = 0; |
| 4713 | mddev->raid_disks = 0; |
| 4714 | mddev->recovery_cp = 0; |
| 4715 | mddev->resync_min = 0; |
| 4716 | mddev->resync_max = MaxSector; |
| 4717 | mddev->reshape_position = MaxSector; |
| 4718 | mddev->external = 0; |
| 4719 | mddev->persistent = 0; |
| 4720 | mddev->level = LEVEL_NONE; |
| 4721 | mddev->clevel[0] = 0; |
| 4722 | mddev->flags = 0; |
| 4723 | mddev->ro = 0; |
| 4724 | mddev->metadata_type[0] = 0; |
| 4725 | mddev->chunk_sectors = 0; |
| 4726 | mddev->ctime = mddev->utime = 0; |
| 4727 | mddev->layout = 0; |
| 4728 | mddev->max_disks = 0; |
| 4729 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4730 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4731 | mddev->delta_disks = 0; |
| 4732 | mddev->new_level = LEVEL_NONE; |
| 4733 | mddev->new_layout = 0; |
| 4734 | mddev->new_chunk_sectors = 0; |
| 4735 | mddev->curr_resync = 0; |
| 4736 | mddev->resync_mismatches = 0; |
| 4737 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4738 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4739 | mddev->recovery = 0; |
| 4740 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4741 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4742 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4743 | mddev->safemode = 0; |
| 4744 | mddev->bitmap_info.offset = 0; |
| 4745 | mddev->bitmap_info.default_offset = 0; |
| 4746 | mddev->bitmap_info.chunksize = 0; |
| 4747 | mddev->bitmap_info.daemon_sleep = 0; |
| 4748 | mddev->bitmap_info.max_write_behind = 0; |
| 4749 | } |
| 4750 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4751 | static void __md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4752 | { |
| 4753 | if (mddev->sync_thread) { |
| 4754 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4755 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4756 | reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | del_timer_sync(&mddev->safemode_timer); |
| 4760 | |
| 4761 | bitmap_flush(mddev); |
| 4762 | md_super_wait(mddev); |
| 4763 | |
| 4764 | if (!mddev->in_sync || mddev->flags) { |
| 4765 | /* mark array as shutdown cleanly */ |
| 4766 | mddev->in_sync = 1; |
| 4767 | md_update_sb(mddev, 1); |
| 4768 | } |
| 4769 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4770 | |
| 4771 | void md_stop_writes(mddev_t *mddev) |
| 4772 | { |
| 4773 | mddev_lock(mddev); |
| 4774 | __md_stop_writes(mddev); |
| 4775 | mddev_unlock(mddev); |
| 4776 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4777 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4778 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4779 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4780 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4781 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4782 | mddev->pers->stop(mddev); |
| 4783 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4784 | mddev->to_remove = &md_redundancy_group; |
| 4785 | module_put(mddev->pers->owner); |
| 4786 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4787 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4788 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4789 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4790 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4791 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4792 | { |
| 4793 | int err = 0; |
| 4794 | mutex_lock(&mddev->open_mutex); |
| 4795 | if (atomic_read(&mddev->openers) > is_open) { |
| 4796 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4797 | err = -EBUSY; |
| 4798 | goto out; |
| 4799 | } |
| 4800 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4801 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4802 | |
| 4803 | err = -ENXIO; |
| 4804 | if (mddev->ro==1) |
| 4805 | goto out; |
| 4806 | mddev->ro = 1; |
| 4807 | set_disk_ro(mddev->gendisk, 1); |
| 4808 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4809 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4810 | err = 0; |
| 4811 | } |
| 4812 | out: |
| 4813 | mutex_unlock(&mddev->open_mutex); |
| 4814 | return err; |
| 4815 | } |
| 4816 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4817 | /* mode: |
| 4818 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4819 | * 2 - stop but do not disassemble array |
| 4820 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4821 | 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] | 4822 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4824 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4826 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4827 | if (atomic_read(&mddev->openers) > is_open || |
| 4828 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4829 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4830 | mutex_unlock(&mddev->open_mutex); |
| 4831 | return -EBUSY; |
| 4832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4834 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4835 | if (mddev->ro) |
| 4836 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4837 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4838 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4839 | md_stop(mddev); |
| 4840 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4841 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4842 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4843 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4844 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4845 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4846 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4847 | if (rdev->raid_disk >= 0) |
| 4848 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4849 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4850 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4851 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4852 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4853 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4854 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4855 | if (mddev->ro) |
| 4856 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4857 | } else |
| 4858 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | /* |
| 4860 | * Free resources if final stop |
| 4861 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4862 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4864 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4865 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4866 | if (mddev->bitmap_info.file) { |
| 4867 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4868 | fput(mddev->bitmap_info.file); |
| 4869 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4870 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4871 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | export_array(mddev); |
| 4874 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4875 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4876 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4877 | if (mddev->hold_active == UNTIL_STOP) |
| 4878 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4879 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4880 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4881 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4882 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4883 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4886 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | static void autorun_array(mddev_t *mddev) |
| 4888 | { |
| 4889 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | int err; |
| 4891 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4892 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
| 4895 | printk(KERN_INFO "md: running: "); |
| 4896 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4897 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | char b[BDEVNAME_SIZE]; |
| 4899 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4900 | } |
| 4901 | printk("\n"); |
| 4902 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4903 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | if (err) { |
| 4905 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4906 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | /* |
| 4911 | * lets try to run arrays based on all disks that have arrived |
| 4912 | * until now. (those are in pending_raid_disks) |
| 4913 | * |
| 4914 | * the method: pick the first pending disk, collect all disks with |
| 4915 | * the same UUID, remove all from the pending list and put them into |
| 4916 | * the 'same_array' list. Then order this list based on superblock |
| 4917 | * update time (freshest comes first), kick out 'old' disks and |
| 4918 | * compare superblocks. If everything's fine then run it. |
| 4919 | * |
| 4920 | * If "unit" is allocated, then bump its reference count |
| 4921 | */ |
| 4922 | static void autorun_devices(int part) |
| 4923 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4924 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | mddev_t *mddev; |
| 4926 | char b[BDEVNAME_SIZE]; |
| 4927 | |
| 4928 | printk(KERN_INFO "md: autorun ...\n"); |
| 4929 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4930 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4932 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | rdev0 = list_entry(pending_raid_disks.next, |
| 4934 | mdk_rdev_t, same_set); |
| 4935 | |
| 4936 | printk(KERN_INFO "md: considering %s ...\n", |
| 4937 | bdevname(rdev0->bdev,b)); |
| 4938 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4939 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4941 | printk(KERN_INFO "md: adding %s ...\n", |
| 4942 | bdevname(rdev->bdev,b)); |
| 4943 | list_move(&rdev->same_set, &candidates); |
| 4944 | } |
| 4945 | /* |
| 4946 | * now we have a set of devices, with all of them having |
| 4947 | * mostly sane superblocks. It's time to allocate the |
| 4948 | * mddev. |
| 4949 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4950 | if (part) { |
| 4951 | dev = MKDEV(mdp_major, |
| 4952 | rdev0->preferred_minor << MdpMinorShift); |
| 4953 | unit = MINOR(dev) >> MdpMinorShift; |
| 4954 | } else { |
| 4955 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4956 | unit = MINOR(dev); |
| 4957 | } |
| 4958 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4960 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4961 | break; |
| 4962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | |
| 4964 | md_probe(dev, NULL, NULL); |
| 4965 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4966 | if (!mddev || !mddev->gendisk) { |
| 4967 | if (mddev) |
| 4968 | mddev_put(mddev); |
| 4969 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | "md: cannot allocate memory for md drive.\n"); |
| 4971 | break; |
| 4972 | } |
| 4973 | if (mddev_lock(mddev)) |
| 4974 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4975 | mdname(mddev)); |
| 4976 | else if (mddev->raid_disks || mddev->major_version |
| 4977 | || !list_empty(&mddev->disks)) { |
| 4978 | printk(KERN_WARNING |
| 4979 | "md: %s already running, cannot run %s\n", |
| 4980 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4981 | mddev_unlock(mddev); |
| 4982 | } else { |
| 4983 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4984 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4985 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | list_del_init(&rdev->same_set); |
| 4987 | if (bind_rdev_to_array(rdev, mddev)) |
| 4988 | export_rdev(rdev); |
| 4989 | } |
| 4990 | autorun_array(mddev); |
| 4991 | mddev_unlock(mddev); |
| 4992 | } |
| 4993 | /* on success, candidates will be empty, on error |
| 4994 | * it won't... |
| 4995 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4996 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4997 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | mddev_put(mddev); |
| 5001 | } |
| 5002 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5003 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5004 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | static int get_version(void __user * arg) |
| 5007 | { |
| 5008 | mdu_version_t ver; |
| 5009 | |
| 5010 | ver.major = MD_MAJOR_VERSION; |
| 5011 | ver.minor = MD_MINOR_VERSION; |
| 5012 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5013 | |
| 5014 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5015 | return -EFAULT; |
| 5016 | |
| 5017 | return 0; |
| 5018 | } |
| 5019 | |
| 5020 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 5021 | { |
| 5022 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5023 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5026 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5027 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5029 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | failed++; |
| 5031 | else { |
| 5032 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5033 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5034 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | else |
| 5036 | spare++; |
| 5037 | } |
| 5038 | } |
| 5039 | |
| 5040 | info.major_version = mddev->major_version; |
| 5041 | info.minor_version = mddev->minor_version; |
| 5042 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5043 | info.ctime = mddev->ctime; |
| 5044 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5045 | info.size = mddev->dev_sectors / 2; |
| 5046 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5047 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5048 | info.nr_disks = nr; |
| 5049 | info.raid_disks = mddev->raid_disks; |
| 5050 | info.md_minor = mddev->md_minor; |
| 5051 | info.not_persistent= !mddev->persistent; |
| 5052 | |
| 5053 | info.utime = mddev->utime; |
| 5054 | info.state = 0; |
| 5055 | if (mddev->in_sync) |
| 5056 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5057 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5058 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5059 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | info.working_disks = working; |
| 5061 | info.failed_disks = failed; |
| 5062 | info.spare_disks = spare; |
| 5063 | |
| 5064 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5065 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
| 5067 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5068 | return -EFAULT; |
| 5069 | |
| 5070 | return 0; |
| 5071 | } |
| 5072 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5073 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5074 | { |
| 5075 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5076 | char *ptr, *buf = NULL; |
| 5077 | int err = -ENOMEM; |
| 5078 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5079 | if (md_allow_write(mddev)) |
| 5080 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5081 | else |
| 5082 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5083 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5084 | if (!file) |
| 5085 | goto out; |
| 5086 | |
| 5087 | /* bitmap disabled, zero the first byte and copy out */ |
| 5088 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5089 | file->pathname[0] = '\0'; |
| 5090 | goto copy_out; |
| 5091 | } |
| 5092 | |
| 5093 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5094 | if (!buf) |
| 5095 | goto out; |
| 5096 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5097 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5098 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5099 | goto out; |
| 5100 | |
| 5101 | strcpy(file->pathname, ptr); |
| 5102 | |
| 5103 | copy_out: |
| 5104 | err = 0; |
| 5105 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5106 | err = -EFAULT; |
| 5107 | out: |
| 5108 | kfree(buf); |
| 5109 | kfree(file); |
| 5110 | return err; |
| 5111 | } |
| 5112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5114 | { |
| 5115 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | mdk_rdev_t *rdev; |
| 5117 | |
| 5118 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5119 | return -EFAULT; |
| 5120 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5121 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | if (rdev) { |
| 5123 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5124 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5125 | info.raid_disk = rdev->raid_disk; |
| 5126 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5127 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5129 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5131 | info.state |= (1<<MD_DISK_SYNC); |
| 5132 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5133 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5134 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | } else { |
| 5136 | info.major = info.minor = 0; |
| 5137 | info.raid_disk = -1; |
| 5138 | info.state = (1<<MD_DISK_REMOVED); |
| 5139 | } |
| 5140 | |
| 5141 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5142 | return -EFAULT; |
| 5143 | |
| 5144 | return 0; |
| 5145 | } |
| 5146 | |
| 5147 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5148 | { |
| 5149 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5150 | mdk_rdev_t *rdev; |
| 5151 | dev_t dev = MKDEV(info->major,info->minor); |
| 5152 | |
| 5153 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5154 | return -EOVERFLOW; |
| 5155 | |
| 5156 | if (!mddev->raid_disks) { |
| 5157 | int err; |
| 5158 | /* expecting a device which has a superblock */ |
| 5159 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5160 | if (IS_ERR(rdev)) { |
| 5161 | printk(KERN_WARNING |
| 5162 | "md: md_import_device returned %ld\n", |
| 5163 | PTR_ERR(rdev)); |
| 5164 | return PTR_ERR(rdev); |
| 5165 | } |
| 5166 | if (!list_empty(&mddev->disks)) { |
| 5167 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5168 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5169 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5171 | if (err < 0) { |
| 5172 | printk(KERN_WARNING |
| 5173 | "md: %s has different UUID to %s\n", |
| 5174 | bdevname(rdev->bdev,b), |
| 5175 | bdevname(rdev0->bdev,b2)); |
| 5176 | export_rdev(rdev); |
| 5177 | return -EINVAL; |
| 5178 | } |
| 5179 | } |
| 5180 | err = bind_rdev_to_array(rdev, mddev); |
| 5181 | if (err) |
| 5182 | export_rdev(rdev); |
| 5183 | return err; |
| 5184 | } |
| 5185 | |
| 5186 | /* |
| 5187 | * add_new_disk can be used once the array is assembled |
| 5188 | * to add "hot spares". They must already have a superblock |
| 5189 | * written |
| 5190 | */ |
| 5191 | if (mddev->pers) { |
| 5192 | int err; |
| 5193 | if (!mddev->pers->hot_add_disk) { |
| 5194 | printk(KERN_WARNING |
| 5195 | "%s: personality does not support diskops!\n", |
| 5196 | mdname(mddev)); |
| 5197 | return -EINVAL; |
| 5198 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5199 | if (mddev->persistent) |
| 5200 | rdev = md_import_device(dev, mddev->major_version, |
| 5201 | mddev->minor_version); |
| 5202 | else |
| 5203 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | if (IS_ERR(rdev)) { |
| 5205 | printk(KERN_WARNING |
| 5206 | "md: md_import_device returned %ld\n", |
| 5207 | PTR_ERR(rdev)); |
| 5208 | return PTR_ERR(rdev); |
| 5209 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5210 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5211 | if (!mddev->persistent) { |
| 5212 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5213 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5214 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5215 | set_bit(In_sync, &rdev->flags); |
| 5216 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5217 | rdev->raid_disk = -1; |
| 5218 | } else |
| 5219 | super_types[mddev->major_version]. |
| 5220 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5221 | if ((info->state & (1<<MD_DISK_SYNC)) && |
| 5222 | (!test_bit(In_sync, &rdev->flags) || |
| 5223 | rdev->raid_disk != info->raid_disk)) { |
| 5224 | /* This was a hot-add request, but events doesn't |
| 5225 | * match, so reject it. |
| 5226 | */ |
| 5227 | export_rdev(rdev); |
| 5228 | return -EINVAL; |
| 5229 | } |
| 5230 | |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5231 | if (test_bit(In_sync, &rdev->flags)) |
| 5232 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5233 | else |
| 5234 | rdev->saved_raid_disk = -1; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5235 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5236 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5237 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5238 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5239 | else |
| 5240 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | rdev->raid_disk = -1; |
| 5243 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5244 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5245 | /* If there is hot_add_disk but no hot_remove_disk |
| 5246 | * then added disks for geometry changes, |
| 5247 | * and should be added immediately. |
| 5248 | */ |
| 5249 | super_types[mddev->major_version]. |
| 5250 | validate_super(mddev, rdev); |
| 5251 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5252 | if (err) |
| 5253 | unbind_rdev_from_array(rdev); |
| 5254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | if (err) |
| 5256 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5257 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5258 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5259 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5260 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5261 | if (mddev->degraded) |
| 5262 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5263 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5264 | if (!err) |
| 5265 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5266 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | return err; |
| 5268 | } |
| 5269 | |
| 5270 | /* otherwise, add_new_disk is only allowed |
| 5271 | * for major_version==0 superblocks |
| 5272 | */ |
| 5273 | if (mddev->major_version != 0) { |
| 5274 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5275 | mdname(mddev)); |
| 5276 | return -EINVAL; |
| 5277 | } |
| 5278 | |
| 5279 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5280 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5281 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | if (IS_ERR(rdev)) { |
| 5283 | printk(KERN_WARNING |
| 5284 | "md: error, md_import_device() returned %ld\n", |
| 5285 | PTR_ERR(rdev)); |
| 5286 | return PTR_ERR(rdev); |
| 5287 | } |
| 5288 | rdev->desc_nr = info->number; |
| 5289 | if (info->raid_disk < mddev->raid_disks) |
| 5290 | rdev->raid_disk = info->raid_disk; |
| 5291 | else |
| 5292 | rdev->raid_disk = -1; |
| 5293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5295 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5296 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5298 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5299 | set_bit(WriteMostly, &rdev->flags); |
| 5300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | if (!mddev->persistent) { |
| 5302 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5303 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5304 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5305 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5306 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5308 | err = bind_rdev_to_array(rdev, mddev); |
| 5309 | if (err) { |
| 5310 | export_rdev(rdev); |
| 5311 | return err; |
| 5312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5319 | { |
| 5320 | char b[BDEVNAME_SIZE]; |
| 5321 | mdk_rdev_t *rdev; |
| 5322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | rdev = find_rdev(mddev, dev); |
| 5324 | if (!rdev) |
| 5325 | return -ENXIO; |
| 5326 | |
| 5327 | if (rdev->raid_disk >= 0) |
| 5328 | goto busy; |
| 5329 | |
| 5330 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5331 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5332 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | |
| 5334 | return 0; |
| 5335 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5336 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5338 | return -EBUSY; |
| 5339 | } |
| 5340 | |
| 5341 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5342 | { |
| 5343 | char b[BDEVNAME_SIZE]; |
| 5344 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | mdk_rdev_t *rdev; |
| 5346 | |
| 5347 | if (!mddev->pers) |
| 5348 | return -ENODEV; |
| 5349 | |
| 5350 | if (mddev->major_version != 0) { |
| 5351 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5352 | " version-0 superblocks.\n", |
| 5353 | mdname(mddev)); |
| 5354 | return -EINVAL; |
| 5355 | } |
| 5356 | if (!mddev->pers->hot_add_disk) { |
| 5357 | printk(KERN_WARNING |
| 5358 | "%s: personality does not support diskops!\n", |
| 5359 | mdname(mddev)); |
| 5360 | return -EINVAL; |
| 5361 | } |
| 5362 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5363 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | if (IS_ERR(rdev)) { |
| 5365 | printk(KERN_WARNING |
| 5366 | "md: error, md_import_device() returned %ld\n", |
| 5367 | PTR_ERR(rdev)); |
| 5368 | return -EINVAL; |
| 5369 | } |
| 5370 | |
| 5371 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5372 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5374 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5376 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5378 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | printk(KERN_WARNING |
| 5380 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5381 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5382 | err = -EINVAL; |
| 5383 | goto abort_export; |
| 5384 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5385 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5387 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5388 | err = bind_rdev_to_array(rdev, mddev); |
| 5389 | if (err) |
| 5390 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | |
| 5392 | /* |
| 5393 | * The rest should better be atomic, we can have disk failures |
| 5394 | * noticed in interrupt contexts ... |
| 5395 | */ |
| 5396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | rdev->raid_disk = -1; |
| 5398 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5399 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | |
| 5401 | /* |
| 5402 | * Kick recovery, maybe this spare has to be added to the |
| 5403 | * array immediately. |
| 5404 | */ |
| 5405 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5406 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5407 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | return 0; |
| 5409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | abort_export: |
| 5411 | export_rdev(rdev); |
| 5412 | return err; |
| 5413 | } |
| 5414 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5415 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5416 | { |
| 5417 | int err; |
| 5418 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5419 | if (mddev->pers) { |
| 5420 | if (!mddev->pers->quiesce) |
| 5421 | return -EBUSY; |
| 5422 | if (mddev->recovery || mddev->sync_thread) |
| 5423 | return -EBUSY; |
| 5424 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5425 | } |
| 5426 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5427 | |
| 5428 | if (fd >= 0) { |
| 5429 | if (mddev->bitmap) |
| 5430 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5431 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5432 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5433 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5434 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5435 | mdname(mddev)); |
| 5436 | return -EBADF; |
| 5437 | } |
| 5438 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5439 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5440 | if (err) { |
| 5441 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5442 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5443 | fput(mddev->bitmap_info.file); |
| 5444 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5445 | return err; |
| 5446 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5447 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5448 | } else if (mddev->bitmap == NULL) |
| 5449 | return -ENOENT; /* cannot remove what isn't there */ |
| 5450 | err = 0; |
| 5451 | if (mddev->pers) { |
| 5452 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5453 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5454 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5455 | if (!err) |
| 5456 | err = bitmap_load(mddev); |
| 5457 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5458 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5459 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5460 | fd = -1; /* make sure to put the file */ |
| 5461 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5462 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5463 | } |
| 5464 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5465 | if (mddev->bitmap_info.file) { |
| 5466 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5467 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5468 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5469 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5472 | return err; |
| 5473 | } |
| 5474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | /* |
| 5476 | * set_array_info is used two different ways |
| 5477 | * The original usage is when creating a new array. |
| 5478 | * In this usage, raid_disks is > 0 and it together with |
| 5479 | * level, size, not_persistent,layout,chunksize determine the |
| 5480 | * shape of the array. |
| 5481 | * This will always create an array with a type-0.90.0 superblock. |
| 5482 | * The newer usage is when assembling an array. |
| 5483 | * In this case raid_disks will be 0, and the major_version field is |
| 5484 | * use to determine which style super-blocks are to be found on the devices. |
| 5485 | * The minor and patch _version numbers are also kept incase the |
| 5486 | * super_block handler wishes to interpret them. |
| 5487 | */ |
| 5488 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5489 | { |
| 5490 | |
| 5491 | if (info->raid_disks == 0) { |
| 5492 | /* just setting version number for superblock loading */ |
| 5493 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5494 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | super_types[info->major_version].name == NULL) { |
| 5496 | /* maybe try to auto-load a module? */ |
| 5497 | printk(KERN_INFO |
| 5498 | "md: superblock version %d not known\n", |
| 5499 | info->major_version); |
| 5500 | return -EINVAL; |
| 5501 | } |
| 5502 | mddev->major_version = info->major_version; |
| 5503 | mddev->minor_version = info->minor_version; |
| 5504 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5505 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5506 | /* ensure mddev_put doesn't delete this now that there |
| 5507 | * is some minimal configuration. |
| 5508 | */ |
| 5509 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | return 0; |
| 5511 | } |
| 5512 | mddev->major_version = MD_MAJOR_VERSION; |
| 5513 | mddev->minor_version = MD_MINOR_VERSION; |
| 5514 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5515 | mddev->ctime = get_seconds(); |
| 5516 | |
| 5517 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5518 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5519 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | mddev->raid_disks = info->raid_disks; |
| 5521 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5522 | * openned |
| 5523 | */ |
| 5524 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5525 | mddev->recovery_cp = MaxSector; |
| 5526 | else |
| 5527 | mddev->recovery_cp = 0; |
| 5528 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5529 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | |
| 5531 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5532 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | |
| 5534 | mddev->max_disks = MD_SB_DISKS; |
| 5535 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5536 | if (mddev->persistent) |
| 5537 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5538 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5540 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5541 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5542 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5543 | mddev->reshape_position = MaxSector; |
| 5544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | /* |
| 5546 | * Generate a 128 bit UUID |
| 5547 | */ |
| 5548 | get_random_bytes(mddev->uuid, 16); |
| 5549 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5550 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5551 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5552 | mddev->new_layout = mddev->layout; |
| 5553 | mddev->delta_disks = 0; |
| 5554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | return 0; |
| 5556 | } |
| 5557 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5558 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5559 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5560 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5561 | |
| 5562 | if (mddev->external_size) |
| 5563 | return; |
| 5564 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5565 | mddev->array_sectors = array_sectors; |
| 5566 | } |
| 5567 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5568 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5569 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5570 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5571 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5572 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5573 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5574 | |
| 5575 | if (mddev->pers->resize == NULL) |
| 5576 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5577 | /* The "num_sectors" is the number of sectors of each device that |
| 5578 | * is used. This can only make sense for arrays with redundancy. |
| 5579 | * linear and raid0 always use whatever space is available. We can only |
| 5580 | * consider changing this number if no resync or reconstruction is |
| 5581 | * 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] | 5582 | * 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] | 5583 | * of each device. If num_sectors is zero, we find the largest size |
| 5584 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5585 | */ |
| 5586 | if (mddev->sync_thread) |
| 5587 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5588 | if (mddev->bitmap) |
| 5589 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5590 | * grow, and re-add. |
| 5591 | */ |
| 5592 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5593 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5594 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5595 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5596 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5597 | num_sectors = avail; |
| 5598 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5599 | return -ENOSPC; |
| 5600 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5601 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5602 | if (!rv) |
| 5603 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5604 | return rv; |
| 5605 | } |
| 5606 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5607 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5608 | { |
| 5609 | int rv; |
| 5610 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5611 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5612 | return -EINVAL; |
| 5613 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5614 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5615 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5616 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5617 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5618 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5619 | |
| 5620 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 5621 | if (rv < 0) |
| 5622 | mddev->delta_disks = 0; |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5623 | return rv; |
| 5624 | } |
| 5625 | |
| 5626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | /* |
| 5628 | * update_array_info is used to change the configuration of an |
| 5629 | * on-line array. |
| 5630 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5631 | * fields in the info are checked against the array. |
| 5632 | * Any differences that cannot be handled will cause an error. |
| 5633 | * Normally, only one change can be managed at a time. |
| 5634 | */ |
| 5635 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5636 | { |
| 5637 | int rv = 0; |
| 5638 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5639 | int state = 0; |
| 5640 | |
| 5641 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5642 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5643 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | |
| 5645 | if (mddev->major_version != info->major_version || |
| 5646 | mddev->minor_version != info->minor_version || |
| 5647 | /* mddev->patch_version != info->patch_version || */ |
| 5648 | mddev->ctime != info->ctime || |
| 5649 | mddev->level != info->level || |
| 5650 | /* mddev->layout != info->layout || */ |
| 5651 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5652 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5653 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5654 | ((state^info->state) & 0xfffffe00) |
| 5655 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | return -EINVAL; |
| 5657 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5658 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5659 | cnt++; |
| 5660 | if (mddev->raid_disks != info->raid_disks) |
| 5661 | cnt++; |
| 5662 | if (mddev->layout != info->layout) |
| 5663 | cnt++; |
| 5664 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5665 | cnt++; |
| 5666 | if (cnt == 0) |
| 5667 | return 0; |
| 5668 | if (cnt > 1) |
| 5669 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | |
| 5671 | if (mddev->layout != info->layout) { |
| 5672 | /* Change layout |
| 5673 | * we don't need to do anything at the md level, the |
| 5674 | * personality will take care of it all. |
| 5675 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5676 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5678 | else { |
| 5679 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5680 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5681 | if (rv) |
| 5682 | mddev->new_layout = mddev->layout; |
| 5683 | return rv; |
| 5684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5686 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5687 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5689 | if (mddev->raid_disks != info->raid_disks) |
| 5690 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5692 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5693 | if (mddev->pers->quiesce == NULL) |
| 5694 | return -EINVAL; |
| 5695 | if (mddev->recovery || mddev->sync_thread) |
| 5696 | return -EBUSY; |
| 5697 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5698 | /* add the bitmap */ |
| 5699 | if (mddev->bitmap) |
| 5700 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5701 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5702 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5703 | mddev->bitmap_info.offset = |
| 5704 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5705 | mddev->pers->quiesce(mddev, 1); |
| 5706 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5707 | if (!rv) |
| 5708 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5709 | if (rv) |
| 5710 | bitmap_destroy(mddev); |
| 5711 | mddev->pers->quiesce(mddev, 0); |
| 5712 | } else { |
| 5713 | /* remove the bitmap */ |
| 5714 | if (!mddev->bitmap) |
| 5715 | return -ENOENT; |
| 5716 | if (mddev->bitmap->file) |
| 5717 | return -EINVAL; |
| 5718 | mddev->pers->quiesce(mddev, 1); |
| 5719 | bitmap_destroy(mddev); |
| 5720 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5721 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5722 | } |
| 5723 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5724 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | return rv; |
| 5726 | } |
| 5727 | |
| 5728 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5729 | { |
| 5730 | mdk_rdev_t *rdev; |
| 5731 | |
| 5732 | if (mddev->pers == NULL) |
| 5733 | return -ENODEV; |
| 5734 | |
| 5735 | rdev = find_rdev(mddev, dev); |
| 5736 | if (!rdev) |
| 5737 | return -ENODEV; |
| 5738 | |
| 5739 | md_error(mddev, rdev); |
| 5740 | return 0; |
| 5741 | } |
| 5742 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5743 | /* |
| 5744 | * We have a problem here : there is no easy way to give a CHS |
| 5745 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5746 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5747 | * dosfs just mad... ;-) |
| 5748 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5749 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5750 | { |
| 5751 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5752 | |
| 5753 | geo->heads = 2; |
| 5754 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5755 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5756 | return 0; |
| 5757 | } |
| 5758 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5759 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | unsigned int cmd, unsigned long arg) |
| 5761 | { |
| 5762 | int err = 0; |
| 5763 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5765 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | |
| 5767 | if (!capable(CAP_SYS_ADMIN)) |
| 5768 | return -EACCES; |
| 5769 | |
| 5770 | /* |
| 5771 | * Commands dealing with the RAID driver but not any |
| 5772 | * particular array: |
| 5773 | */ |
| 5774 | switch (cmd) |
| 5775 | { |
| 5776 | case RAID_VERSION: |
| 5777 | err = get_version(argp); |
| 5778 | goto done; |
| 5779 | |
| 5780 | case PRINT_RAID_DEBUG: |
| 5781 | err = 0; |
| 5782 | md_print_devices(); |
| 5783 | goto done; |
| 5784 | |
| 5785 | #ifndef MODULE |
| 5786 | case RAID_AUTORUN: |
| 5787 | err = 0; |
| 5788 | autostart_arrays(arg); |
| 5789 | goto done; |
| 5790 | #endif |
| 5791 | default:; |
| 5792 | } |
| 5793 | |
| 5794 | /* |
| 5795 | * Commands creating/starting a new array: |
| 5796 | */ |
| 5797 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5798 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | |
| 5800 | if (!mddev) { |
| 5801 | BUG(); |
| 5802 | goto abort; |
| 5803 | } |
| 5804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | err = mddev_lock(mddev); |
| 5806 | if (err) { |
| 5807 | printk(KERN_INFO |
| 5808 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5809 | err, cmd); |
| 5810 | goto abort; |
| 5811 | } |
| 5812 | |
| 5813 | switch (cmd) |
| 5814 | { |
| 5815 | case SET_ARRAY_INFO: |
| 5816 | { |
| 5817 | mdu_array_info_t info; |
| 5818 | if (!arg) |
| 5819 | memset(&info, 0, sizeof(info)); |
| 5820 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5821 | err = -EFAULT; |
| 5822 | goto abort_unlock; |
| 5823 | } |
| 5824 | if (mddev->pers) { |
| 5825 | err = update_array_info(mddev, &info); |
| 5826 | if (err) { |
| 5827 | printk(KERN_WARNING "md: couldn't update" |
| 5828 | " array info. %d\n", err); |
| 5829 | goto abort_unlock; |
| 5830 | } |
| 5831 | goto done_unlock; |
| 5832 | } |
| 5833 | if (!list_empty(&mddev->disks)) { |
| 5834 | printk(KERN_WARNING |
| 5835 | "md: array %s already has disks!\n", |
| 5836 | mdname(mddev)); |
| 5837 | err = -EBUSY; |
| 5838 | goto abort_unlock; |
| 5839 | } |
| 5840 | if (mddev->raid_disks) { |
| 5841 | printk(KERN_WARNING |
| 5842 | "md: array %s already initialised!\n", |
| 5843 | mdname(mddev)); |
| 5844 | err = -EBUSY; |
| 5845 | goto abort_unlock; |
| 5846 | } |
| 5847 | err = set_array_info(mddev, &info); |
| 5848 | if (err) { |
| 5849 | printk(KERN_WARNING "md: couldn't set" |
| 5850 | " array info. %d\n", err); |
| 5851 | goto abort_unlock; |
| 5852 | } |
| 5853 | } |
| 5854 | goto done_unlock; |
| 5855 | |
| 5856 | default:; |
| 5857 | } |
| 5858 | |
| 5859 | /* |
| 5860 | * Commands querying/configuring an existing array: |
| 5861 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5862 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5863 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5864 | if ((!mddev->raid_disks && !mddev->external) |
| 5865 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5866 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5867 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | err = -ENODEV; |
| 5869 | goto abort_unlock; |
| 5870 | } |
| 5871 | |
| 5872 | /* |
| 5873 | * Commands even a read-only array can execute: |
| 5874 | */ |
| 5875 | switch (cmd) |
| 5876 | { |
| 5877 | case GET_ARRAY_INFO: |
| 5878 | err = get_array_info(mddev, argp); |
| 5879 | goto done_unlock; |
| 5880 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5881 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5882 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5883 | goto done_unlock; |
| 5884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | case GET_DISK_INFO: |
| 5886 | err = get_disk_info(mddev, argp); |
| 5887 | goto done_unlock; |
| 5888 | |
| 5889 | case RESTART_ARRAY_RW: |
| 5890 | err = restart_array(mddev); |
| 5891 | goto done_unlock; |
| 5892 | |
| 5893 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5894 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | goto done_unlock; |
| 5896 | |
| 5897 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5898 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | goto done_unlock; |
| 5900 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5901 | case BLKROSET: |
| 5902 | if (get_user(ro, (int __user *)(arg))) { |
| 5903 | err = -EFAULT; |
| 5904 | goto done_unlock; |
| 5905 | } |
| 5906 | err = -EINVAL; |
| 5907 | |
| 5908 | /* if the bdev is going readonly the value of mddev->ro |
| 5909 | * does not matter, no writes are coming |
| 5910 | */ |
| 5911 | if (ro) |
| 5912 | goto done_unlock; |
| 5913 | |
| 5914 | /* are we are already prepared for writes? */ |
| 5915 | if (mddev->ro != 1) |
| 5916 | goto done_unlock; |
| 5917 | |
| 5918 | /* transitioning to readauto need only happen for |
| 5919 | * arrays that call md_write_start |
| 5920 | */ |
| 5921 | if (mddev->pers) { |
| 5922 | err = restart_array(mddev); |
| 5923 | if (err == 0) { |
| 5924 | mddev->ro = 2; |
| 5925 | set_disk_ro(mddev->gendisk, 0); |
| 5926 | } |
| 5927 | } |
| 5928 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | /* |
| 5932 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5933 | * superblock, so we do not allow them on read-only arrays. |
| 5934 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5935 | * here and hit the 'default' below, so only disallow |
| 5936 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5938 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5939 | if (mddev->ro == 2) { |
| 5940 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5941 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5942 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5943 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5944 | } else { |
| 5945 | err = -EROFS; |
| 5946 | goto abort_unlock; |
| 5947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | switch (cmd) |
| 5951 | { |
| 5952 | case ADD_NEW_DISK: |
| 5953 | { |
| 5954 | mdu_disk_info_t info; |
| 5955 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5956 | err = -EFAULT; |
| 5957 | else |
| 5958 | err = add_new_disk(mddev, &info); |
| 5959 | goto done_unlock; |
| 5960 | } |
| 5961 | |
| 5962 | case HOT_REMOVE_DISK: |
| 5963 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5964 | goto done_unlock; |
| 5965 | |
| 5966 | case HOT_ADD_DISK: |
| 5967 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5968 | goto done_unlock; |
| 5969 | |
| 5970 | case SET_DISK_FAULTY: |
| 5971 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5972 | goto done_unlock; |
| 5973 | |
| 5974 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5975 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | goto done_unlock; |
| 5977 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5978 | case SET_BITMAP_FILE: |
| 5979 | err = set_bitmap_file(mddev, (int)arg); |
| 5980 | goto done_unlock; |
| 5981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | err = -EINVAL; |
| 5984 | goto abort_unlock; |
| 5985 | } |
| 5986 | |
| 5987 | done_unlock: |
| 5988 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5989 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5990 | err != -EINVAL) |
| 5991 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | mddev_unlock(mddev); |
| 5993 | |
| 5994 | return err; |
| 5995 | done: |
| 5996 | if (err) |
| 5997 | MD_BUG(); |
| 5998 | abort: |
| 5999 | return err; |
| 6000 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6001 | #ifdef CONFIG_COMPAT |
| 6002 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6003 | unsigned int cmd, unsigned long arg) |
| 6004 | { |
| 6005 | switch (cmd) { |
| 6006 | case HOT_REMOVE_DISK: |
| 6007 | case HOT_ADD_DISK: |
| 6008 | case SET_DISK_FAULTY: |
| 6009 | case SET_BITMAP_FILE: |
| 6010 | /* These take in integer arg, do not convert */ |
| 6011 | break; |
| 6012 | default: |
| 6013 | arg = (unsigned long)compat_ptr(arg); |
| 6014 | break; |
| 6015 | } |
| 6016 | |
| 6017 | return md_ioctl(bdev, mode, cmd, arg); |
| 6018 | } |
| 6019 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6021 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | { |
| 6023 | /* |
| 6024 | * Succeed if we can lock the mddev, which confirms that |
| 6025 | * it isn't being stopped right now. |
| 6026 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6027 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | int err; |
| 6029 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6030 | if (mddev->gendisk != bdev->bd_disk) { |
| 6031 | /* we are racing with mddev_put which is discarding this |
| 6032 | * bd_disk. |
| 6033 | */ |
| 6034 | mddev_put(mddev); |
| 6035 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6036 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6037 | /* Then retry the open from the top */ |
| 6038 | return -ERESTARTSYS; |
| 6039 | } |
| 6040 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6041 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6042 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | goto out; |
| 6044 | |
| 6045 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6046 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6047 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6049 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | out: |
| 6051 | return err; |
| 6052 | } |
| 6053 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6054 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6056 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6058 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6059 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | mddev_put(mddev); |
| 6061 | |
| 6062 | return 0; |
| 6063 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6064 | |
| 6065 | static int md_media_changed(struct gendisk *disk) |
| 6066 | { |
| 6067 | mddev_t *mddev = disk->private_data; |
| 6068 | |
| 6069 | return mddev->changed; |
| 6070 | } |
| 6071 | |
| 6072 | static int md_revalidate(struct gendisk *disk) |
| 6073 | { |
| 6074 | mddev_t *mddev = disk->private_data; |
| 6075 | |
| 6076 | mddev->changed = 0; |
| 6077 | return 0; |
| 6078 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6079 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | { |
| 6081 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6082 | .open = md_open, |
| 6083 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6084 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6085 | #ifdef CONFIG_COMPAT |
| 6086 | .compat_ioctl = md_compat_ioctl, |
| 6087 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6088 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6089 | .media_changed = md_media_changed, |
| 6090 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | }; |
| 6092 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6093 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | { |
| 6095 | mdk_thread_t *thread = arg; |
| 6096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | /* |
| 6098 | * md_thread is a 'system-thread', it's priority should be very |
| 6099 | * high. We avoid resource deadlocks individually in each |
| 6100 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6101 | * the very same RT priority as kswapd, thus we will never get |
| 6102 | * into a priority inversion deadlock. |
| 6103 | * |
| 6104 | * we definitely have to have equal or higher priority than |
| 6105 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6106 | * many dirty RAID5 blocks. |
| 6107 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6109 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6110 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6112 | /* We need to wait INTERRUPTIBLE so that |
| 6113 | * we don't add to the load-average. |
| 6114 | * That means we need to be sure no signals are |
| 6115 | * pending |
| 6116 | */ |
| 6117 | if (signal_pending(current)) |
| 6118 | flush_signals(current); |
| 6119 | |
| 6120 | wait_event_interruptible_timeout |
| 6121 | (thread->wqueue, |
| 6122 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6123 | || kthread_should_stop(), |
| 6124 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6126 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6127 | if (!kthread_should_stop()) |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 6128 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | return 0; |
| 6132 | } |
| 6133 | |
| 6134 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6135 | { |
| 6136 | if (thread) { |
| 6137 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6138 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6139 | wake_up(&thread->wqueue); |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6144 | const char *name) |
| 6145 | { |
| 6146 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6148 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | if (!thread) |
| 6150 | return NULL; |
| 6151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | init_waitqueue_head(&thread->wqueue); |
| 6153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | thread->run = run; |
| 6155 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6156 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6157 | thread->tsk = kthread_run(md_thread, thread, |
| 6158 | "%s_%s", |
| 6159 | mdname(thread->mddev), |
| 6160 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6161 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | kfree(thread); |
| 6163 | return NULL; |
| 6164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | return thread; |
| 6166 | } |
| 6167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | void md_unregister_thread(mdk_thread_t *thread) |
| 6169 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6170 | if (!thread) |
| 6171 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6172 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6173 | |
| 6174 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | kfree(thread); |
| 6176 | } |
| 6177 | |
| 6178 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6179 | { |
| 6180 | if (!mddev) { |
| 6181 | MD_BUG(); |
| 6182 | return; |
| 6183 | } |
| 6184 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6185 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6187 | |
| 6188 | if (mddev->external) |
| 6189 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6190 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6192 | mdname(mddev), |
| 6193 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6194 | __builtin_return_address(0),__builtin_return_address(1), |
| 6195 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6196 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6197 | if (!mddev->pers) |
| 6198 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | if (!mddev->pers->error_handler) |
| 6200 | return; |
| 6201 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6202 | if (mddev->degraded) |
| 6203 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6204 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6206 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6207 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6208 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6209 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6210 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | /* seq_file implementation /proc/mdstat */ |
| 6214 | |
| 6215 | static void status_unused(struct seq_file *seq) |
| 6216 | { |
| 6217 | int i = 0; |
| 6218 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | |
| 6220 | seq_printf(seq, "unused devices: "); |
| 6221 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6222 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | char b[BDEVNAME_SIZE]; |
| 6224 | i++; |
| 6225 | seq_printf(seq, "%s ", |
| 6226 | bdevname(rdev->bdev,b)); |
| 6227 | } |
| 6228 | if (!i) |
| 6229 | seq_printf(seq, "<none>"); |
| 6230 | |
| 6231 | seq_printf(seq, "\n"); |
| 6232 | } |
| 6233 | |
| 6234 | |
| 6235 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6236 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6237 | sector_t max_sectors, resync, res; |
| 6238 | unsigned long dt, db; |
| 6239 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6240 | int scale; |
| 6241 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6243 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | |
| 6245 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6246 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6248 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | |
| 6250 | /* |
| 6251 | * Should not happen. |
| 6252 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6253 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | MD_BUG(); |
| 6255 | return; |
| 6256 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6257 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6258 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6259 | * u32, as those are the requirements for sector_div. |
| 6260 | * Thus 'scale' must be at least 10 |
| 6261 | */ |
| 6262 | scale = 10; |
| 6263 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6264 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6265 | scale++; |
| 6266 | } |
| 6267 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6268 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6269 | |
| 6270 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6272 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | seq_printf(seq, "["); |
| 6274 | for (i = 0; i < x; i++) |
| 6275 | seq_printf(seq, "="); |
| 6276 | seq_printf(seq, ">"); |
| 6277 | for (i = 0; i < y; i++) |
| 6278 | seq_printf(seq, "."); |
| 6279 | seq_printf(seq, "] "); |
| 6280 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6281 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6282 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6283 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6284 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6285 | "check" : |
| 6286 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6287 | "resync" : "recovery"))), |
| 6288 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6289 | (unsigned long long) resync/2, |
| 6290 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | |
| 6292 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | * dt: time from mark until now |
| 6294 | * db: blocks written from mark until now |
| 6295 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6296 | * |
| 6297 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6298 | * So we divide before multiply in case it is 32bit and close |
| 6299 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6300 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6301 | * near the end of resync when the number of remaining sectors |
| 6302 | * is close to 'db'. |
| 6303 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6304 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | */ |
| 6306 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6307 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6308 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6309 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6311 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6312 | sector_div(rt, db/32+1); |
| 6313 | rt *= dt; |
| 6314 | rt >>= 5; |
| 6315 | |
| 6316 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6317 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6319 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6323 | { |
| 6324 | struct list_head *tmp; |
| 6325 | loff_t l = *pos; |
| 6326 | mddev_t *mddev; |
| 6327 | |
| 6328 | if (l >= 0x10000) |
| 6329 | return NULL; |
| 6330 | if (!l--) |
| 6331 | /* header */ |
| 6332 | return (void*)1; |
| 6333 | |
| 6334 | spin_lock(&all_mddevs_lock); |
| 6335 | list_for_each(tmp,&all_mddevs) |
| 6336 | if (!l--) { |
| 6337 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6338 | mddev_get(mddev); |
| 6339 | spin_unlock(&all_mddevs_lock); |
| 6340 | return mddev; |
| 6341 | } |
| 6342 | spin_unlock(&all_mddevs_lock); |
| 6343 | if (!l--) |
| 6344 | return (void*)2;/* tail */ |
| 6345 | return NULL; |
| 6346 | } |
| 6347 | |
| 6348 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6349 | { |
| 6350 | struct list_head *tmp; |
| 6351 | mddev_t *next_mddev, *mddev = v; |
| 6352 | |
| 6353 | ++*pos; |
| 6354 | if (v == (void*)2) |
| 6355 | return NULL; |
| 6356 | |
| 6357 | spin_lock(&all_mddevs_lock); |
| 6358 | if (v == (void*)1) |
| 6359 | tmp = all_mddevs.next; |
| 6360 | else |
| 6361 | tmp = mddev->all_mddevs.next; |
| 6362 | if (tmp != &all_mddevs) |
| 6363 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6364 | else { |
| 6365 | next_mddev = (void*)2; |
| 6366 | *pos = 0x10000; |
| 6367 | } |
| 6368 | spin_unlock(&all_mddevs_lock); |
| 6369 | |
| 6370 | if (v != (void*)1) |
| 6371 | mddev_put(mddev); |
| 6372 | return next_mddev; |
| 6373 | |
| 6374 | } |
| 6375 | |
| 6376 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6377 | { |
| 6378 | mddev_t *mddev = v; |
| 6379 | |
| 6380 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6381 | mddev_put(mddev); |
| 6382 | } |
| 6383 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6384 | struct mdstat_info { |
| 6385 | int event; |
| 6386 | }; |
| 6387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6389 | { |
| 6390 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6391 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6393 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6394 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | |
| 6396 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6397 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | seq_printf(seq, "Personalities : "); |
| 6399 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6400 | list_for_each_entry(pers, &pers_list, list) |
| 6401 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | |
| 6403 | spin_unlock(&pers_lock); |
| 6404 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6405 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | return 0; |
| 6407 | } |
| 6408 | if (v == (void*)2) { |
| 6409 | status_unused(seq); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6413 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6417 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6418 | mddev->pers ? "" : "in"); |
| 6419 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6420 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6422 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6423 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | seq_printf(seq, " %s", mddev->pers->name); |
| 6425 | } |
| 6426 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6427 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6428 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | char b[BDEVNAME_SIZE]; |
| 6430 | seq_printf(seq, " %s[%d]", |
| 6431 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6432 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6433 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6434 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | seq_printf(seq, "(F)"); |
| 6436 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6437 | } else if (rdev->raid_disk < 0) |
| 6438 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6439 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | if (!list_empty(&mddev->disks)) { |
| 6443 | if (mddev->pers) |
| 6444 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6445 | (unsigned long long) |
| 6446 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | else |
| 6448 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6449 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6451 | if (mddev->persistent) { |
| 6452 | if (mddev->major_version != 0 || |
| 6453 | mddev->minor_version != 90) { |
| 6454 | seq_printf(seq," super %d.%d", |
| 6455 | mddev->major_version, |
| 6456 | mddev->minor_version); |
| 6457 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6458 | } else if (mddev->external) |
| 6459 | seq_printf(seq, " super external:%s", |
| 6460 | mddev->metadata_type); |
| 6461 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6462 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | |
| 6464 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6465 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6467 | if (mddev->pers->sync_request) { |
| 6468 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6469 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6470 | seq_printf(seq, "\n "); |
| 6471 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6472 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6473 | else if (mddev->recovery_cp < MaxSector) |
| 6474 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6475 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6476 | } else |
| 6477 | seq_printf(seq, "\n "); |
| 6478 | |
| 6479 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6480 | unsigned long chunk_kb; |
| 6481 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6482 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6483 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6484 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6485 | "%lu%s chunk", |
| 6486 | bitmap->pages - bitmap->missing_pages, |
| 6487 | bitmap->pages, |
| 6488 | (bitmap->pages - bitmap->missing_pages) |
| 6489 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6490 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6491 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6492 | if (bitmap->file) { |
| 6493 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6494 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6495 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6496 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6497 | seq_printf(seq, "\n"); |
| 6498 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | seq_printf(seq, "\n"); |
| 6502 | } |
| 6503 | mddev_unlock(mddev); |
| 6504 | |
| 6505 | return 0; |
| 6506 | } |
| 6507 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6508 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | .start = md_seq_start, |
| 6510 | .next = md_seq_next, |
| 6511 | .stop = md_seq_stop, |
| 6512 | .show = md_seq_show, |
| 6513 | }; |
| 6514 | |
| 6515 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6516 | { |
| 6517 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6518 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6519 | if (mi == NULL) |
| 6520 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | |
| 6522 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6523 | if (error) |
| 6524 | kfree(mi); |
| 6525 | else { |
| 6526 | struct seq_file *p = file->private_data; |
| 6527 | p->private = mi; |
| 6528 | mi->event = atomic_read(&md_event_count); |
| 6529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | return error; |
| 6531 | } |
| 6532 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6533 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6534 | { |
| 6535 | struct seq_file *m = filp->private_data; |
| 6536 | struct mdstat_info *mi = m->private; |
| 6537 | int mask; |
| 6538 | |
| 6539 | poll_wait(filp, &md_event_waiters, wait); |
| 6540 | |
| 6541 | /* always allow read */ |
| 6542 | mask = POLLIN | POLLRDNORM; |
| 6543 | |
| 6544 | if (mi->event != atomic_read(&md_event_count)) |
| 6545 | mask |= POLLERR | POLLPRI; |
| 6546 | return mask; |
| 6547 | } |
| 6548 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6549 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6550 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | .open = md_seq_open, |
| 6552 | .read = seq_read, |
| 6553 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6554 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6555 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | }; |
| 6557 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6558 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6560 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6561 | list_add_tail(&p->list, &pers_list); |
| 6562 | 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] | 6563 | spin_unlock(&pers_lock); |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6567 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6569 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6571 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | spin_unlock(&pers_lock); |
| 6573 | return 0; |
| 6574 | } |
| 6575 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6576 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | { |
| 6578 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6580 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | |
| 6582 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6583 | rcu_read_lock(); |
| 6584 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6586 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6587 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6588 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6589 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6590 | * as sync_io is counted when a request starts, and |
| 6591 | * disk_stats is counted when it completes. |
| 6592 | * So resync activity will cause curr_events to be smaller than |
| 6593 | * when there was no such activity. |
| 6594 | * non-sync IO will cause disk_stat to increase without |
| 6595 | * increasing sync_io so curr_events will (eventually) |
| 6596 | * be larger than it was before. Once it becomes |
| 6597 | * substantially larger, the test below will cause |
| 6598 | * the array to appear non-idle, and resync will slow |
| 6599 | * down. |
| 6600 | * If there is a lot of outstanding resync activity when |
| 6601 | * we set last_event to curr_events, then all that activity |
| 6602 | * completing might cause the array to appear non-idle |
| 6603 | * and resync will be slowed down even though there might |
| 6604 | * not have been non-resync activity. This will only |
| 6605 | * happen once though. 'last_events' will soon reflect |
| 6606 | * the state where there is little or no outstanding |
| 6607 | * resync requests, and further resync activity will |
| 6608 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6609 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6611 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | rdev->last_events = curr_events; |
| 6613 | idle = 0; |
| 6614 | } |
| 6615 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6616 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6617 | return idle; |
| 6618 | } |
| 6619 | |
| 6620 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6621 | { |
| 6622 | /* another "blocks" (512byte) blocks have been synced */ |
| 6623 | atomic_sub(blocks, &mddev->recovery_active); |
| 6624 | wake_up(&mddev->recovery_wait); |
| 6625 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6626 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | md_wakeup_thread(mddev->thread); |
| 6628 | // stop recovery, signal do_sync .... |
| 6629 | } |
| 6630 | } |
| 6631 | |
| 6632 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6633 | /* md_write_start(mddev, bi) |
| 6634 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6635 | * in superblock) before writing, schedule a superblock update |
| 6636 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6637 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6638 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6640 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6641 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6642 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6643 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6644 | BUG_ON(mddev->ro == 1); |
| 6645 | if (mddev->ro == 2) { |
| 6646 | /* need to switch to read/write */ |
| 6647 | mddev->ro = 0; |
| 6648 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6649 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6650 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6651 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6652 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6653 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6654 | if (mddev->safemode == 1) |
| 6655 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6656 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6657 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6658 | if (mddev->in_sync) { |
| 6659 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6660 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6661 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6662 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6663 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6664 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6665 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6666 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6667 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6668 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6669 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6670 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | void md_write_end(mddev_t *mddev) |
| 6674 | { |
| 6675 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6676 | if (mddev->safemode == 2) |
| 6677 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6678 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6680 | } |
| 6681 | } |
| 6682 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6683 | /* md_allow_write(mddev) |
| 6684 | * Calling this ensures that the array is marked 'active' so that writes |
| 6685 | * may proceed without blocking. It is important to call this before |
| 6686 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6687 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6688 | * |
| 6689 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6690 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6691 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6692 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6693 | { |
| 6694 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6695 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6696 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6697 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6698 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6699 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6700 | |
| 6701 | spin_lock_irq(&mddev->write_lock); |
| 6702 | if (mddev->in_sync) { |
| 6703 | mddev->in_sync = 0; |
| 6704 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6705 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6706 | if (mddev->safemode_delay && |
| 6707 | mddev->safemode == 0) |
| 6708 | mddev->safemode = 1; |
| 6709 | spin_unlock_irq(&mddev->write_lock); |
| 6710 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6711 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6712 | } else |
| 6713 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6714 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6715 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6716 | return -EAGAIN; |
| 6717 | else |
| 6718 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6719 | } |
| 6720 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | #define SYNC_MARKS 10 |
| 6723 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6724 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | { |
| 6726 | mddev_t *mddev2; |
| 6727 | unsigned int currspeed = 0, |
| 6728 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6729 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | unsigned long mark[SYNC_MARKS]; |
| 6731 | sector_t mark_cnt[SYNC_MARKS]; |
| 6732 | int last_mark,m; |
| 6733 | struct list_head *tmp; |
| 6734 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6735 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6736 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6737 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | |
| 6739 | /* just incase thread restarts... */ |
| 6740 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6741 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6742 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6743 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6745 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6746 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6747 | desc = "data-check"; |
| 6748 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6749 | desc = "requested-resync"; |
| 6750 | else |
| 6751 | desc = "resync"; |
| 6752 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6753 | desc = "reshape"; |
| 6754 | else |
| 6755 | desc = "recovery"; |
| 6756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6757 | /* we overload curr_resync somewhat here. |
| 6758 | * 0 == not engaged in resync at all |
| 6759 | * 2 == checking that there is no conflict with another sync |
| 6760 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6761 | * commense |
| 6762 | * other == active in resync - this many blocks |
| 6763 | * |
| 6764 | * Before starting a resync we must have set curr_resync to |
| 6765 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6766 | * less than ours. When we find one that is the same or higher |
| 6767 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6768 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6769 | * This will mean we have to start checking from the beginning again. |
| 6770 | * |
| 6771 | */ |
| 6772 | |
| 6773 | do { |
| 6774 | mddev->curr_resync = 2; |
| 6775 | |
| 6776 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6777 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6778 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6779 | |
| 6780 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6782 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | if (mddev2 == mddev) |
| 6784 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6785 | if (!mddev->parallel_resync |
| 6786 | && mddev2->curr_resync |
| 6787 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | DEFINE_WAIT(wq); |
| 6789 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6790 | /* arbitrarily yield */ |
| 6791 | mddev->curr_resync = 1; |
| 6792 | wake_up(&resync_wait); |
| 6793 | } |
| 6794 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6795 | /* no need to wait here, we can wait the next |
| 6796 | * time 'round when curr_resync == 2 |
| 6797 | */ |
| 6798 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6799 | /* We need to wait 'interruptible' so as not to |
| 6800 | * contribute to the load average, and not to |
| 6801 | * be caught by 'softlockup' |
| 6802 | */ |
| 6803 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6804 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6805 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6806 | printk(KERN_INFO "md: delaying %s of %s" |
| 6807 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6809 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6811 | if (signal_pending(current)) |
| 6812 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | schedule(); |
| 6814 | finish_wait(&resync_wait, &wq); |
| 6815 | goto try_again; |
| 6816 | } |
| 6817 | finish_wait(&resync_wait, &wq); |
| 6818 | } |
| 6819 | } |
| 6820 | } while (mddev->curr_resync < 2); |
| 6821 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6822 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6823 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6825 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | */ |
| 6827 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6828 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6829 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6830 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6831 | j = mddev->resync_min; |
| 6832 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6833 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6834 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6835 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6836 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6837 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6839 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6840 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6841 | rcu_read_lock(); |
| 6842 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6843 | if (rdev->raid_disk >= 0 && |
| 6844 | !test_bit(Faulty, &rdev->flags) && |
| 6845 | !test_bit(In_sync, &rdev->flags) && |
| 6846 | rdev->recovery_offset < j) |
| 6847 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6848 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6851 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6852 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6853 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6854 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6855 | "(but not more than %d KB/sec) for %s.\n", |
| 6856 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6858 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6859 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6860 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6862 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6863 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | } |
| 6865 | last_mark = 0; |
| 6866 | mddev->resync_mark = mark[last_mark]; |
| 6867 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6868 | |
| 6869 | /* |
| 6870 | * Tune reconstruction: |
| 6871 | */ |
| 6872 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 6873 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 6874 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | |
| 6876 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | last_check = 0; |
| 6878 | |
| 6879 | if (j>2) { |
| 6880 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6881 | "md: resuming %s of %s from checkpoint.\n", |
| 6882 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | mddev->curr_resync = j; |
| 6884 | } |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6885 | mddev->curr_resync_completed = j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | |
| 6887 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6888 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6890 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6891 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6892 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6893 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6894 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6895 | > (max_sectors >> 4)) || |
| 6896 | (j - mddev->curr_resync_completed)*2 |
| 6897 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6898 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6899 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6900 | wait_event(mddev->recovery_wait, |
| 6901 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6902 | mddev->curr_resync_completed = j; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6903 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6904 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6905 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6906 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6907 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6908 | /* As this condition is controlled by user-space, |
| 6909 | * we can block indefinitely, so use '_interruptible' |
| 6910 | * to avoid triggering warnings. |
| 6911 | */ |
| 6912 | flush_signals(current); /* just in case */ |
| 6913 | wait_event_interruptible(mddev->recovery_wait, |
| 6914 | mddev->resync_max > j |
| 6915 | || kthread_should_stop()); |
| 6916 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6917 | |
| 6918 | if (kthread_should_stop()) |
| 6919 | goto interrupted; |
| 6920 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6921 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6922 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6923 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6924 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | goto out; |
| 6926 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6927 | |
| 6928 | if (!skipped) { /* actual IO requested */ |
| 6929 | io_sectors += sectors; |
| 6930 | atomic_add(sectors, &mddev->recovery_active); |
| 6931 | } |
| 6932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | j += sectors; |
| 6934 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6935 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6936 | if (last_check == 0) |
| 6937 | /* this is the earliers that rebuilt will be |
| 6938 | * visible in /proc/mdstat |
| 6939 | */ |
| 6940 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6941 | |
| 6942 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | continue; |
| 6944 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6945 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6947 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | break; |
| 6949 | |
| 6950 | repeat: |
| 6951 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6952 | /* step marks */ |
| 6953 | int next = (last_mark+1) % SYNC_MARKS; |
| 6954 | |
| 6955 | mddev->resync_mark = mark[next]; |
| 6956 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6957 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6958 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | last_mark = next; |
| 6960 | } |
| 6961 | |
| 6962 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6963 | if (kthread_should_stop()) |
| 6964 | goto interrupted; |
| 6965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | |
| 6967 | /* |
| 6968 | * this loop exits only if either when we are slower than |
| 6969 | * the 'hard' speed limit, or the system was IO-idle for |
| 6970 | * a jiffy. |
| 6971 | * the system might be non-idle CPU-wise, but we only care |
| 6972 | * about not overloading the IO subsystem. (things like an |
| 6973 | * e2fsck being done on the RAID array should execute fast) |
| 6974 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | cond_resched(); |
| 6976 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6977 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6978 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6980 | if (currspeed > speed_min(mddev)) { |
| 6981 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6982 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6983 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | goto repeat; |
| 6985 | } |
| 6986 | } |
| 6987 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6988 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6989 | /* |
| 6990 | * this also signals 'finished resyncing' to md_stop |
| 6991 | */ |
| 6992 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6994 | |
| 6995 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6996 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6998 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6999 | mddev->curr_resync > 2) { |
| 7000 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7001 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7002 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7003 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7004 | "md: checkpointing %s of %s.\n", |
| 7005 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7006 | mddev->recovery_cp = mddev->curr_resync; |
| 7007 | } |
| 7008 | } else |
| 7009 | mddev->recovery_cp = MaxSector; |
| 7010 | } else { |
| 7011 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7012 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7013 | rcu_read_lock(); |
| 7014 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7015 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7016 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7017 | !test_bit(Faulty, &rdev->flags) && |
| 7018 | !test_bit(In_sync, &rdev->flags) && |
| 7019 | rdev->recovery_offset < mddev->curr_resync) |
| 7020 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7021 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7024 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7027 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7028 | /* We completed so min/max setting can be forgotten if used. */ |
| 7029 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7030 | mddev->resync_min = 0; |
| 7031 | mddev->resync_max = MaxSector; |
| 7032 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7033 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | mddev->curr_resync = 0; |
| 7035 | wake_up(&resync_wait); |
| 7036 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7037 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7038 | return; |
| 7039 | |
| 7040 | interrupted: |
| 7041 | /* |
| 7042 | * got a signal, exit. |
| 7043 | */ |
| 7044 | printk(KERN_INFO |
| 7045 | "md: md_do_sync() got signal ... exiting\n"); |
| 7046 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7047 | goto out; |
| 7048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7050 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7052 | static int remove_and_add_spares(mddev_t *mddev) |
| 7053 | { |
| 7054 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7055 | int spares = 0; |
| 7056 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7057 | mddev->curr_resync_completed = 0; |
| 7058 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7059 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7060 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7061 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7062 | (test_bit(Faulty, &rdev->flags) || |
| 7063 | ! test_bit(In_sync, &rdev->flags)) && |
| 7064 | atomic_read(&rdev->nr_pending)==0) { |
| 7065 | if (mddev->pers->hot_remove_disk( |
| 7066 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7067 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7068 | rdev->raid_disk = -1; |
| 7069 | } |
| 7070 | } |
| 7071 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7072 | if (mddev->degraded) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7073 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7074 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7075 | !test_bit(In_sync, &rdev->flags) && |
NeilBrown | 4274215 | 2011-06-28 16:59:42 +1000 | [diff] [blame] | 7076 | !test_bit(Faulty, &rdev->flags) && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7077 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7078 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7079 | if (rdev->raid_disk < 0 |
| 7080 | && !test_bit(Faulty, &rdev->flags)) { |
| 7081 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7082 | if (mddev->pers-> |
| 7083 | hot_add_disk(mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7084 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7085 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7086 | spares++; |
| 7087 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7088 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7089 | } else |
| 7090 | break; |
| 7091 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7092 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7093 | } |
| 7094 | return spares; |
| 7095 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7096 | |
| 7097 | static void reap_sync_thread(mddev_t *mddev) |
| 7098 | { |
| 7099 | mdk_rdev_t *rdev; |
| 7100 | |
| 7101 | /* resync has finished, collect result */ |
| 7102 | md_unregister_thread(mddev->sync_thread); |
| 7103 | mddev->sync_thread = NULL; |
| 7104 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7105 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7106 | /* success...*/ |
| 7107 | /* activate any spares */ |
| 7108 | if (mddev->pers->spare_active(mddev)) |
| 7109 | sysfs_notify(&mddev->kobj, NULL, |
| 7110 | "degraded"); |
| 7111 | } |
| 7112 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7113 | mddev->pers->finish_reshape) |
| 7114 | mddev->pers->finish_reshape(mddev); |
| 7115 | md_update_sb(mddev, 1); |
| 7116 | |
| 7117 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7118 | * information must be scrapped |
| 7119 | */ |
| 7120 | if (!mddev->degraded) |
| 7121 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7122 | rdev->saved_raid_disk = -1; |
| 7123 | |
| 7124 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7125 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7126 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7127 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7128 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7129 | /* flag recovery needed just to double check */ |
| 7130 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7131 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7132 | md_new_event(mddev); |
| 7133 | } |
| 7134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | /* |
| 7136 | * This routine is regularly called by all per-raid-array threads to |
| 7137 | * deal with generic issues like resync and super-block update. |
| 7138 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7139 | * need this as they never do any recovery or update the superblock. |
| 7140 | * |
| 7141 | * It does not do any resync itself, but rather "forks" off other threads |
| 7142 | * to do that as needed. |
| 7143 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7144 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7145 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | * and wakeups up this thread which will reap the thread and finish up. |
| 7147 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7148 | * |
| 7149 | * The overall approach is: |
| 7150 | * 1/ if the superblock needs updating, update it. |
| 7151 | * 2/ If a recovery thread is running, don't do anything else. |
| 7152 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7153 | * 4/ If there are any faulty devices, remove them. |
| 7154 | * 5/ If array is degraded, try to add spares devices |
| 7155 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7156 | */ |
| 7157 | void md_check_recovery(mddev_t *mddev) |
| 7158 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7159 | if (mddev->suspended) |
| 7160 | return; |
| 7161 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7162 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7163 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7165 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7166 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7167 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7168 | mdname(mddev)); |
| 7169 | mddev->safemode = 2; |
| 7170 | } |
| 7171 | flush_signals(current); |
| 7172 | } |
| 7173 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7174 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7175 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7176 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7177 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7178 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7179 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7180 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7181 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7182 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | )) |
| 7184 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7185 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7186 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7187 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7188 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7189 | if (mddev->ro) { |
| 7190 | /* Only thing we do on a ro array is remove |
| 7191 | * failed devices. |
| 7192 | */ |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7193 | mdk_rdev_t *rdev; |
| 7194 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 7195 | if (rdev->raid_disk >= 0 && |
| 7196 | !test_bit(Blocked, &rdev->flags) && |
| 7197 | test_bit(Faulty, &rdev->flags) && |
| 7198 | atomic_read(&rdev->nr_pending)==0) { |
| 7199 | if (mddev->pers->hot_remove_disk( |
| 7200 | mddev, rdev->raid_disk)==0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7201 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a8c42c7 | 2011-01-31 13:47:13 +1100 | [diff] [blame] | 7202 | rdev->raid_disk = -1; |
| 7203 | } |
| 7204 | } |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7205 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7206 | goto unlock; |
| 7207 | } |
| 7208 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7209 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7210 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7211 | spin_lock_irq(&mddev->write_lock); |
| 7212 | if (mddev->safemode && |
| 7213 | !atomic_read(&mddev->writes_pending) && |
| 7214 | !mddev->in_sync && |
| 7215 | mddev->recovery_cp == MaxSector) { |
| 7216 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7217 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7218 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7219 | } |
| 7220 | if (mddev->safemode == 1) |
| 7221 | mddev->safemode = 0; |
| 7222 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7223 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7224 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7225 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7226 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7227 | if (mddev->flags) |
| 7228 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7231 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7232 | /* resync/recovery still happening */ |
| 7233 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7234 | goto unlock; |
| 7235 | } |
| 7236 | if (mddev->sync_thread) { |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7237 | reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | goto unlock; |
| 7239 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7240 | /* Set RUNNING before clearing NEEDED to avoid |
| 7241 | * any transients in the value of "sync_action". |
| 7242 | */ |
| 7243 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7244 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7245 | /* Clear some bits that don't mean anything, but |
| 7246 | * might be left set |
| 7247 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7248 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7249 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7251 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7252 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | /* no recovery is running. |
| 7254 | * remove any failed drives, then |
| 7255 | * add spares if possible. |
| 7256 | * Spare are also removed and re-added, to allow |
| 7257 | * the personality to fail the re-add. |
| 7258 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7260 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7261 | if (mddev->pers->check_reshape == NULL || |
| 7262 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7263 | /* Cannot proceed */ |
| 7264 | goto unlock; |
| 7265 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7266 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7267 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7268 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7269 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7270 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7271 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7272 | } else if (mddev->recovery_cp < MaxSector) { |
| 7273 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7274 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7275 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7276 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7277 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7280 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7281 | /* We are adding a device or devices to an array |
| 7282 | * which has the bitmap stored on all devices. |
| 7283 | * So make sure all bitmap pages get written |
| 7284 | */ |
| 7285 | bitmap_write_all(mddev->bitmap); |
| 7286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7288 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7289 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | if (!mddev->sync_thread) { |
| 7291 | printk(KERN_ERR "%s: could not start resync" |
| 7292 | " thread...\n", |
| 7293 | mdname(mddev)); |
| 7294 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7295 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7296 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7297 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7298 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7299 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7300 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7302 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7303 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | } |
| 7305 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7306 | if (!mddev->sync_thread) { |
| 7307 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7308 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7309 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7310 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7311 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7313 | mddev_unlock(mddev); |
| 7314 | } |
| 7315 | } |
| 7316 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7317 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7318 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7319 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7320 | wait_event_timeout(rdev->blocked_wait, |
| 7321 | !test_bit(Blocked, &rdev->flags), |
| 7322 | msecs_to_jiffies(5000)); |
| 7323 | rdev_dec_pending(rdev, mddev); |
| 7324 | } |
| 7325 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7326 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7327 | static int md_notify_reboot(struct notifier_block *this, |
| 7328 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | { |
| 7330 | struct list_head *tmp; |
| 7331 | mddev_t *mddev; |
| 7332 | |
| 7333 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7334 | |
| 7335 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7336 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7337 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7338 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7339 | /* Force a switch to readonly even array |
| 7340 | * appears to still be in use. Hence |
| 7341 | * the '100'. |
| 7342 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7343 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7344 | mddev_unlock(mddev); |
| 7345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | /* |
| 7347 | * certain more exotic SCSI devices are known to be |
| 7348 | * volatile wrt too early system reboots. While the |
| 7349 | * right place to handle this issue is the given |
| 7350 | * driver, we do want to have a safe RAID driver ... |
| 7351 | */ |
| 7352 | mdelay(1000*1); |
| 7353 | } |
| 7354 | return NOTIFY_DONE; |
| 7355 | } |
| 7356 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7357 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | .notifier_call = md_notify_reboot, |
| 7359 | .next = NULL, |
| 7360 | .priority = INT_MAX, /* before any real devices */ |
| 7361 | }; |
| 7362 | |
| 7363 | static void md_geninit(void) |
| 7364 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7366 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7367 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7368 | } |
| 7369 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7370 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7371 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7372 | int ret = -ENOMEM; |
| 7373 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 7374 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7375 | if (!md_wq) |
| 7376 | goto err_wq; |
| 7377 | |
| 7378 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7379 | if (!md_misc_wq) |
| 7380 | goto err_misc_wq; |
| 7381 | |
| 7382 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7383 | goto err_md; |
| 7384 | |
| 7385 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7386 | goto err_mdp; |
| 7387 | mdp_major = ret; |
| 7388 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7389 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7390 | md_probe, NULL, NULL); |
| 7391 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | md_probe, NULL, NULL); |
| 7393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7395 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | |
| 7397 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7400 | err_mdp: |
| 7401 | unregister_blkdev(MD_MAJOR, "md"); |
| 7402 | err_md: |
| 7403 | destroy_workqueue(md_misc_wq); |
| 7404 | err_misc_wq: |
| 7405 | destroy_workqueue(md_wq); |
| 7406 | err_wq: |
| 7407 | return ret; |
| 7408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | |
| 7410 | #ifndef MODULE |
| 7411 | |
| 7412 | /* |
| 7413 | * Searches all registered partitions for autorun RAID arrays |
| 7414 | * at boot time. |
| 7415 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7416 | |
| 7417 | static LIST_HEAD(all_detected_devices); |
| 7418 | struct detected_devices_node { |
| 7419 | struct list_head list; |
| 7420 | dev_t dev; |
| 7421 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7422 | |
| 7423 | void md_autodetect_dev(dev_t dev) |
| 7424 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7425 | struct detected_devices_node *node_detected_dev; |
| 7426 | |
| 7427 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7428 | if (node_detected_dev) { |
| 7429 | node_detected_dev->dev = dev; |
| 7430 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7431 | } else { |
| 7432 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7433 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | |
| 7438 | static void autostart_arrays(int part) |
| 7439 | { |
| 7440 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7441 | struct detected_devices_node *node_detected_dev; |
| 7442 | dev_t dev; |
| 7443 | int i_scanned, i_passed; |
| 7444 | |
| 7445 | i_scanned = 0; |
| 7446 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7447 | |
| 7448 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7449 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7450 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7451 | i_scanned++; |
| 7452 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7453 | struct detected_devices_node, list); |
| 7454 | list_del(&node_detected_dev->list); |
| 7455 | dev = node_detected_dev->dev; |
| 7456 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7457 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | if (IS_ERR(rdev)) |
| 7459 | continue; |
| 7460 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7461 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | MD_BUG(); |
| 7463 | continue; |
| 7464 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7465 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7466 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7467 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7469 | |
| 7470 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7471 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | |
| 7473 | autorun_devices(part); |
| 7474 | } |
| 7475 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7476 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7477 | |
| 7478 | static __exit void md_exit(void) |
| 7479 | { |
| 7480 | mddev_t *mddev; |
| 7481 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7482 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7483 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7484 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7485 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7486 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | unregister_blkdev(mdp_major, "mdp"); |
| 7488 | unregister_reboot_notifier(&md_notifier); |
| 7489 | unregister_sysctl_table(raid_table_header); |
| 7490 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7491 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7492 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7493 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7495 | destroy_workqueue(md_misc_wq); |
| 7496 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7497 | } |
| 7498 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7499 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7500 | module_exit(md_exit) |
| 7501 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7502 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7503 | { |
| 7504 | return sprintf(buffer, "%d", start_readonly); |
| 7505 | } |
| 7506 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7507 | { |
| 7508 | char *e; |
| 7509 | int num = simple_strtoul(val, &e, 10); |
| 7510 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7511 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7512 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7513 | } |
| 7514 | return -EINVAL; |
| 7515 | } |
| 7516 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7517 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7518 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7519 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7520 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | EXPORT_SYMBOL(register_md_personality); |
| 7523 | EXPORT_SYMBOL(unregister_md_personality); |
| 7524 | EXPORT_SYMBOL(md_error); |
| 7525 | EXPORT_SYMBOL(md_done_sync); |
| 7526 | EXPORT_SYMBOL(md_write_start); |
| 7527 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | EXPORT_SYMBOL(md_register_thread); |
| 7529 | EXPORT_SYMBOL(md_unregister_thread); |
| 7530 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7531 | EXPORT_SYMBOL(md_check_recovery); |
| 7532 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7533 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7534 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7535 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |